Sunday, September 1, 2013

Odd-Number Triangle

Q. Write a C program to print the following odd-number triangle C program as:

135
11111
33333
55555

Ans.

/*c program for odd number triangle*/
#include<stdio.h>
int main()
{

 int num=5,r,c,p;
 //as num=5 is given 
 for(r=1,p=1; r<num-1; r++,p=p+2)
    printf("%d",p);
 printf("\n");
 for(r=1,p=1; r<num-1; r++,p=p+2)
 {
  for(c=1; c<=num; c++)
     printf("%d",p);
  printf("\n");
 }
 getch();
 return 0;
}

The output of above program would be:

Output of odd number triangle C program
Figure : Screen shot for odd number triangle C program


No comments:

Post a Comment