written 8.3 years ago by | modified 2.8 years ago by |
Mumbai university > FE > SEM 2 > Structured Programming Approach
Marks: 10M
Year: May 2014
written 8.3 years ago by | modified 2.8 years ago by |
Mumbai university > FE > SEM 2 > Structured Programming Approach
Marks: 10M
Year: May 2014
written 8.3 years ago by |
Pattern 1:
The pattern 1 shown is known as Floyd triangle of row = 4.
Algorithm:
Start
Declare and initialize required variables for controlling loop, inputting number of rows and printing numbers.
Enter the number of rows to be printed.
Print the number in standard format utilizing the application of loop as follow:
do for x=1 to n.
do for y=1 to n.
print number.
increase the number answer y by 1.
go to next line.
Print triangle.
Stop.
Program:
#include <stdio.h>
int main() {
int n, i, c, a = 1;
printf("Enter the number of rows of Floyd's triangle to print:\n");
scanf("%d", &n);
for (i = 1; i <= n; i++) {
for (c = 1; c <= i; c++) {
printf("%d ",a);
a++;
}
printf("\n");
}
return 0;
}
Output:
Enter the number of rows of Floyd's triangle to print: 4
Pattern 2:
In this pattern we use row = 3.
Algorithm:
Start
Declare and initialize required variables.
Enter the number of rows to be printed.
Print the number in standard format utilizing the application of loop as follow:
do for i=1 to no. of rows required.
do for j=1 to no. of rows - 1.
do for j=1 to i.
Print star.
Stop.
Program:
#include<stdio.h>
#include<conio.h>
int main(){
int i, j;
for(i = 1; i<=3; i++) {
for(j = 1; j<=3-i; j++)
printf(" ");
for(j = 1; j<=i; j++)
printf("*");
printf("\n");
}
for(i = 1; i<=3; i++) {
for(j = 1; j<=i; j++)
printf(" ");
for(j = 1; j<=3-i; j++)
printf("*");
printf("\n");
}
getch();
}
Output: