Monday, October 29, 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, October 22, 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, October 20, 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, October 19, 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

Tuesday, October 16, 2012

What is Function

What is function in C?
A function is a self-contained block of statement that perform a coherent task of some kind.
There are two types of function in C:
  1. Library function or In-built function
  2. User define function

Structure of function in C program

/* demonstration of function status in program */  

#include<stdio.h>                                  
void disp();   /* function prototype declaration */
int main()                                         
{                                                  
  disp();      /*function calling*/                
  printf("C is Awesome!!!");                       
  return 0;                                        
}                                                  
void disp()    /* function definition */           
{                                                  
  printf("Hi, ");                                  
}                                                  

The output of above program would be:

Hi, C is Awesome!!!

Let's grab some useful info from the above program:
In above program we have 2 function:
main()
disp()

Here you can see and thought:
1. why main() is used only once in program?
2. why disp() is used three times in program?

The answer of first question is "if a c program contains only one function then it must be main()." i.e. every C program must have main() function.

The answer of second question: let's understand the meaning of every disp() in program:

void disp();
this is the first disp() that used in program. It is function prototype.
This prototype tells the compiler that as:
What is function type ( i.e. void or not ),
What is name of function( i.e. in our program function name is disp)
Both parenthesis () indicate that disp is function.
keep in mind: void word indicating that "nothing return".

disp();
this is the second disp() that used in program. It is function calling.
Every calling function main work is as:
when compiler reached at calling function disp(); program control passes to the function disp(), (i.e. activity of main() is temporary suspend ), when disp() function runs out all his statement execute, the control returns to the main() function and begins executing code at the exact point where it is left off.


void disp()
{
  printf("Hi, ");
}

this is the third disp() that used in program. It is function definition.
In function definition we can used all C language keywords, operators, variable etc.
In above program recently we have only 1 printf(); statement.

Let's exercise, what we learn:

/*exercise of function calling C program*/
#include<stdio.h>
void fun1();
void fun2();
void fun3();
int main()
{
  printf("\nI am in main function!!");
  fun1();
  fun2();
  fun3();
  return 0;
}
void fun1()
{
  printf("\nI am in fun 1");
}


void fun2()
{
  printf("\nI am in fun 2");
}


void fun3()
{
  printf("\nI am in fun 3");
}


The output of above program would be:

I am in main function!!
I am in fun 1
I am in fun 2
I am in fun 3


User Define Function

What is User Define Function?
A function that is declare, calling and define by the user is called user define function.
Every user define function has three parts as:

  1. Prototype or Declaration
  2. Calling
  3. Definition
1. Prototype or Declaration

Prototype/declaration set the outline of function like as:
    function is something returning or not?
    what is function name?
    what would be the nature of parameter?
    how much parameters used in function?
This is generally written at top of program like as after or before the main() function.

syntax:
return_data_type function_name(data_type parameter, ......);

Description of syntax:

In above syntax "return_data_type" may be two types:
if return_data_type is void: This mean that our function nothing return.
if return_data_type is other like as int, float, double etc. then it would be return as int, float, double etc. respectively.
for example:
int factorial(int num); //it would return a integer value.
void display(int num); //it would nothing return.

In above syntax "function_name" means that the name of function which we want to make.
for example:
int myfunction(int i);

In above syntax "data_type parameter" means that first write down the data type( int, float, double, long, char ) and after write down the name of related parameter/argument/variable. It is may be one, two and so on as the requirement of function.
for example:
int sum(int x, int y);
int myfunction(int name);

In Prototype or Declaration of function, we can write only the name of data type, not require the name of parameter/argument( it is optional ).
for example:
int sum(int , int );  //it would return a integer value.
int myfunction(int );  // this is valid and correct
int myfunction(char ); // this is valid and correct


2. Calling

The calling part of function work as:
    It is call the function.
    It forward the control of main() to user define function and vice versa.
The calling is generally written as middle of program after grab all related argument.

syntax:
//if return type is void:
function_name(actual argument/parameter);

//if return type is not void:
return_parameter=function_name(actual argument/parameter);


for example:
display(num);   //void calling function- display
result=sum(x, y)  //return calling function-sum
f=factorial(num)  //return calling function- factorial
ex=myfunction(number)  //return calling function - myfunction

3.  Definition

The "definition" part is the heart of function.
    What will function do? It define the all function body.
The definition part must be exist at outside of main() function. i.e. it is written after making all the program.

syntax:

//if return type is void:
function_name(data_type argument/parameter)
{
   statement 1;
   statement 1;
   statement 1;
          .
          .
          .
   statement n;
}

//if return type is not void:
return_data_type function_name(data_type parameter)
{
   statement 1;
   statement 1;
   statement 1;
          .
          .
          .
   statement n;
   return ( any_return_value);
}

example:
int sum(int x, int y)
{
  int res;
  res = x+y;
  return(res);
}

A complete example of user define function:

/*demonstration of user define function-sum*/
#include<stdio.h>
int sum(intint);  //Prototype or Declaration
int main()
{
  int x,y,result;
  printf("Enter value of x and y: ");
  scanf("%d %d", &x, &y);
  result=sum(x, y);  //calling of function-sum
  printf("Sum of %d + %d = %d",x,y,result);
  return 0;
}

//Definition of function sum
int sum(int a, int b)
{
  int res;
  res = a + b;
  return ( res );
}

The output of above program would be:

Output of user define function sum C program
Figure: Screen shot for user define function
sum C program

Saturday, October 13, 2012

Star triangle frame pyramid

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

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

Ans.

/*c program for star triangle frame pyramid*/
#include<stdio.h>
int main()
{
 int num,r,j,sp;
 printf("Enter no of rows: ");
 scanf("%d", &num);
 printf("\n*\n");
 for(r=1; r<=num; r++)
 {
  printf("*");
  for(sp=1; sp<r; sp++)
    printf(" ");
  printf("*\n");
 }
 for(j=1; j<=num+2; j++)
   printf("*");
 return 0;
}

The output of above program would be:


Output of star triangle frame pyramid C program
Figure: Screen shot for star triangle frame pyramid C program

Convert Inch to Feet and Feet to Inch

Q. Write a C program to convert Inch to Feet and Feet to Inch.

Ans.

keep in mind:

1 Foot  = 12 Inch
1 Inch  = 0.083 Foot

/*c program for convert inch to feet and vice-versa*/
#include<stdio.h>
int main()
{
 int ch;
 double foot,inch;
 printf("\nEnter 1 for convert Foot to Inch.");
 printf("\nEnter 2 for convert Inch to Foot.");
 printf("\nEnter 0 for exit.");
 printf("\n\nEnter your choice : ");
 scanf("%d", &ch);
 switch(ch)
 {
  case 1:
    printf("\nEnter value in Foot: ");
    scanf("%lf", &foot);
    inch = 12 * foot;
    printf("\n\t-- Convert Foot to Inch --\n");
    printf("\n%lf foot = %lf Inch",foot,inch);
    break;
  case 2:
    printf("\nEnter value in Inch: ");
    scanf("%lf", &inch);
    foot = (0.083) * inch;
    printf("\n\t-- Convert Inch to Foot --\n");
    printf("\n%lf Inch = %lf Foot",inch,foot);
    break;
  case 0:
    goto exit;
  default:
    printf("\nYou enter invalid options.");
 }
 exit:
 return 0;
}

The output of above program would be:


Output of convert Feet to Inch C program
Figure: Screen shot for convert Feet to Inch C program


Output of convert Inch to Feet C program
Figure: Screen shot for convert Inch to Feet C program

Convert Meter to Inch and Inch to Meter

Q. Write a C program to convert Meter to Inch and Inch to Meter.

Ans.

keep in mind:

1 Meter = 39.37 Inch
1 Inch  = 0.025 Meter

/*c program for convert meter to inch and vice-versa*/
#include<stdio.h>
int main()
{
 int ch;
 double meter,inch;
 printf("\nEnter 1 for convert Meter to Inch.");
 printf("\nEnter 2 for convert Inch to Meter.");
 printf("\nEnter 0 for exit.");
 printf("\n\nEnter your choice : ");
 scanf("%d", &ch);
 switch(ch)
 {
  case 1:
    printf("\nEnter value in Meter: ");
    scanf("%lf", &meter);
    inch = (39.37) * meter;
    printf("\n\t-- Convert Meter to Inch --\n");
    printf("\n%lf meter = %lf Inch",meter,inch);
    break;
  case 2:
    printf("\nEnter value in Inch: ");
    scanf("%lf", &inch);
    meter = (.025) * inch;
    printf("\n\t-- Convert Inch to meter --\n");
    printf("\n%lf Inch = %lf Meter",inch,meter);
    break;
  case 0:
    goto exit;
  default:
    printf("\nYou enter invalid options.");
 }
 exit:
 return 0;
}


The output of above program would be:


Output of convert Meter to Inch C program
Figure: Screen shot for
convert Meter to Inch C program


Output of convert Inch to Meter C program
Figure: Screen shot for 
convert Inch to Meter C program