Wednesday, September 25, 2013

Calculating Sum of Number of elements

Q. Write a C program to accept the number of elements to be insert from the user and calculate these elements sum.

For example:

if user enter no. of elements : 5
then he/she should enter elements value as:
Enter 1 element/number : 2
Enter 2 element/number : 7
Enter 3 element/number : 1
Enter 4 element/number : 10
Enter 5 element/number : 50
Result will be : 
Sum of elements/numbers : 70

Ans.

/*c program for entered number of elements and calculate the sum*/
#include<stdio.h>
int main()
{
 int num,arr[20],i,sum=0;
 //arr[20] value of changeable as big value

 printf("Enter number of element : ");
 scanf("%d", &num);
 for(i=1; i<=num; i++)
 {
    printf("Enter %d element : ",i);
    scanf("%d", &arr[i]);
 }
 for(i=1; i<=num; i++)
    sum = sum + arr[i];
 printf("\nSum of all numbers : %d",sum);
 getch();
 return 0;
}

/******************************************
The output of above program would be
*******************************************/


Output of Calculating Sum of Number of elements C program
Figure : Screen-shot for Calculating Sum of Number of
 elements C program

Saturday, September 21, 2013

Star-Zero Nested Pattern

Q. Write a C program to print the following star-zero nested pattern pyramid as:

*000*000*
0*00*00*0
00*0*0*00
000***000

Ans.

How to make above star-zero nested pattern:

*000*000*
0*00*00*0
00*0*0*00
000***000

In above star-zero nested pattern, there are 4 loops(each loop have different color), so before you making above pyramid, you should make first individual each loop, after making all loop, join them.
That's done.

/*c program for star-zero nested pattern pyramid*/
#include<stdio.h>
int main()
{

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

/**************************************************************
The output of above program would be
***************************************************************/


Output of Star-Zero Nested Pattern C program
Figure: Screen-shot for Star-Zero Nested Pattern C program

Thursday, September 12, 2013

Even Number Pattern

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

 2
 2 4
 2 4 6
 2 4 6 8

Ans.

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

 int r,c,p;
 for(r=1; r<=4; r++)
 {
   for(c=1,p=2; c<=r; c++,p=p+2)
     printf(" %d",p);
   printf("\n");
 }
 getch();
 return 0;
}

/************************************************************
The output of above program would be:
*************************************************************/

Output of even number pattern C program
Figure: Screen shot for even number pattern C program

Friday, September 6, 2013

Number Pyramid Structure

Q. Write a C program to print the following number pyramid structure design as:

1 2 3 4 5
2 3 4 5
3 4 5
4 5
5

Ans.

/*c program for number pyramid pattern design*/
#include<stdio.h>
int main()
{

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

/*************************************************************
The output of number pyramid pattern design program would be:
**************************************************************/


Output of Number Pyramid Pattern C program
Figure: Screen-shot for number pyramid pattern C program


Positive-Negative Number Triangle

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

9
8  6
7  5  3
4  2  0 -2
1 -1 -3 -5 -7

Ans.

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

 int num,n,r,c;
 for(r=1,num=9; r<=5; r++,num--)
 {
   if(r==4)
   {
     for(c=1,n=4; c<=r; c++,n=n-2)
        printf(" %d",n);
   }
   else if(r==5)
   {
     for(c=1,n=1; c<=r; c++,n=n-2)
        printf(" %d",n);
   }
   else
   {
     for(c=1,n=num; c<=r; c++,n=n-2)
        printf(" %d",n);
   }
   printf("\n");
 }
 getch();
 return 0;
}

/***************************************************************
The output of above positive-negative number triangle C program would be:
****************************************************************/

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

Thursday, September 5, 2013

Equilateral Triangle Number Design

Q. Write a C program to print the following equilateral triangle number design as:

     5
    454
   34543
  2345432
 123454321

Ans.

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

 int num,r,c,sp,n;
 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);
  for(c=1,n=num-1; c<r; c++,n--)
     printf("%d",n);
  printf("\n");
 }
 getch();
 return 0;
}

The output of above program would be:


Output of Equilateral Triangle design C program
Figure: Screen shot for Equilateral Triangle design C program


Z-Shape Pyramid

Q. Write a C program to print the Z-Shape pyramid as:

******
    *
   *
  *
 *
******

Ans.

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

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

The output of above program would be:

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

Number Triangle

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

     1
    21
   321
  4321
 54321

Ans.

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

 int num,r,c,sp;
 printf("Enter no of rows : ");
 scanf("%d", &num);
 for(r=1; r<=num; r++)
 {
  for(sp=num-r; sp>=1; sp--)
     printf(" ");
  for(c=r; c>=1; c--)
     printf("%d",c);
  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

Sunday, September 1, 2013

Increased Number Triangle

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

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

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

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

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