Solved C Programs

48
Programming in C /* Program to wish hello to user hello.c */ # include<stdio.h> // including header file main() { char name[10]; // variable declaration of character type for accepting string value of max clrscr(); // function to clear the output screen printf("Enter your name:"); scanf("%s", name); // accepting input from user printf("\n Hell o %s welcome to the world of C", name); //printing mess age getch(); // function for the quick output screen } /* Program to find out the area of a circle circle.c */ # include<stdio.h> // including header file const float pi= 3.14; main() { float area=1.0 , radius=1.0; // variable area is initially 1.0 clrscr(); // function to clear the output screen printf("Enter radius"); scanf("%f", &radius ); // accepting radius from user area= pi*radius* radius; // calculation for area printf("\n The area of circle is %f", area); // Printing result getch(); // function for the quick output screen } /* Program to convert Fahrenheit temperature to Celsius. tempconv.c */ # include<stdio.h> // including header file main() { float ftemp=1.0 , celtemp=1.0 ; // variables are initially 1.0 clrscr(); // function to clear the output screen printf("Enter temperature in Fahrenheit:"); scanf("%f", &ftemp ); // accepting temperature in Fahrenheit celtemp= (ftemp-32 )/1.8; // Conversion formula printf("\n The temperature in Celsius is %f", celtemp); // Printing result getch(); // function for the quick output screen } /* Program to accept 3 nos. add them and find out average. avg.c */ # include<stdio.h> // including header file main() { int no1=0, no2=0,no3=0, total=0; // variables declaration and initialization float average=1.0; clrscr(); // function to clear the output screen printf("Enter 3 numbers:"); scanf("%d %d %d", &no1, &n o2,&no3 ); // accepting 3 numbers us ing si ngle sc anf. total=no1+no2+no3 average=total/3; // Calculating average printf("\n The total is %d and average is ", total, average); // Printing result getch(); // function for the quick output screen } D. H. B. Soni College, Solap ur D. H. B. Soni College, Solapur - 1 1 - -

Transcript of Solved C Programs

8/8/2019 Solved C Programs

http://slidepdf.com/reader/full/solved-c-programs 1/48

Programming in C

/* Program to wish hello to user hello.c */# include<stdio.h> // including header file

main(){char name[10]; // variable declaration of character type for accepting string value of maxclrscr(); // function to clear the output screen

printf("Enter your name:");scanf("%s", name); // accepting input from userprintf("\n Hello %s welcome to the world of C", name); //printing message

getch(); // function for the quick output screen}

/* Program to find out the area of a circle circle.c */# include<stdio.h> // including header fileconst float pi= 3.14;main(){

float area=1.0 , radius=1.0; // variable area is initially 1.0clrscr(); // function to clear the output screenprintf("Enter radius");

scanf("%f", &radius ); // accepting radius from userarea= pi*radius* radius; // calculation for area

printf("\n The area of circle is %f", area); // Printing resultgetch(); // function for the quick output screen}

/* Program to convert Fahrenheit temperature to Celsius. tempconv.c */# include<stdio.h> // including header file

main(){float ftemp=1.0 , celtemp=1.0 ; // variables are initially 1.0

clrscr(); // function to clear the output screenprintf("Enter temperature in Fahrenheit:");

scanf("%f", &ftemp ); // accepting temperature in Fahrenheitceltemp= (ftemp-32 )/1.8; // Conversion formulaprintf("\n The temperature in Celsius is %f", celtemp); // Printing resultgetch(); // function for the quick output screen}

/* Program to accept 3 nos. add them and find out average. avg.c */# include<stdio.h> // including header filemain()

{int no1=0, no2=0,no3=0, total=0; // variables declaration and initializationfloat average=1.0;clrscr(); // function to clear the output screenprintf("Enter 3 numbers:");scanf("%d %d %d", &no1, &no2,&no3 ); // accepting 3 numbers using single scanf.total=no1+no2+no3average=total/3; // Calculating average

printf("\n The total is %d and average is ", total, average); // Printing result

getch(); // function for the quick output screen}

D. H. B. Soni College, Solapur D. H. B. Soni College, Solapur  -- 11 --

8/8/2019 Solved C Programs

http://slidepdf.com/reader/full/solved-c-programs 2/48

Programming in C

// Program to calculate the interest amount when interest is 11 % per year. intcal.c# include<stdio.h> // including header file

# define int_rate 10main(){float principle=1.0 , int_amt=1.0 ; // variables are initially 1.0int period=1;

clrscr(); // function to clear the output screenprintf("Enter Principle amount and period(years)");scanf("%f %d ", &principle,& period ); // accepting principle and period

int_amt= (principle * int_rate/100)* period; // formulaprintf("\n The Interest amount is %f", int_amt); // Printing result

getch(); // function for the quick output screen}

/* Calculate total sales and & commission for the sales man as 7% of sales Sales.c*/# include<stdio.h>const int comm=7;

main(){float tot_sale=1.0, comm_amt=1.0, rate=1.0;

int qty=1;clrscr();

printf("Enter quantity and item rate:");scanf("%d %f ", &qty, &rate);tot_sale=qty*rate;comm_amt=tot_sale* comm/100;printf("\n Items=%d \t rate=%f \t Total sales=%f commission =%f", qty, rate,tot_sale,comm_amt);getch();

}

/* Program to find out the area and perimeter of rectangle. rectangle.c */

# include<stdio.h>void main()

{float length=1.0, breadth=1.0;float area=1.0, perim=0.0;printf("Enter length and breadth");scanf("%f %f", &length, &breadth);area=length*breadth;

perim=(2*length) +(2*breadth);

printf("\n Area of rectangle is %f and perimeter is %f", area, perim);getch();}

/* Program to find out the greater of 2 numbers.. max.c */# include<stdio.h>void main(){int no1=0, no2=0, max=0;printf("Enter two numbers");scanf("%d %d", &no1, &no2);

max= (no1>no2?no1:no2); // use of ternary operator ‘?:’ to find out the greater

number.printf("\n The greater number is %d", max);getch();

}

D. H. B. Soni College, Solapur D. H. B. Soni College, Solapur  -- 22 --

8/8/2019 Solved C Programs

http://slidepdf.com/reader/full/solved-c-programs 3/48

Programming in C

/* Program to calculate employee salary salcal.c */

# include<stdio.h>void main(){int basic=1;float hra=1.0, da=1.0, ta=1.0, gsal=0.0, netsal=0.0,pt=1.0;

clrscr();printf("\n Enter basic salary");scanf("%d", &basic); // Accepting basic salary from user.

hra=(50*basic)/100; // calculating hrada=(25*basic)/100; //calculating da

ta=(5*basic)/100; //calculating tagsal=basic +hra +da +ta; // Gross salary calculationnetsal=gsal-pt; // Calculating net salaryprintf("\n HRA=%f \t DA=%f \t TA=%f ", hra, da, ta); //Display result.printf("\n Gross salary=%f\t Net Salary= %f", gsal, netsal); //Display result.getch();}

/* Program to convert rupees into paisa convertl.c */# include<stdio.h>

void main(){

float rupees=1.0;double paisa=1.0;clrscr();printf("\n Enter rupees: ");scanf("%f", &rupees);paisa=rupees*100;printf("\n Rupees=%f and Paisa=%lf", rupees, paisa ); //Display result.

getch();}

// program to display hexadecimal, decimal and octal formats of entered number.# include<stdio.h>

void main(){int num=0;clrscr();printf("Enter number:");scanf("%d", &num);

printf("\nHexadecimal number=%x \t Octal number=%o\t Decimal number=

%d",num,num,num);getch();}

/* To display the entered number with trailing zeros and leading zeros prnzero.c*/# include<stdio.h>void main(){long int num=0;clrscr();

printf("Enter number:");

scanf("%d",&num);printf("\n %05d",num);printf("\n %5d",num*1000);

getch();

D. H. B. Soni College, Solapur D. H. B. Soni College, Solapur  -- 33 --

8/8/2019 Solved C Programs

http://slidepdf.com/reader/full/solved-c-programs 4/48

Programming in C

}

/* To display entered numbers with right justification and left justification justify.c */# include<stdio.h>void main(){int num=0;printf("Enter number");scanf("%d", &num);printf("The number(R): %d",num);

printf("\nThe number(L):%-d",num);getch();

}

/* To demonstrate all possible format specefiers formspec.c */# include<stdio.h>void main(){int ac_no;float int_amt=1.0;float interest=1;char ac_type;

char name[20];float deposit=1.0;

clrscr();printf("\n Enter account number & members name");scanf("%d %s",&ac_no,name);printf("Enter Deposit and rate of interest");scanf("%f %f", &deposit, &interest);int_amt=(deposit*interest)/100;printf("A/C No:%d \t Name-%s \t Deposit-%f\t Interest=%f ",ac_no, name, deposit,int_amt);

getch();}

/* Comparison of two numbers. comp2.c */# include<stdio.h>

void main(){int num1=0, num2=0;printf("Enter two numbers:");scanf("%d %d", &num1, &num2);if(num1>num2) //Checking condition if num1>num2{

printf("\n The greater number is: %d", num1); //display number num1 is greater (TRUE)}

else

{printf("\n The greater number is: %d", num2); //display number num2 is greater(FALSE)

}getch();}

/* Program to find whether number is even or odd. evenodd.c */# include<stdio.h>void main()

{int num=0;clrscr();

printf("Enter number:");

D. H. B. Soni College, Solapur D. H. B. Soni College, Solapur  -- 44 --

8/8/2019 Solved C Programs

http://slidepdf.com/reader/full/solved-c-programs 5/48

Programming in C

scanf("%d", &num);if(num%2==0) // if num%2 is zero then number is divisible by 2 so it is

even{printf("\n %d is even number", num);}else

{printf("\n %d is odd number " ,num); // num%2 is non zero then number is odd}

getch();}

/* Program to find whether number is positive or negative checknum.c */

# include<stdio.h>void main(){signed int num=0;clrscr();

printf("Enter number:");scanf("%d", &num);if(num>0) // if num>0 then it is positive

{printf("\n %d is positive number", num);

}else{printf("\n %d is negative number", num); // if num < 0 then it is negative}getch();}

/* Accept age and display the person is adult or not. age.c */# include<stdio.h>

void main(){

int age=0;clrscr();printf("Enter age :");scanf("%d", &age);if(age>=18) // if age is > or = 18 then person is adult{

printf("\n The person is adult");

}else{

printf("\n The person is not adult"); // if num < 18 then it is negative}getch();}

/* Accept character form user and check whether it is vowel or consonant. char.c*/# include<stdio.h>

void main()

{char code;clrscr();

printf("Enter a single character:");

D. H. B. Soni College, Solapur D. H. B. Soni College, Solapur  -- 55 --

8/8/2019 Solved C Programs

http://slidepdf.com/reader/full/solved-c-programs 6/48

Programming in C

scanf("%c", &code);if(code=='a' || code=='e' || code=='i' || code=='o' || code=='u')// Checking for vowel

{printf("\n The %c is vowel", code);}else{

printf("\n The %c is consonant", code); // if not vowel then consonant}getch();

}

/* Accept first character of designation form user and display the post or designation.desig.c */

# include<stdio.h>void main(){char code;clrscr();

printf("\n\t Designation\t character");printf("\n\t Manager\t -m");printf("\n\t supervisor\t-s");

printf("\n\t Clerk\t -c");printf("\n\t Worker\t -w");

printf("\n Enter designation code:" );scanf("%c", &code);if (code=='m') // checking for manager designation{printf("\n The designation is manager");}else if (code=='s') // else checking for supervisor designation

{printf("\n The designation is supervisor");}

else if (code=='c') // else checking for clerk designation{printf("\n The designation is clerk");}else if (code=='w') // else checking for worker designation{printf("\n The designation is worker");}

else //else the character entered is invalid

{printf("\n Invalid character entered");}

getch();}

/* display menu for operation and accept choice number from user dispmenu.c */# include<stdio.h>void main(){int num=0, choice=0;

clrscr();

printf("\n\t 1.Square");printf("\n\t 2. Cube");printf("\n\t 3. Octal");printf("\n\t 4.Hexadecimal");

D. H. B. Soni College, Solapur D. H. B. Soni College, Solapur  -- 66 --

8/8/2019 Solved C Programs

http://slidepdf.com/reader/full/solved-c-programs 7/48

Programming in C

printf("\n Enter number 1/2/3/4 for your choice");scanf("%d", &choice);

printf("\n Enter number:" );scanf("%d", &num);if (choice==1) // checking for square of number operation{printf("\n The square of a number is %d", num*num);

}else if (choice==2) // else checking for cube of a number operation{

printf("\n The cube is %d", num*num*num);}

else if (choice==3) // else checking for printing equivalent octalnumber{printf("\n Equivalent octal of %d is %o",num ,num);}else if (choice==4) // else checking for printing equivalent hexadecimalnumber

{printf("\n Equivalent hexadecimal of %d is %x ",num, num);}

else //else the character entered is invalid{

printf("\n Choice is Invalid ");}getch();}

/* Accept 3 numbers from user and find out the greatest number gcomp3.c */

# include<stdio.h>void main(){

int n1=0, n2=0,n3=0;clrscr();printf("\n Enter 3 numbers to compare");scanf("%d %d %d", &n1,&n2,&n3);

if (n1>=n2 && n1>=n3) //checking number1 with number 2 and number3.{printf("\n The greatest number is %d",n1);

}

else if (n2>=n1 && n2>=n3) //checking number2 with number 1 and number3{printf("\n The greatest number is %d",n2);

}else if (n3>=n1 && n3>=n2 ) //checking number3 with number 1 and number2{printf("\n The greatest number is %d",n3);}else

{printf("\n All numbers are equal");

}

getch();}

D. H. B. Soni College, Solapur D. H. B. Soni College, Solapur  -- 77 --

8/8/2019 Solved C Programs

http://slidepdf.com/reader/full/solved-c-programs 8/48

Programming in C

/* Accept 3 numbers from user and find out the smallest number scomp3.c */# include<stdio.h>

void main(){int n1=0, n2=0,n3=0;clrscr();printf("\n Enter 3 numbers to compare");

scanf("%d %d %d", &n1,&n2,&n3);

if (n1<=n2 && n1<=n3) //checking number1 with number 2 and number3.

{printf("\n The smallest number is %d",n1);

}else if (n2<=n1 && n2<=n3) //checking number2 with number 1 and number3{printf("\n The smallest number is %d",n2);}else if (n3<=n1 && n3<=n2 ) //checking number3 with number 1 and number2{

printf("\n The smallest number is %d",n3);}else

{printf("\n All numbers are equal");

}getch();}

/* Accept marks in percentage from user and display result. result.c */# include<stdio.h>void main()

{int marks;clrscr();

printf("\n Enter marks(percentage)");scanf("%d", &marks);if (marks>0 && marks<50) //checking fail condition{printf("\n The candidate is fail");} //checking pass class conditionelse if (marks>=50 && marks<=54){

printf("\n The candidate is passed in pass class");

}else if(marks>=55 && marks<=59) //checking second class condition{

printf("\n The candidate is passed in second class");}else if (marks>=60 && marks<=69) //checking First class condition{printf("\n The candidate is passed in first class");}

else if (marks>=70 && marks<=79) //checking distinction condition{

printf("\n The candidate is passed in distinction");

}else if (marks>=80 && marks<=100) //checking Merit condition{printf("\n The candidate is passed in Merit");}

D. H. B. Soni College, Solapur D. H. B. Soni College, Solapur  -- 88 --

8/8/2019 Solved C Programs

http://slidepdf.com/reader/full/solved-c-programs 9/48

Programming in C

else{

printf("\n Entered marks are invalid"); //checking for wrong input

}getch();}

/* calculation of income tax. it.c */

# include<stdio.h>void main()

{char code;

long int income=1;clrscr();printf("\n Enter Gender m/f: ");scanf("%c", & code);printf("\n Enter annual income");

scanf("%ld", &income);if (code=='f') //If the person is female{

if(income>135000) //Checking for taxable income{

printf("%d is the income tax on %d income for female",((income-13500)*10/100));}else //Tax is not applicable{printf("No income tax for female " );}}

else //else of outer most if {if(code=='m') //If the person is male

{if (income>=100000) //Checking for taxable income{printf("%d is the income tax on %d income for male" ,((income-10000) *10/100));}else //Tax is not applicable{printf("No income tax for Male " );

} //closing of else

} // closing of if } //closing of outer elsegetch();

}

/* Using switch checking vowel switchvow.c */# include<stdio.h>void main(){char code;

clrscr();

printf("\n Enter character:");scanf("%c", &code);switch(code) // passing code to check the value{

D. H. B. Soni College, Solapur D. H. B. Soni College, Solapur  -- 99 --

8/8/2019 Solved C Programs

http://slidepdf.com/reader/full/solved-c-programs 10/48

Programming in C

case 'a':case 'i':

case 'o':case 'u':printf("%c is vowel", code);break;default:

printf("The %c is not vowel", code);break;}

getch();}

/* displaying menu to do arithmetical operations arithop.c */# include<stdio.h>void main(){int no1=0,no2=0, choice=0;

clrscr();printf("\n 1.Addition");printf("\n 2.Subtraction");

printf("\n 3.Multiplication");printf("\n 4.Division");

printf("Enter two numbers:");scanf("%d %d", &no1, &no2);printf("\n Enter choice");scanf("%d", &choice);switch (choice) // passing choice to check its values{case 1: // Checks if 1 is entered as a choice

printf("\n Addition is %d", no1+no2);break; // case will get break if executedcase 2:

printf("\n Subtraction is %d", no1-no2);break;case 3:printf("\n Multiplication is %d", no1*no2);break;case 4:printf("\n Division is %d", no1/no2);break;

default:

printf("\n Invalid value entered"); // if value other than 1,2,3 or 4 is enteredbreak;}

getch();}

/* Program to accept the type of travel and display fare for that type. Fare.c*/# include<stdio.h>main(){

char type;

printf("\n\t Traveling details:");printf("\n Bus (b)");printf("\n Train(t)");printf("\n Private car(c)");

D. H. B. Soni College, Solapur D. H. B. Soni College, Solapur  -- 1010 --

8/8/2019 Solved C Programs

http://slidepdf.com/reader/full/solved-c-programs 11/48

Programming in C

printf("\n By air(a)");printf("\n Enter choice b/t/c/a");

scanf("%c", &type);switch(type) // Accepted value is passed to switch statement{case 'b':printf("\n Bus fare is 450 Rs");

break;case 't':printf("\n Train fare is 375 Rs");

break;case 'c':

printf("\n Private car fare is 800 Rs");break;case 'a':printf("\n Bus fair is 1200 Rs");break;default: // If choice is entered other than the given casesprintf("\n Invalid choice");

break;}getch();

}

/* Program to display number from 1 to 10 and display its spelling spell.c */# include<stdio.h>void main(){int no=0;clrscr();

printf("Enter number 1-10");scanf("%d", &no);switch(no)

{case 1:printf("\n One");break;case 2:printf("\n Two");break;case 3:

printf("\n Three");

break;case 4:printf("\n Four");

break;case 5:printf("\n Five");break;case 6:printf("\n Six");

break;case 7:

printf("\n Seven");

break;case 8:printf("\n Eight");break;case 9:

D. H. B. Soni College, Solapur D. H. B. Soni College, Solapur  -- 1111 --

8/8/2019 Solved C Programs

http://slidepdf.com/reader/full/solved-c-programs 12/48

Programming in C

printf("\n Nine");break;

case 10:printf("\n Ten");break;default:printf("\n Invalid number");

break;}getch();

}

/* print user’s name 5 times using while whileprn.c */# include<stdio.h>

void main(){int i=1;char name[15];printf("\n Enter your name:");

scanf("%s",name);while (i<=5) //loop will run till value of i will become 5{

printf("\n Your name is %s", name);i++; //Incrementing value of i each time by 1

}getch();}

/* printing 1 to 10 numbers using while loop Wh1to10.c*/# include<stdio.h>

void main(){int i=1; // declaring variable i =1 as we would like to print numbers starting from 1

while (i<=10)

{printf("\n %d ",i); //printing value of ii++; // value of i will be increased by 1.}getch();}

/* printing Fibonacci using while loop wfibo.c */# include<stdio.h>

void main(){int fno=0, sno=1, tno=0,pos=0,i=1;clrscr();printf("\n Enter last position for Fibonacci");scanf("%d",&pos);printf("\n %d \t %d", fno, sno) ; // printing initial values 0 and 1while (i<=pos)

{

tno=fno+sno; // third number is first number + second numberprintf("\t %d", tno); // printing third numberfno=sno; // for next addition second number will be treated as first

number

D. H. B. Soni College, Solapur D. H. B. Soni College, Solapur  -- 1212 --

8/8/2019 Solved C Programs

http://slidepdf.com/reader/full/solved-c-programs 13/48

Programming in C

sno=tno; //recent third number will be treated as second number.i++; // i will increment by 1. when i>pos then loop gets break.

}getch();}

/* program to count number of digits and print the reverse of a number revno.c */# include<stdio.h>void main()

{int no=0, rem=0,cnt=0;

printf("Enter number:");scanf("%d", &no);

printf("\n Reverse of a number"); // Printing messagewhile (no!=0) // While number does not become zero{rem=no%10; // rem is remainder of no/10 due to this we get last digit.cnt++; // when last digit is received the counter counts it

printf("%d",rem); // printing the remainder i.e. last digit.no=no/10; // dividing the last digits to get one by digits}

printf("\n The number of digits are %d", cnt);getch();

}

/* accept 5 numbers from user and sum them, if user enters 100 exit the program */# include<stdio.h>void main(){

int no=0, i=1,sum=0;clrscr();while(i<=5) // condition for accepting five numbers i.e. till I becomes 5

{printf("\n Enter number:");

scanf("%d", &no);if (no==100) // if accepted number is 100 .{break; // the control gets break and it comes out of loop}sum=sum + no;

i++;

}printf("The sum of is %d", sum); // the final sum of all the numbers.getch();

}

/* Using while write a program to calculate factorial of a number. wfact.c */# include<stdio.h>void main(){long int no=1, fact=1 , i=1;

clrscr();

printf("\n Enter number:");scanf("%ld", &no);while (i<=no) // while i becomes the number.

{

D. H. B. Soni College, Solapur D. H. B. Soni College, Solapur  -- 1313 --

8/8/2019 Solved C Programs

http://slidepdf.com/reader/full/solved-c-programs 14/48

Programming in C

fact=fact*i; // factorial is its previous value into ii++; // i is incremented by one

}printf("\n Factorial of a number is %ld", fact); // Printing final value of factorialgetch();}

/* Accept characters from the user, if user enters ‘x ‘ stop accepting characterswchar.c */

# include<stdio.h>main()

{char code='a';

clrscr();while (code!='x'){printf("\nEnter character (to exit type x):");code= getch();

printf("\n You have entered %c", code);}printf("\nYou have exited");

getch();}

/* program for controlling flow of data using goto goto */# include<stdio.h>

main(){int no;

clrscr();accept: // label given as a acceptprintf("Enter number");

scanf("%d",&no);if (no==0) goto accept; // if number is zero then using goto it goes to accept

getch();}

/* program to print the table of number using do while dotable.c */# include<stdio.h>void main()

{int no=0;int i=1;

printf("\n Enter number:");scanf("%d", &no);do // beginning of the do loop{printf("\t%d", no*i); // action to be performedi++; // value of i is incremented to print the table of a number}while(i<=10);getch();

}

/* program to add the numbers till it becomes 100. doaddno.c */

# include<stdio.h>

D. H. B. Soni College, Solapur D. H. B. Soni College, Solapur  -- 1414 --

8/8/2019 Solved C Programs

http://slidepdf.com/reader/full/solved-c-programs 15/48

Programming in C

void main(){

int no=0;int sum=0;do // beginning of the do loop{printf("\n Enter number:");

scanf("%d", &no);sum=sum+ no;}while(sum<100); //Test condition is checked at the end of loop.

printf("The sum is %d",sum);getch();

}

/* program to print multiples of 7 from 1 to 50. do7mul.c */

# include<stdio.h>void main()

{int i=1;do // beginning of the do loop

{if (i%7==0) // finding multiple of 7

{printf("\n %d", i); // printing only multiples of 7}i++;}while(i<=50);getch();}

/* program to print array of pointer */

# include<stdio.h>void main()

{char *str[5]={"Keyboard",

"Monitor","CPU" ,"Hard disk","SMPS"

};

int i=0;clrscr();for(i=0;i<=4;i++)

{printf("\n %d element=%s",i, str[i]);}getch();}

/* program to print table of a given no. */

# include<stdio.h>

main(){int no=1, i=1; // declaring number and i as 1.

clrscr();

D. H. B. Soni College, Solapur D. H. B. Soni College, Solapur  -- 1515 --

8/8/2019 Solved C Programs

http://slidepdf.com/reader/full/solved-c-programs 16/48

Programming in C

do{

no++; // number is incrementeddo{printf("\t%d", no*i); // printing table of a numberi++;

}while (i<=10); // multiplying the number from 1 to 10i=1;printf("\n"); // printing new line

}while(no<12); //printing table of a number from 2 to 12.getch();

}

/* Accept college name and print it twenty times colprint.c */# include<stdio.h>void main(){char col_name[30];

int i;printf("\n Enter college name:");scanf("%s", col_name);

for (i=1;i<=20;i++){

printf("\n %s", col_name);}getch();}

/* printing multiple of 5 from 1 to position entered by user. formul5.c */# include<stdio.h>

void main(){int i=0, pos=0;

printf("\n Enter last position ");scanf("%d", &pos);

for(i=5;i<=pos; i+=5) // The loop starts and increment by 5 and runs till the pos{printf("\t %d",i);}getch();}

/* Display even numbers from 1 to 100. dispeven.c */# include<stdio.h>void main()

{int i=0;for(i=1;i<=100; i++){if (i%2==0){printf("\t %d",i);}

}

getch();}

/* Display even numbers from 1 to 100. sum100.c */

# include<stdio.h>

D. H. B. Soni College, Solapur D. H. B. Soni College, Solapur  -- 1616 --

8/8/2019 Solved C Programs

http://slidepdf.com/reader/full/solved-c-programs 17/48

Programming in C

void main(){

int i=0, sum=0;for(i=1;i<=100; i++){sum=sum+i;}

printf("sum of 1-100 numbers= %d",sum);getch();}

/*printing series of 2,6,12,20,30,42 using for loop. Accept position from user.fseries .c */

# include<stdio.h>void main(){int i=0, pos=0;clrscr();

printf("\n Enter last position ");scanf("%d", &pos);for(i=1;i<=pos; i++) // The loop starts with 1 and iterates till the position entered by user

{printf("\t %d",(i*i) + i);

}getch();}

/* program to check whether a given no. is +ve, -ve or zero. */# include<stdio.h>

void main(){int i=1, cnt1=0,cnt2=0, cnt3=0, no=0;

clrscr();for(i=1;i<=10;i++)

{printf("\n Enter value");scanf(" %d", &no);if (no==0){cnt1++;

}

if (no<0){cnt2++;

}if (no>0){cnt3++;}}printf("\n Positive numbers.=%d\t Negative numbers=%d\t Zeros=%d", cnt3,cnt2,cnt1);getch();

}

/* print the * pattern forstrar.c */

# include<stdio.h>

D. H. B. Soni College, Solapur D. H. B. Soni College, Solapur  -- 1717 --

8/8/2019 Solved C Programs

http://slidepdf.com/reader/full/solved-c-programs 18/48

Programming in C

void main(){

int i=0, j=0, pos=0;clrscr();printf("Enter position/ number of rows");scanf("%d", &pos);for(i=1;i<=pos ; i++)

{printf("\n");

for(j=1;j<=i;j++){

printf("*");}}getch();}

/* print the above pattern forpat1.c */

# include<stdio.h>void main(){

int i=0, j=0, pos=0;clrscr();

printf("Enter position/ number of rows");scanf("%d", &pos);for(i=1;i<=pos ; i++){printf("\n");for(j=1;j<=i;j++){

printf("%d",i );}}

getch();}

/* print the above pattern forpat2.c */# include<stdio.h>void main(){int i=0, j=0, pos=0,cnt=1;

clrscr();

printf("Enter position/ number of rows");scanf("%d", &pos);for(i=1;i<=pos ; i++)

{printf("\n");

for(j=1;j<=i;j++){printf("%d",cnt );cnt++;}

}

getch();}

/* Print the series of factorial numbers e.g. 1, 2, 6, 2 4,120, 720 …..n factser1.c */

D. H. B. Soni College, Solapur D. H. B. Soni College, Solapur  -- 1818 --

8/8/2019 Solved C Programs

http://slidepdf.com/reader/full/solved-c-programs 19/48

Programming in C

# include<stdio.h>void main()

{int i=0, j=0, pos=0;long int fact=1;clrscr();printf("Enter last position");

scanf("%d", &pos);for(i=1;i<=pos ; i++){

printf("\t");for(j=1;j<=i;j++)

{fact=fact*j;}printf("%ld" ,fact );fact=1;}getch();

}

/* program to demonstrate use of continue statementcontdemo.c */

void main(){int i=0,j=0;for(i=1;i<=2;i++){for(j=1;j<=2;j++){

if(i==j)continue;printf("\n %d %d", i,j);

}}getch();}

/* program to print the contents of the array using for loop */# include <stdio.h>

main()

{int arr[5]={10,15,78,90,100}; // Declaration and initialization of arrayclrscr();

int i=0;for {i=0;i<=4;i++) // for loop is used to count the location from 0to 4{printf(“\n %d”,arr[i]); // printing values of ith location of an array}getch();}

D. H. B. Soni College, Solapur D. H. B. Soni College, Solapur  -- 1919 --

8/8/2019 Solved C Programs

http://slidepdf.com/reader/full/solved-c-programs 20/48

Programming in C

/* program to accept 5 elements store them in an array and display result */# include<stdio.h>

main(){int number[5]; // Declaring integer arraynumber of 5 elements.int i=0; // i is used for repetition as well as for accessing locations.

clrscr();for(i=0;i<=4;i++){

printf("\n Enter number:");scanf("%d", &number[i]);

}for(i=0;i<=4;i++) // for loop to print the valuesfrom array.{printf("\n %d is stored at %d location of array", number[i], i);}getch();

}

/* program to find sum and average of array elements */

# include<stdio.h>main()

{int number[5];int i=0,sum=0;float avg=1.0;clrscr();for(i=0;i<=4;i++){

printf("\n Enter number:");scanf("%d", &number[i]);sum=sum+number[i];

}avg=sum/5; // Calculating average out of the loop.printf("\n Sum of array elements= %d", sum); // Printing sumprintf("\n Average of array elements=%f",avg); // printing averagegetch();}

/* program to check whether a given no. is +ve, -ve or zero. */

# include<stdio.h>

main(){int number[5];

int i=0,cnt1=0,cnt2=0,cnt3=0;clrscr();for(i=0;i<=4;i++){printf("\n Enter number:");scanf("%d", &number[i]);if (number[i]<0){

cnt1++;

}if (number[i]==0){cnt2++;

D. H. B. Soni College, Solapur D. H. B. Soni College, Solapur  -- 2020 --

8/8/2019 Solved C Programs

http://slidepdf.com/reader/full/solved-c-programs 21/48

Programming in C

}if (number[i]>0)

{cnt3++;}}printf("\n Number of Negative numbers= %d", cnt1);

printf("\n Number of zeros=%d", cnt2);printf("\n Number of Positive numbers= %d",cnt3);getch();

}

// Program to convert characters

# include<stdio.h>main(){char grade[5];int i=0;

for(i=0;i<=4;i++){printf("\nEnter character:");

grade[i]=getche();grade[i]=grade[i] -32;

}for (i=0;i<=4;i++){printf("\nThe converted character are %c",grade[i]);}getch();}

/* program to copy the elements of one array to another array. */

# include<stdio.h>main()

{float arr1[5]={17.2, 15.90, 17.22, 100.76, 33.77};float arr2[5]={0.0,0.0,0.0,0.0,0.0};int i=0;clrscr();printf("\n Before copy action");

printf("\n arr1\t\tarr2\n");

for (i=0;i<=4;i++){printf("\narr1[%d]=%2.2f \t arr2[%d]=%2.2f",i,arr1[i],i,arr2[i]);

}printf("\n\nAfter copying elements");printf("\n arr1\t\tarr2\n");for(i=0;i<=4;i++){arr2[i]=arr1[i];printf("\narr1[%d]=%2.2f \t arr2[%d]=%2.2f",i,arr1[i],i,arr2[i]);}

getch();

}

D. H. B. Soni College, Solapur D. H. B. Soni College, Solapur  -- 2121 --

8/8/2019 Solved C Programs

http://slidepdf.com/reader/full/solved-c-programs 22/48

Programming in C

//Program to find out smallest and greatest number from array.# include<stdio.h>

main(){int arr[5];int i=0,small=0, great=0;clrscr();

for(i=0;i<=4;i++){printf("\n Enter the numbers:");

scanf("%d", &arr[i]);}

small=arr[0];great=arr[0];for (i=0;i<=4;i++){if (arr[i]<=small){small=arr[i];

}if (arr[i]>=great){

great=arr[i];}

}printf("\n %d is the smallest number in the list",small);printf("\n %d is the greatest number in the list",great);getch();}

/*program to accept elements in two single dimension 2D array

and add them into third array */# include<stdio.h>main()

{int arr1[5], arr2[5], arr3[5];int i=0;printf("\n Enter elements for first array");for(i=0;i<=4;i++){printf("\nEnter number:");scanf("%d",&arr1[i]);

}

i=0;printf("\n Enter elements for second array");for(i=0;i<=4;i++)

{printf("\nEnter number:");scanf("%d",&arr2[i]);}i=0;printf("\n addition stored in third array");

for(i=0;i<=4;i++){

arr3[i]=arr1[i]+arr2[i];

printf("\n arr3[%d]=%d ",i, arr3[i]);}getch();}

D. H. B. Soni College, Solapur D. H. B. Soni College, Solapur  -- 2222 --

8/8/2019 Solved C Programs

http://slidepdf.com/reader/full/solved-c-programs 23/48

Programming in C

/* Sorting of an array in ascending order i.e. top to bottom */# include<stdio.h>

void main(){int arr[5];int i=0, temp=0,j=0;for(i=0;i<=4;i++)

{printf("Enter number:");scanf("%d", &arr[i]);

}temp=arr[0];

for(j=0;j<=4;j++){for(i=0;i<=4;i++){if (arr[i+1]<arr[i]){temp=arr[i];

arr[i]=arr[i+1];arr[i+1]=temp;}

}}

for(i=1;i<=5;i++){printf("\n %d", arr[i]);}getch();}

/* Sorting of an array in descending order i.e. bottom to top*/# include<stdio.h>void main()

{int arr[5];int i=0, temp=0,j=0;for(i=0;i<=4;i++){printf("Enter number:");scanf("%d", &arr[i]);}

temp=arr[0];

for(j=0;j<=4;j++){for(i=0;i<=4;i++)

{if (arr[i+1]>arr[i]){temp=arr[i];arr[i]=arr[i+1];arr[i+1]=temp;

} } }for(i=0;i<=4;i++)

{

printf("\n %d", arr[i]);}getch();}

D. H. B. Soni College, Solapur D. H. B. Soni College, Solapur  -- 2323 --

8/8/2019 Solved C Programs

http://slidepdf.com/reader/full/solved-c-programs 24/48

Programming in C

/* program to display contents of 2d array */# include<stdio.h>

main(){int arr[2][2]={

{1,2},{3,4}

};int i=0,j=0;clrscr();

printf("\n Array contents are:\n");for(i=0;i<=1;i++)

{for(j=0;j<=1;j++){printf("arr{%d][%d]=%d\t", i,j,arr[i][j]);}printf("\n");}

getch();}

/* Accept & program to display contents of 2d array */

# include<stdio.h>main(){int arr[3][3];int i=0,j=0;clrscr();printf("\n Enter elements are:\n");

for(i=0;i<=1;i++){printf("\n");

for(j=0;j<=1;j++){printf("Enter arr{%d][%d]:\t", i,j);scanf("%d", &arr[i][j]);}}for(i=0;i<=1;i++){

printf("\n");

for(j=0;j<=1;j++){printf("arr{%d][%d]=%d\t", i,j,arr[i][j]);

}}getch();}

/* program to add and subtract two 3X3 matrices */# include<stdio.h>

main()

{int arr1[3][3], arr2[3][3];int arradd[3][3], arrsub[3][3];int i=0,j=0;

D. H. B. Soni College, Solapur D. H. B. Soni College, Solapur  -- 2424 --

8/8/2019 Solved C Programs

http://slidepdf.com/reader/full/solved-c-programs 25/48

Programming in C

clrscr();for(i=0;i<=1;i++)

{printf("\n");for(j=0;j<=1;j++){printf("Enter arr{%d][%d]:\t", i,j);

scanf("%d", &arr1[i][j]);}}

for(i=0;i<=1;i++){

printf("\n");for(j=0;j<=1;j++){printf("Enter arr{%d][%d]:\t", i,j);scanf("%d", &arr2[i][j]);}}

for(i=0;i<=1;i++){for(j=0;j<=1;j++)

{arradd[i][j]=arr1[i][j]+arr2[i][j];

arrsub[i][j]=arr1[i][j]-arr2[i][j];}}printf("\n Addition of two matrices\n\n");for(i=0;i<=1;i++){

for(j=0;j<=1;j++)

{printf("arradd[%d[%d]=%d\t",i,j,arradd[i][j]);}

printf("\n");}printf("\nAddition of two matrices\n\n");for(i=0;i<=1;i++){for(j=0;j<=1;j++){printf("arradd[%d[%d]=%d\t",i,j,arrsub[i][j]);

}

printf("\n");}getch();}

// program for matrix multiplication.# include<stdio.h>void main(){

int arr1[2][2], arr2[2][2], arr3[2][2];int i=0,j=0;

clrscr();

printf("\n Enter elements for first array:");for(i=0;i<=1;i++){for(j=0;j<=1;j++){

D. H. B. Soni College, Solapur D. H. B. Soni College, Solapur  -- 2525 --

8/8/2019 Solved C Programs

http://slidepdf.com/reader/full/solved-c-programs 26/48

Programming in C

printf("\n Enter element");scanf("%d", &arr1[i][j]);

}printf("\n");}printf("\n Enter elements for second array:");for(i=0;i<=1;i++)

{for(j=0;j<=1;j++){

printf("\n Enter element");scanf("%d", &arr2[i][j]);

}printf("\n");}arr3[0][0]=arr1[0][0]*arr2[0][0]+arr1[0][1]*arr2[1][0];arr3[0][1]=arr1[0][0]*arr2[0][1]+arr1[0][1]*arr2[1][1];arr3[1][0]=arr1[1][0]*arr2[0][0]+arr1[1][1]*arr2[1][0];arr3[1][1]=arr1[1][0]*arr2[0][1]+arr1[1][1]*arr2[1][1];

printf("\n Matrix multiplication is:");for(i=0;i<=1;i++){

for(j=0;j<=1;j++){

printf("\n%d\t", arr3[i][j]);}printf("\n");}getch();}

// Program to display the contents of the string# include<stdio.h>void main()

{char str[20]="Welcome to Tech-Max";int i=0;clrscr();printf("\n Entered string is %s",str);printf("\n Values stored at each location are:\n");for(i=0;i<=19;i++){

printf("str[%d]=%c\n", i,str[i]);

}getch();}

//Program to accept string from user and display message.# include<stdio.h>void main(){char name[15];clrscr();printf("\nEnter name:");

scanf("%s", name);

printf("Welcome %s you are running C program", name);getch();}

D. H. B. Soni College, Solapur D. H. B. Soni College, Solapur  -- 2626 --

8/8/2019 Solved C Programs

http://slidepdf.com/reader/full/solved-c-programs 27/48

Programming in C

// Program to calculate the string length //# include<stdio.h>

void main(){int i=0;char str[10];printf("\n Enter string:");

scanf("%s",str);while (str[i]!=NULL){

i++;}

printf("\String length of %s=%d",str,i);getch();}

// Program to print reverse of the string //# include<stdio.h>void main()

{int i=0,j=1;char str[10];

clrscr();printf("\n Enter string:");

scanf("%s",str);while (str[i]!=NULL){i++;}printf("\n the reverse of the string is");for (j=i;j>=0;j--)

{printf("%c",str[j]);}

getch();}

// Program to print reverse of the string //# include<stdio.h>void main(){int i=0,j=1;

char str[10];

clrscr();printf("\n Enter string:");scanf("%s",str);

while (str[i]!=NULL){if (str[i]>=97 && str[i]<=122){str[i]=str[i]-32;}else if(str[i]>=65 && str[i]<=96){

str[i]=str[i]+32;

}i++;}printf("\n The upper case string is %s", str);

D. H. B. Soni College, Solapur D. H. B. Soni College, Solapur  -- 2727 --

8/8/2019 Solved C Programs

http://slidepdf.com/reader/full/solved-c-programs 28/48

Programming in C

getch();}

// program for string copy..# include<stdio.h>main(){char str1[10], str2[10];int i=0;clrscr();

printf("\n Enter first string:");scanf("%s", str1);

printf("\nEnter second string:");scanf("%s",str2);

printf("\nBefore copying:");printf("\nFirst string= %s Second string=%s",str1,str2);for(i=0;i<=9;i++){str2[i]=str1[i];

}printf("\nAfter copying");printf("\nFirst string= %s Second string=%s",str1,str2);

getch();}

// program for string comparison# include<stdio.h>main(){char str1[10], str2[10];int i=0, flag=0;

printf("\n Enter first string:");scanf("%s",str1);printf("\n Enter second string:");

scanf("%s", str2);for (i=0;i<=9;i++)

{if (str1[i]==str2[i]){flag=1;}else

{

flag=2;break;}

}if (flag==1){printf("\n %s and %s are equal", str1,str2);}else{printf("\n %s and %s are not equal", str1,str2);

}

getch();}

// program to find whether a given string is vowel or consonent

D. H. B. Soni College, Solapur D. H. B. Soni College, Solapur  -- 2828 --

8/8/2019 Solved C Programs

http://slidepdf.com/reader/full/solved-c-programs 29/48

Programming in C

# include<stdio.h>void main()

{char str[10];int i=0, cnt1=0, cnt2=0;clrscr();printf("Enter string:");

scanf("%s",str);while(str[i]!=NULL){

if (str[i]=='a' || str[i]=='e' || str[i]=='i' || str[i]=='o' || str[i]=='u'){

cnt1++;}else{cnt2++;}i++;

}printf("\n The string entered is %s", str);printf("\n Vowels=%d and consonants=%d", cnt1, cnt2);

getch();}

// program to find given string palindrome or not.# include<stdio.h>void main(){int i=0, j=0;char str[10];

int flag=0;clrscr();printf("Enter string");

scanf("%s", str);while(str[i]!=NULL){i++;}i--;while (j!=i){

if (str[i]==str[j])

{flag=1;}

else{flag=2;break;}

 j++;

i--;}

if (flag==1)

{printf("%s is palindrom",str);}else{

D. H. B. Soni College, Solapur D. H. B. Soni College, Solapur  -- 2929 --

8/8/2019 Solved C Programs

http://slidepdf.com/reader/full/solved-c-programs 30/48

Programming in C

printf("%s is not palindrom",str);}

getch();}

// Concatingating two strings in one string# include<stdio.h>main(){char str1[10], str2[10], str3[20];

int i=0,j=0;clrscr();

printf("Enter first string:");scanf("%s", str1);

printf("\n Enter second string:");scanf("%s", str2);while(str1[i]!='\0'){str3[i]=str1[i];

i++;}while(str2[j]!='\0')

{str3[i]=str2[j];

 j++;i++;}printf("The concatinated string is %s", str3);getch();}

// program to print the two dimensional array element.# include<stdio.h>void main()

{char city[5][10]= {

"Mumbai","Pune","Nasik","auragabad","Kolhapur"};

int i=0;

clrscr();

for(i=0;i<=4;i++){printf("\n %s", city[i]);

}getch();}

//program to accept names of 5 students and display them.# include<stdio.h>void main(){

int i=0;

char fname[5][10];clrscr();for(i=0;i<=4;i++)

{

D. H. B. Soni College, Solapur D. H. B. Soni College, Solapur  -- 3030 --

8/8/2019 Solved C Programs

http://slidepdf.com/reader/full/solved-c-programs 31/48

Programming in C

printf("\n Enter string:");scanf("%s", fname[i]);

}printf("\n List of students");for (i=0;i<=4;i++){printf("\n %s", fname[i]);

}getch();}

// program to check whether two strings are equal or not.

# include<stdio.h>main()

{char str1[20]="Computer";char str2[10]="World";clrscr();printf("\n String length of %s is %d", str1,strlen(str1));

printf("\n String length of %s is %d", str2,strlen(str2));if (strcmp(str1,str2)!=0){

printf("\n %s and %s are not equal",str1,str2);}

strcat(str1,str2,3);printf("\n After concatination str1 is %s", str1);strcpy(str1,str2);printf("\n After copying str1=%s str2=%s", str1,str2);getch();}

// Use of automatic variables# include<stdio.h>void fun1(void);

void fun2(void);void main()

{int i=500;clrscr();printf("\n %d",i);fun1();fun2();

getch();

}void fun1(){

int i=100;printf("\n %d");}

void fun2(){int i=50;printf("\n %d");

}

D. H. B. Soni College, Solapur D. H. B. Soni College, Solapur  -- 3131 --

8/8/2019 Solved C Programs

http://slidepdf.com/reader/full/solved-c-programs 32/48

Programming in C

// program on functions demo

# include<stdio.h>int max=100;void fun1(void);void main(){

void fun2(void);printf("\n Original max=%d",max);max=max-20;

printf("\n max in main()=%d",max);fun1();

fun2();getch();}void fun1(){max=max*2;printf("\n max in fun1=%d", max);

}void fun2(){

max=max+100;printf("\n max in fun2=%d", max);

}

# include<stdio.h>

void demo(void);main()

{

int i=1;clrscr();for (i=1;i<=3;i++){demo();}getch();

}void demo()

{int static s=0;s=s+1;

printf("\n %d",s);}

# include<stdio.h>void main(){register int i=1;clrscr();for (i=1;i<=3;i++){

printf("\n Welcome to Tech-Max Publications");

}getch();}

D. H. B. Soni College, Solapur D. H. B. Soni College, Solapur  -- 3232 --

8/8/2019 Solved C Programs

http://slidepdf.com/reader/full/solved-c-programs 33/48

Programming in C

// to add two numbers using add function

# include<stdio.h>void main(){void add(void);clrscr();

add();getch();}

void add(){

int a=0,b=0;printf("\n Enter first number:");scanf("%d", &a);printf("\n Enter second number:");scanf("%d", &b);printf("\n The addition of %d + %d =%d", a, b,a+b);}

//find area of circle using function

# include <stdio.h>const float pi=3.14;

void area(void);void main(){clrscr();area();getch();}

void area(void){float radius=1.0;

printf("\n Enter area of circle");scanf("%f", &radius);printf("\n Area of circle is %f", pi*radius*radius);}

// program to find out whether number is even or odd.# include<stdio.h>

void main()

{void evenodd(void);clrscr();

evenodd();getch();}void evenodd(){int number=25;if (number%2==0)printf("%d is even", number);

else

printf("%d is odd", number);}

D. H. B. Soni College, Solapur D. H. B. Soni College, Solapur  -- 3333 --

8/8/2019 Solved C Programs

http://slidepdf.com/reader/full/solved-c-programs 34/48

Programming in C

// program to calculate rate of intrest yearly as 21 %# include<stdio.h>

void int_cal(void);void main(void){int_cal();getch();

}void int_cal(){

float roi=21, interest=1.0, prin_amt=1.0;printf("Enter principle amount:");

scanf("%f", &prin_amt);interest=prin_amt*roi/100;printf("\n Yearly interest=%f", interest);}

//program to print odd numbers from 1 to 50

# include<stdio.h>void odd_prn(void);void main()

{clrscr();

odd_prn();getch();}void odd_prn(){int i=1;printf("\n Odd numbers from 1 to 50:\n");

for(i=1;i<=50; i+=2){printf("%d\t", i);

}}

//program to convert given number into binary equivalent# include<stdio.h>void main(){

void bin_conv(void);

clrscr();bin_conv();getch();

}void bin_conv(){int number=0, rem=0,arr[5];int i=0, j=0;printf("\n Enter number:");scanf("%d", &number);printf("\n Binaray equvivalent of %d=",number);

while (number!=0)

{rem=number%2;arr[i]=rem;i++;

D. H. B. Soni College, Solapur D. H. B. Soni College, Solapur  -- 3434 --

8/8/2019 Solved C Programs

http://slidepdf.com/reader/full/solved-c-programs 35/48

Programming in C

number=number/2;}

i--;for(j=i;j>=0;j--){printf("%d",arr[j]);}

}

// Program to convert decimal to binary

# include<stdio.h># include<math.h>

void conv_deci(void);void main()

{clrscr();conv_deci();getch();}

void conv_deci(){int num=0;

int dnum=0;int i=0, rem=0;

printf("Enter number");scanf("%d", &num);printf("Decimal of %d is :", num);while(num!=0){rem=num%10;dnum=dnum+(pow(2,i)*rem);

num=num/10;i++;}

printf("%d", dnum);}

//progrm to print the square of numbers till the position entered by user.# include<stdio.h>void sqr_prn(int);main()

{

int pos=0;printf("\nEnter last position");scanf("%d", &pos);

sqr_prn(pos);getch();}void sqr_prn(int x){int i=1;for(i=1;i<=x;i++){

printf("%d\t", i*i);

}}

D. H. B. Soni College, Solapur D. H. B. Soni College, Solapur  -- 3535 --

8/8/2019 Solved C Programs

http://slidepdf.com/reader/full/solved-c-programs 36/48

Programming in C

// program to print start pattern using functions.# include<stdio.h>

void star_prn(int);main(){int row=0;clrscr();

printf("Enter last row:");scanf("%d", &row);star_prn(row);

getch();}

void star_prn(int r){int i=0,j=0;for(i=1;i<=r;i++){printf("\n");for(j=r;j>=i;j--)

{printf(" * ");}

}}

// program to write the power function# include<stdio.h>main(){int num=0, raiseto=0;void power(int, int);

clrscr();printf("\nEnter number:");scanf("%d", &num);

printf("\nEnter raise to:", &raiseto);scanf("%d", &raiseto);power(num, raiseto);getch();}void power(int n, int r){int i=1;

int ans=1;

for (i=1;i<=r;i++){ans=ans*n;

}printf(" %d raise to %d=%d", n,r,ans);}

// program to write a function which will exchange two strings# include<stdio.h>void exchange(char[], char[]);

void main()

{char str1[10],str2[10];clrscr();printf("\n Enter first string");

D. H. B. Soni College, Solapur D. H. B. Soni College, Solapur  -- 3636 --

8/8/2019 Solved C Programs

http://slidepdf.com/reader/full/solved-c-programs 37/48

Programming in C

scanf("%s",str1);printf("\n Enter second string");

scanf("%s",str2);printf("\nBefore exchange first string= %s & second string=%s", str1, str2);exchange(str1,str2);getch();}

void exchange(char s1[], char s2[]){char temp[10];

int i=0;for(i=1;i<=9;i++)

{temp[i]=s1[i];}for(i=1;i<=9;i++){s1[i]=s2[i];}

for(i=1;i<=9;i++){s2[i]=temp[i];

}printf("\nAfter exchange first string= %s & second string=%s", s1, s2);

}

// Program to calcualte number of even and odd numbers.# include<stdio.h>void evenodd(int []);void main()

{int i=0, arr[5];for (i=0;i<=4;i++)

{printf("\n Enter numbers:");scanf("%d", &arr[i]);}evenodd(arr);getch();}void evenodd(int a[])

{

int i=0;int evenno=0, oddno=0;for(i=0;i<=4;i++)

{if(a[i]%2==0){evenno++;}if(a[i]%2>=1)

{oddno++;

}

}printf("Even numbers=%d \t Odd number=%d",evenno,oddno);}

D. H. B. Soni College, Solapur D. H. B. Soni College, Solapur  -- 3737 --

8/8/2019 Solved C Programs

http://slidepdf.com/reader/full/solved-c-programs 38/48

Programming in C

//program to write a function to identify number whether it is prime or not.

# include<stdio.h>void prime(int);void main(){int no=0;

clrscr();printf("\n Enter number:");scanf("%d", &no);

prime(no);getch();

}void prime(int n){if (n==3 || n==5 || n==7){printf("\n %d is prime",n);}

else{if(n%2>0 && n%3>0 && n%5>0 && n%7>0)

{printf("\n %d is prime",n);

}else{printf("\n %d is not prime",n);}}

}

// To display the case of alphabet in words.void charcase(char);

# include<stdio.h>void main(){char code;clrscr();printf("\n Enter character");scanf("%c", &code);charcase(code);

getch();

}void charcase(char x){

if(x>=65 && x<=91){printf("\n %c is in upper case",x);}else if(x>=97 && x<=122){

printf("\n %c is in lower case",x);}

else

{printf("\n Entered value is not character");}}

D. H. B. Soni College, Solapur D. H. B. Soni College, Solapur  -- 3838 --

8/8/2019 Solved C Programs

http://slidepdf.com/reader/full/solved-c-programs 39/48

Programming in C

// program to calculate prime numbers from 1 to 50.

# include<stdio.h>int primeadd(void);void main(){int ans=0;

clrscr();ans=primeadd();printf("\n Additin of prime numbers from 1-50=%d",ans);

getch();}

int primeadd(){int i=1, sum=0;printf("\n Prime numbers are:\n");for (i=1;i<=50;i++){if (i==3 || i==5 || i==7)

{printf("%d\t",i);sum=sum+i;

}if(i%2>0 && i%3>0 && i%5>0 && i%7>0)

{sum=sum+i;printf("%d\t", i);}}return sum;

}

// calculate the string lenghth using function.# include<stdio.h>int str_len();void main(){int length=0;clrscr();length=str_len();

printf("\n string length= %d", length);

getch();}int str_len()

{int i=0;char str[10];printf("\n Enter string");scanf("%s", str);while (str[i]!=NULL)

{i++;

}

return i;}

D. H. B. Soni College, Solapur D. H. B. Soni College, Solapur  -- 3939 --

8/8/2019 Solved C Programs

http://slidepdf.com/reader/full/solved-c-programs 40/48

Programming in C

// program to find out greates number from given matrix using function.# include<stdio.h>

int matgreat();void main(){clrscr();printf("\n Greatest number in matrix=%d", matgreat());

getch();}int matgreat()

{int i=0,j=0,mat[2][2],gno=0;

for(i=0;i<=1;i++){printf("\n");for(j=0;j<=1;j++){printf("Enter[%d][%d]element:",i,j);scanf("%d", &mat[i][j]);

}}for(i=0;i<=1;i++)

{for(j=0;j<=1;j++)

{if (gno<mat[i][j]){gno=mat[i][j];}}

}

return gno;}

// program to find out factorial of number using function.# include<stdio.h>void main(){long int fact(void);clrscr();printf("\n The factorial of number %d",fact());

getch();

}long int fact(void){

int no=1;long fact=1,i=0;printf("\n Enter number:");scanf("%d", &no);for (i=1;i<=no;i++){

fact=fact*i;}

return fact;

}

D. H. B. Soni College, Solapur D. H. B. Soni College, Solapur  -- 4040 --

8/8/2019 Solved C Programs

http://slidepdf.com/reader/full/solved-c-programs 41/48

Programming in C

// program to find out the sum of digits.# include<stdio.h>

int digit_sum(int);main(){int no;clrscr();

printf("\n Enter number:");scanf("%d", &no);printf("Sum of digits of %d=%d",no,digit_sum(no));

getch();}

int digit_sum(int num){int rem=0, sum=0;while(num!=0){rem=num%10;sum=sum+rem;

num=num/10;}return sum;

}

// program to accept angles and check whether it is triangle or not.# include<stdio.h>int triangle(int,int,int);void main(){int a1=0,a2=0,a3=0;int tota=0;

clrscr();printf("\n Enter first angle:");scanf("%d", &a1);

printf("\n Enter second angle:");scanf("%d", &a2);printf("\n Enter third angle:");scanf("%d", &a3);tota=triangle(a1,a2,a3);if (tota==180){printf("\n This is a triangle");

}

else{printf("\n this is not a triangle");

}getch();}int triangle(int x, int y, int z){return x+y+z;

}

// program to find out sub string

int substr(int,int,char[]);void main(){char str[10],n=0;

D. H. B. Soni College, Solapur D. H. B. Soni College, Solapur  -- 4141 --

8/8/2019 Solved C Programs

http://slidepdf.com/reader/full/solved-c-programs 42/48

Programming in C

int fpos=0, lpos=0;clrscr();

printf("\n Enter string:");scanf("%s", str);printf("\n Enter first and last position:");scanf("%d %d", &fpos, &lpos);n=substr(fpos,lpos,str);

printf("\n Number of characters displayed=%d", n);getch();}

int substr(int f, int l, char st[]){

int i=0;f--;l--;for(i=f;i<=l;i++){printf("%c",st[i]);}

l++;return l-f;}

// program to show the recursion by displaying hello message.# include<stdio.h>void main(){printf("\n Hello, You are in the world of 'C'");main();getch();

}

// program for factorial of a no. using recursion function# include<stdio.h>

long int fact(int);void main(){int no=1;clrscr();printf("\n Enter number:");

scanf("%d", &no);

printf("Factorial of number=%ld",fact(no));getch();}

long int fact(int n){long int f;if(n==1){return 1 ;}else

f=n*fact(n-1);

return (f);}

D. H. B. Soni College, Solapur D. H. B. Soni College, Solapur  -- 4242 --

8/8/2019 Solved C Programs

http://slidepdf.com/reader/full/solved-c-programs 43/48

Programming in C

// Addition of two numbers using call by reference.int add(int *, int*);

# include<stdio.h>main(){int a=0,b=0;clrscr();

printf("\n Enter first number:");scanf("%d", &a);printf("\n Enter second number:");

scanf("%d",&b);printf("\nAddition = %d",add(&a,&b));

getch();}int add(int *x, int *y){return *x+*y;}

//Program to demonstrate the initialization of structures.# include <stdio.h>void main()

{struct emp

{int emp_no;char emp_name[10];float emp_sal;};struct emp e= {101,"Ashish", 5250.50};printf("\n emp_no= %d",e.emp_no);

printf("\n emp_name=%s",e.emp_name);printf("\n emp_sal=%f", e.emp_sal);getch();

}

// program to accept and display details of book structure.# include<stdio.h>struct book{int book_id;

char book_name[10];

float price;}b;void main()

{clrscr();printf("\n Enter book-id:");scanf("%d",&b.book_id);printf("\n Enter book-name");scanf("%s",b.book_name);printf("\n Enter book price:");scanf("%f", &b.price);

printf("\nBook id= %d \t book name=%s \t book price=%f \t", b.book_id,b.book_name,

b.price);getch();}

D. H. B. Soni College, Solapur D. H. B. Soni College, Solapur  -- 4343 --

8/8/2019 Solved C Programs

http://slidepdf.com/reader/full/solved-c-programs 44/48

Programming in C

/*program to create a structure to store the account details of the bank.include account number,member name, balance and account type i.e. saving, current,

etc. If balance id < 2000 then display message "Minimum Balance". */# include<stdio.h>void main(){struct bank_acno

{int acno;char mem_name[10];

float balance;char ac_ty[10];

}b_acc;clrscr();printf("\n Enter Bank Account details\n");printf("\n Enter Bank Account Number:");scanf("%d", &b_acc.acno);printf("\n Enter Account holder's Number:");scanf("%s", b_acc.mem_name);

printf("\n Enter balance");scanf("%f",&b_acc.balance);printf("\n Enter Account type:");

scanf("%s", b_acc.ac_ty);printf("\n\n Bank Account details\n");

printf("\n Bank Account Number=%d", b_acc.acno);printf("\n Account holder's Name=%s", b_acc.mem_name);printf("\n Account type=%s",b_acc.ac_ty);printf("\n Balance=%f",b_acc.balance);if (b_acc.balance<2000){

printf("\n Minimum Balance in your Account!!!!!!!!!!!!!!!!!!!!!1");

}getch();}

// program to find highest student given in array of structures.# include<stdio.h>void main(){struct stud{

int rollno;

char name[10];int percent;}s[3];

int i=0;for(i=0;i<=2;i++){printf("\n Enter rollno, name and percent:");scanf("%d %s %d",&s[i].rollno, s[i].name, &s[i].percent);}

for(i=0;i<=2;i++){

printf("\n rollno=%d name=%s percent=%d",s[i].rollno, s[i].name, s[i].percent);

}getch();}

D. H. B. Soni College, Solapur D. H. B. Soni College, Solapur  -- 4444 --

8/8/2019 Solved C Programs

http://slidepdf.com/reader/full/solved-c-programs 45/48

Programming in C

/* Program to store the details of employee and find out the

highest salary. */# include<stdio.h>void main(){struct emp

{int empno;char name[10];

int salary;}e[3];

int i=0, temp=0;clrscr();for(i=0;i<=2;i++){printf("\n Enter employee number:");scanf("%d",&e[i].empno);printf("\n Enter employee name:");

scanf("%s",e[i].name);printf("\n Enter employee salary");scanf("%d",&e[i].salary);

}for(i=0;i<=2;i++)

{printf("\nEmpno-%d\tEmp name-%s \t emp salary=%d", e[i].empno, e[i].name, e[i].salary);if (temp<e[i].salary){temp=e[i].salary;}

}

printf("\n The hightest salary=%d",temp);getch();}

// program to demonstrate the use of pointers# include<stdio.h>void main(){int number=0;int *ptr;clrscr();

printf("\n Enter number:");

scanf("%d", &number);ptr=&number;printf("\n %d is stored at %u",number,ptr);

getch();}

// program to print the pointers# include<stdio.h>main(){int no1=0, no2=0;

int *pt1, *pt2;

clrscr();printf("\n Enter first number:");scanf("%d", &no1);printf("\n Enter second number:");

D. H. B. Soni College, Solapur D. H. B. Soni College, Solapur  -- 4545 --

8/8/2019 Solved C Programs

http://slidepdf.com/reader/full/solved-c-programs 46/48

Programming in C

scanf("%d", &no2);pt1=&no1;

pt2=&no2;printf("\n %d * %d=%d ",*pt1,*pt2, (*pt1)*(*pt2));getch();}

// Pointer arithmetic# include<stdio.h>main()

{int a=0, b=0;

int *p1, *p2;clrscr();

printf("\n Enter first number:");scanf("%d", &a);printf("\n Enter second number:");scanf("%d", &b);p1=&a;

p2=&b;printf("\n %d is at %u and %d is at %u", a,p1,b,p2);printf("\n p1-p2= %d " , p1-p2);

printf("\n *p1 + *p2=%d ", *p1 + *p2);printf("\n *p1*5= %d", *p1*5);

printf("\n*p1/*p2= %d", *p1/ *p2);printf("\n *p1/5= %d", *p1/5);printf("\n p1++ =%u",++p1);getch();}

// pointer addition using function

# include<stdio.h>int add(int *);void main()

{int *pt, pos=0;

printf("\n enter position:");scanf("%d", &pos);pt=&pos;printf("%d", add(pt));getch();}

int add(int *p)

{int i=0;int sum=0;

for(i=1;i<=(*p);i++){sum=sum+i;}return sum;}

// program to accept array elements and print them using pointers.

# include<stdio.h>

void main(){int arr[5],i=0;

int *ptr;

D. H. B. Soni College, Solapur D. H. B. Soni College, Solapur  -- 4646 --

8/8/2019 Solved C Programs

http://slidepdf.com/reader/full/solved-c-programs 47/48

Programming in C

clrscr();for(i=0;i<=4;i++)

{printf("Enter numbers:");scanf("%d", &arr[i]);}ptr=&arr[0];

i=1;while (i<=5){

printf("\n%d is stored at %u", *ptr, ptr);ptr++;

i++;}getch();}

// program to count zeros in an element# include<stdio.h>

void zero_cnt(int *);void main(){

int arr[5], *p;int i=0;

clrscr();for(i=0;i<=4;i++){printf("\nEnter elements:");scanf("%d", &arr[i]);}p=&arr[0];

zero_cnt(p);getch();}

void zero_cnt(int *x){int i=0, cnt=0;for(i=0;i<=4;i++){if( (*x)==0){

cnt++;

}x++;}

printf("\n Number of zeros=%d",cnt);}

//program to find out string length using pointer and function.# include<stdio.h>int leng(char *);void main(){

char *ptr, str[10];

clrscr();printf("\n Enter string:");scanf("%s", str);ptr=&str[0];

D. H. B. Soni College, Solapur D. H. B. Soni College, Solapur  -- 4747 --

8/8/2019 Solved C Programs

http://slidepdf.com/reader/full/solved-c-programs 48/48

Programming in C

printf("\n Length of string=%d",leng(ptr));getch();

}int leng(char *p){int cnt=0;while (*p!=NULL)

{cnt++;p++;

}return cnt;

}

// demo of structure# include<stdio.h>void main(){

struct movie{char movie_nm[20];

char actor[15];int rel_year;

};struct movie *ptr;clrscr();printf("\n Enter movie_nm:");scanf("%s", ptr->movie_nm);printf("\n Enter movie actor:");scanf("%s", ptr->actor);

printf("\n Enter release year:");scanf("%d",&ptr->rel_year);printf("\n Movie name - %s", ptr->movie_nm);

printf("\n Actor-%s",ptr->actor);printf("\n Release year-%d",ptr->rel_year);getch();}