Diamond Pattern C program Posted: 03 Jul 2013 09:19 AM PDT Q. Write a C program to print the following diamond pattern as: 4434432344321234432344344 Ans. /*c program for diamond pattern*/#include<stdio.h>int main(){ int num,r,c,z,n,nm,no; printf("Enter max. number : "); scanf("%d", &num); nm = num+1; for(r=1; r<=num; r++,nm--) { for(c=1,n=num; c<=r; c++,n--) printf("%d",n); for(z=nm,c=1; c<r; c++,z++) printf("%d",z); printf("\n"); } no = num-1; for(r=1; r<=num; r++,no++) { for(c=num; c>r; c--) printf("%d",c); for(z=no,c=num-2; c>=r; c--,z++) printf("%d",z); printf("\n"); } getch(); return 0;} The output of above program would be:
| Figure: Screen-shot for diamond pattern C program |
|
Number Triangle Posted: 03 Jul 2013 07:42 AM PDT Q. Write a C program to print the following number triangle/pyramid program: 1121231234123121 Ans. /*c program for number triangle*/#include<stdio.h>int main(){ int num,n,r,c,z; printf("Enter max. number : "); scanf("%d", &num); for(r=1; r<=num; r++) { for(c=1; c<=r; c++) printf("%d",c); printf("\n"); } for(r=1; r<num; r++) { for(c=num,z=1; c>r; c--,z++) printf("%d",z); printf("\n"); } getch(); return 0;} The output of above program would be:
| Figure: Screen-shot for number triangle C program |
|
Number-Character Pattern Posted: 03 Jul 2013 07:21 AM PDT Q. Write a C program to print the following number-character pattern as: 11111111A1AAAA1AA1AAA1AAA1AA1AAAA1A1AAAAA11111111 Ans. /*c program for number-character pattern*/#include<stdio.h>int main(){ int num=7,r,c,z; printf("1111111\n"); for(r=1; r<=5; r++) { for(c=1; c<=num; c++) { if(c==1) printf("1"); else { if( (r==1 && c==3) || (r==2 && c==4) || (r==3 && c==5) || (r==4 && c==6) || (r==5 && c==7) ) printf("1"); else printf("A"); } } printf("\n"); } printf("1111111"); getch(); return 0;} The output of above program would be:
| Figure: Screen-shot for number-character pattern C program |
|
Odd Number Pyramid Posted: 03 Jul 2013 06:55 AM PDT Q. Write a C program to print the following odd number pyramid as: 13 55 7 97 9 11 13 Ans. /*c program for odd number pyramid*/#include<stdio.h>int main(){ int num,r,c,z=1,p; printf("Enter no. of rows : "); scanf("%d", &num); for(r=1; r<=num; r++,z=z+2) { for(c=1,p=z; c<=r; c++,p=p+2) printf(" %d",p); printf("\n"); } getch(); return 0;} The output of above program would be:
| Figure: Screen-shot for odd number pyramid C program |
|
Post a Comment