C Programming

C Programming


Diamond Pattern C program

Posted: 03 Jul 2013 09:19 AM PDT

Q. Write a C program to print the following diamond pattern as:

4
434
43234
4321234
43234
434
4

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:


Output of diamond pattern C program
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:

1
12
123
1234
123
12
1

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:

Output of number triangle C program
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:

1111111
1A1AAAA
1AA1AAA
1AAA1AA
1AAAA1A
1AAAAA1
1111111

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:

output of Number-Character pattern C program
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:

1
3 5
5 7 9
7 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:

output for odd number pyramid C program
Figure: Screen-shot for odd number pyramid C program




Post a Comment