INDEX

10.29.2012

Q. Write a C program to print the following number pyramid:543215432543545Ans./*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:

10.20.2012

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:

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*/

10.19.2012

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 divisionPercentage between 50 and 59- 2nd divisionPercentage between 40 and 49- 3rd divisionPercentage less then 40- FailWrite 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:

10.17.2012

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:

Library function or In-built function

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 1I am in fun 2I am in fun 3

10.16.2012

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:

Prototype or Declaration

Calling

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.