Monday, 29 October 2012

Number Pyramid

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

54321
5432
543
54
5

Ans.

/*c program for number pyramid*/
#include<stdio.h>
int main()
{
 int num,r,c;
 printf("Enter any number: ");
 scanf("%d", &num);
 for(r=1; r<=num; r++)
 {
  for(c=num; c>=r; c--)
     printf("%d",c);
  printf("\n");
 }
 return 0;
}

The output of above program would be:

Output of number pyramid C program
Figure: Screen shot for number pyramid C program

Monday, 22 October 2012

Call by reference swapping flowchart

Q. Write a C program to give a example of swapping two values using call by reference method.
Also draw appropriate flowchart.

Ans.

C program for  call by reference swap method, click below link:
click me for call by reference swap C program

Call by reference swapping flowchart as:

Flowchart for swap two values using "Call By Reference"
Figure: Flowchart for call by reference swap two values program

Odd Number Series Pyramid

Q. Write a C program to print the odd number series pyramid.
or
Q. Write a C program to print the following number pyramid as:

1
3  5  7
9 11  13  15  17  19

Ans.

/*c program for odd number pyramid*/
#include<stdio.h>
int main()
{
 int n=2,r,c,z=3;
 printf("1\n");
 for(r=1; r<=2; r++)
 {
  for(c=1; c<=r*3; c++,z=z+2)
     printf("%d ",z);
  printf("\n");
 }
 return 0;
}

The output of above program would be:


Output of odd number series pyramid C program
Figure: Screen shot for odd number series
pyramid C program

Saturday, 20 October 2012

Reverse Star Pyramid

Q. Write a C program to print the following star structure:

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

Ans.

/*C program for reverse star pyramid*/
#include<stdio.h>
#include<conio.h>
int main()
{
 int num,c,sp,r=1;
 printf("Enter loop repeat number(rows): ");
 scanf("%d", &num);
 printf("\n");
 for(; num>=1; num--,r++)
 {
  for(c=1; c<=num; c++)
    printf("*");
  for(sp=r; sp>1; sp--)
    printf(" ");
  for(sp=r; sp>1; sp--)
    printf(" ");
  for(c=num; c>=1; c--)
    printf("*");
  printf("\n");
 }
 return 0;
}


The output of above program would be:

Output of star reverse pyramid C program
Figure: Screen shot for reverse star pyramid C program

Odd Loop

What is odd loop?
In real life programming, there are many times comes a situation when we don't know how many times the statements in the loop are to be executed.
There is comes concept of odd loop.
Execution of loop an unknown number of times can be done by - while, for and do...while loops.

/*A demonstration of odd loop using do...while*/
#include<stdio.h>
int main()
{
 int n;
 char answer;
 do
 {
  printf("Enter any number : ");
  scanf("%d", &n);
  printf("Square of %d is %d",n,n*n);
  fflush(stdin);
  printf("\nWant to calculate more square y/n: ");
  scanf("%c", &answer);
 }while(answer=='y');
 return 0;
}

The output of above program would be:
Output of odd loop ( do..while) square C program
Figure: Screen shot of odd loop (do...while) to calculate
square of number C program 



The above odd loop program we can write using for loop as:

/*odd loop using for loop of calculate square number C program*/

#include<stdio.h>
int main()
{
 int n;
 char ans='y';
 for(; ans=='y' ; )
 {
  printf("Enter any number : ");
  scanf("%d"&n);
  printf("Square of %d is %d",n,n*n);
  fflush(stdin);
  printf("\nWant to calculate more square y/n: ");
  scanf("%c"&ans);
 }
 return 0;
}

The output of above program would be:
Output of odd loop ( for ) square C program
Figure: Screen shot of odd loop (for) to calculate 
square of number C program 



The odd loop program we can write using while loop as:
/*odd loop using while loop of calculate square number C program*/

#include<stdio.h>
int main()
{
 int n;
 char ans='y';
 while(ans=='y')
 {
  printf("Enter any number : ");
  scanf("%d"&n);
  printf("Square of %d is %d",n,n*n);
  fflush(stdin);
  printf("\nWant to calculate more square y/n: ");
  scanf("%c"&ans);
 }
 return 0;
}



The output of above program would be:
Output of odd loop ( while ) square C program
Figure: Screen shot of odd loop ( while ) to calculate 
square of number C program 

Friday, 19 October 2012

Get Student Marks and Calculate Percentage & Division

Q. The marks obtained by a student in 5 different subjects are input through the keyboard.
The student gets a division as per the following rules:

Percentage above or equal to 60- 1st division
Percentage between 50 and 59- 2nd division
Percentage between 40 and 49- 3rd division
Percentage less then 40- Fail

Write a C program to calculate the division obtained by the student.

Ans.

/*c program for read 5 subject marks and calculate percentage & division of student*/
/*we assume total marks is 500*/
#include<stdio.h>
int main()
{
 float m1,m2,m3,m4,m5,avg,per; //m=marks
 printf("Enter 5 subject marks: ");
 scanf("%f %f %f %f %f",&m1,&m2,&m3,&m4,&m5);
 per = (m1+m2+m3+m4+m5)*100/500;
 printf("Student get %0.2f percentage. \n",per);
 if(per>=60)
   printf("1st Division");
 else if(per>=50 && per<=59)
   printf("2nd Division");
 else if(per>=40 && per<=49)
   printf("3rd Division");
 else if(per<40)
   printf("Fail");
 return 0;
}

The output of above program would be:


Output for calculate student percentage and division of C program
Figure: Screen shot for read student marks and calculate
percentage and find division C program

Related programs:

  1. Find student grade through structure

Star pyramid pattern

Q. Write a C program to print the following star pattern.
or
Q. Write a C program to print the following star structure.

*
***
******

Ans.

/*c program for print the specific star structure*/
#include<stdio.h>
int main()
{
 int n=2,r,c;
 printf("*\n");
 for(r=1; r<=2; r++)
 {
  for(c=1; c<=r*3; c++)
     printf("*");
  printf("\n");
 }
 return 0;
}

The output of above program would be:

Output of star pattern pyramid C program
Figure: Screen-shot for star triangle/pyramid C program