C Programming

C Programming


Increased Number Triangle

Posted: 01 Sep 2013 04:44 AM PDT

Q. Write a C program to print the following Increased Number Triangle as:

    5
   45
  345
 2345
12345

Ans.

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

 int num,r,n,sp;
 printf("Enter no. of rows : ");
 scanf("%d", &num);
 for(r=1; r<=num; r++)
 {
  for(sp=num-r; sp>=1; sp--)
     printf(" ");
  for(n=num-r+1; n<=num; n++)
     printf("%d",n);
  printf("\n");
 }
 getch();
 return 0;
}

The output of above program would be:

Output of Increased Number Triangle C program
Figure: Screen-shot for Increased Number Triangle C program


Decreased Number Triangle

Posted: 01 Sep 2013 03:58 AM PDT

Q. Write a C program for decreased number triangle as:

5
54
543
5432
54321

Ans.

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

{
 int num,r,c,n;
 printf("Enter no. of rows : ");
 scanf("%d", &num);
 for(r=1; r<=num; r++)
 {
   for(c=1,n=num; c<=r; c++,n--)
       printf("%d",n);
   printf("\n");
 }
 getch();
 return 0;
}

The output of above program would be:

Output of Decreased Number Triangle C program
Figure: Screen-shot for Decreased Number Triangle C program



Half-Square Number Triangle

Posted: 01 Sep 2013 03:10 AM PDT

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

543212345
 4321234
  32123
   212
    1

Ans.

/*c program for half-square number triangle*/
#include<stdio.h>
int main()
{
 int num,r,c,n,sp,p;

 printf("Enter any number : ");
 scanf("%d", &num);
 for(r=1,n=num; r<=num; r++,n--)
 {
   for(sp=r; sp>1; sp--)
      printf(" ");
   for(c=r,p=n; c<=num; c++,p--)
      printf("%d",p);
   for(c=num-r,p=2; c>=1; c--,p++)
      printf("%d",p);
   printf("\n");
 }
 getch();
 return 0;
}

The output of above program would be:


Output of Half-Square Number Triangle C program
Figure :  Screen shot for Half-Square Number Triangle C program


Odd-Number Triangle

Posted: 01 Sep 2013 02:39 AM PDT

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


V-Shape Pyramid

Posted: 01 Sep 2013 02:08 AM PDT

Q. Write a C program to print the following V symbol star pyramid as:

*     *
 *   *
  * *
   *

Ans.

/*c pyramid program for v-symbol*/
#include<stdio.h>
int main()
{

 int num=4,r,c,sp;
 for(r=1,c=num+1; r<=num; r++,c=c-2)
 {
   for(sp=r; sp>1; sp--)
      printf(" ");
   printf("*");
   for(sp=c; sp>=1; sp--)
      printf(" ");
   if(c>=1)
      printf("*");
   printf("\n");
 }
 getch();
 return 0;
}

The output of above program would be:


Output of V-Shape C pyramid program
Figure : Screen shot for V-Shape pyramid C program




Post a Comment