Best C Programming Solution

38
Language : - It is the media of communication. C is the Language of programs & functions developed by Dennis Ritchie in 1972-73 & in AT & T Bell Laboratories in USA. Programs : - It is a group of statement which are executed step by step to perform a desire task. Programs are the instructions given to the system. Instruction : - A single order given to the system is called Instruction. Software : - It is a group of programs working with coordination with each other to get the desired result. Software is of two types: - 1. System Software : - like operating system (window) 2. Application Software : - like MS Office, Adobe Reader and Anti-Virus. Language is of two types: - 1. Low Level Language : - Which is also called Machine language. E.g.: - C,C++ 2. High Level Language : - Which is used in simple English Language. E.g.: - HTML, PHP, Java and Dot Net.

Transcript of Best C Programming Solution

Page 1: Best C Programming Solution

Language: - It is the media of communication.

C is the Language of programs & functions developed by Dennis Ritchie in 1972-73 & in AT & T Bell Laboratories in USA.

Programs: - It is a group of statement which are executed step by step to perform a desire task. Programs are the instructions given to the system.

Instruction: - A single order given to the system is called Instruction.

Software: - It is a group of programs working with coordination with each other to get the desired result.

Software is of two types: -

1. System Software : - like operating system (window)2. Application Software : - like MS Office, Adobe Reader and Anti-Virus.

Language is of two types: -

1. Low Level Language : - Which is also called Machine language. E.g.: - C,C++

2. High Level Language: - Which is used in simple English Language. E.g.: - HTML, PHP, Java and Dot Net.

C is also called Middle Level Language because it is flexible & compatible with machine as like Low Level Language or use friendly as like high level language.

Translators: - These are the software’s which convert high level language or vice versa.

There are 2 types of Translators.

1. Compiler

Page 2: Best C Programming Solution

2. Interpreter

Structure of C Language

1. Write a program to get the output the output Hello?

# include <stdio.h> # include <conio.h> Void main () { Int a, b, c; printf (“Enter the value of a & b”); scanf (“%d%d”,&a,&b); c=a+b; printf (“\n Sum is of %d”,c); getch (); }

Output: Hello2. Write a program to get the sum of a & b?

# include <stdio.h># include <conio.h>Void main (){Int a, b, c;printf (“Enter the value of a & b”);scanf (“%d%d”,&a,&b);C=a+b;printf (“\n Sum is of %d”, c);getch ();}

Page 3: Best C Programming Solution

Output: Enter the value of a & b:-A=3 B=2

Output will be 5.

3. Write a program to enter 3 numbers & calculate their sum?

# include <stdio.h># include <conio.h>Void main (){Int a, b, c, d;clrscr ();printf (“Enter the value of a & b & c”);scanf (“%d%d%d”,&a,&b,&c);d=a+b+c;printf (“\n Sum is of %d”, d);getch (); }

Output: Enter the value of a,b,c:-A=3B=4C=5

The sum is 12

4.W.A.P to calculate Simple Interest? # include <stdio.h># include <conio.h>void main (){Int p, r, t, s;clrscr ();printf (“Enter the value of p & r & t”);scanf (“%d%d%d”, &p, &r, &t);

Page 4: Best C Programming Solution

S=p*r*t/100;printf (“\n Simple Interest is of %d”, s);getch ();}

Output: put value of p=1000, r=2, t=1Simple interest is = 20

5.W.A.P to calculate the perimeter of circle & rectangle?# include <stdio.h># include <conio.h>void main (){int a, l, b, r;clrscr ();printf (“Enter the value of l & b & r”);scanf (“%d%d%d”, &l, &b, &r);A=2*a+b;B=2*3.14*r;printf (“\n Perimeter of rect. & circle is of %d\n%d”, a, b);getch ();}

Output: put l=6,b=8,r=4Answer is = 20,25

6.W.A.P to enter basic salary, da, hpa, pf & calculate total salary & net salary?

# include <stdio.h># include <conio.h>Void main (){Int ts, ns, bs, da, hpa, pf;Clrscr ();Printf (“Enter the value of bs & da & hpa & pf”);Scanf (“%d%d%d%d”, &bs, &da, &hpa, &pf);Ts=bs+hpa+da;Ns=ts-pf;Printf (“\n TS & NS is of %d\n%d”, ts, ns)Getch (); }

Output: put bs=67, da=67, hpa=56, pf=87;

Page 5: Best C Programming Solution

Answer is = Ts=190, Ns=103

7.W.A.P to enter initial reading, Final reading & charges per unit (CPU) & calculate units consume & bill?

# include <stdio.h># include <conio.h>void main (){int uc, b, ir, fr, cpu;clrscr ();printf (“Enter the value of ir & cpu & fr”);scanf (“%d%d%d”, &ir, &cpu, &fr);uc =fr-ir;b =uc*cpu;printf (“\n UC & B is of %d\n%d”, uc, b);getch (); }

Output= put ir=60, cpu=5, fr=80Answer: - Uc= 20; B=100

8.W.A.P to get the maximum number?# include <stdio.h># include <conio.h>void main (){int a, b, max;printf (“\n Enter the value a & b”);scanf (“%d%d”, &a, &b);max= (a>b)? a:b;printf (“\n Max No. is %d”, max);getch ();}

Output: put a=9, b=6Answer is: - Max No. is 9

9.W.A.P to enter any no. & click whether it is even or odd?# include <stdio.h>

Page 6: Best C Programming Solution

# include <conio.h>void main (){int num;clrscr ();printf (“Enter the value of num”);scanf (“%d”, & num);(num%2==0)? Printf (“even”): printf (“odd”);printf (“\n number is %d”, num);getch ();}

Output: Enter the value of num=8Even 8

10. W.A.P to enter a number and check whether it is positive or negative?

# include <stdio.h> # include <conio.h> Void main () { Int num; Printf (“Enter the value of num”); Scanf (“%d”, &num); (num>0)? Printf (“positive”): printf (“negative”); Getch (); }

Output: Enter the value of num:7 positive

11. W.A.P to enter any year whether it is leap year or not? # include <stdio.h> # include <conio.h> void main ()

Page 7: Best C Programming Solution

{int num;printf (“Enter the value of num”);scanf (“%d”, &num);(num%4==0)? Printf (“leap year”): printf (“not leap year”);printf (“\n leap year is %d”, num);getch ();}

Output: Enter the value of num: 2013 Not a leap year.

12. W.A.P to enter cost prize & selling prize & check whether there is profit or loss?# include <stdio.h># include <conio.h>void main (){int cp, sp;clrscr ();printf (“Enter the value of cp & sp”);scanf (“%d%d”, &cp, &sp);(cp<sp)? Printf (“profit”): printf (“loss”);getch ();}

Output: Enter the value of cp & sp: 500,300 loss of 200

13. W.A.P to input any age & check whether the person can vote or not?# include <stdio.h># include <conio.h>Void main (){Int age;

Page 8: Best C Programming Solution

Printf (“Enter the value of age”);Scanf (“%d”, &age);(age<18==0)? Printf (“can vote”): printf (“can’t Vote”);Getch ();}

Output: Enter the value of age: 18 Can Vote.

If Else Programs

14. W.A.P to enter the loss & profit?# include <stdio.h># include <conio.h>void main (){int cp, sp, l, p;clrscr ();printf (“Enter the value of cp & sp”);scanf (“%d%d”, &cp, &sp);if (cp>sp){l=cp-sp;printf (“\n loss is of %d”, l);}else{p=sp-cp;printf (“\n profit is of %d”, p);}getch ();}

Output: cp=500, sp=400

Page 9: Best C Programming Solution

Loss=100

15. W.A.P to enter the Positive or Negative?# include <stdio.h># include <conio.h>void main (){int num;clrscr ();printf (“Enter the value of num”);scanf (“%d”, &num);if (num>0){printf (“\n Number is Positive”);}else{printf (“\n Number is Negative”);}getch ();}

Output: Enter the value of num 77 is positive

16. W.A.P to enter the age and to know the person can vote or not?# include <stdio.h># include <conio.h>void main (){int age;clrscr ();

Page 10: Best C Programming Solution

printf (“Enter the value of age”);scanf (“%d”, &age);if (age>18){printf (“\n can Vote”);}else{printf (“\n can’t Vote”);}getch ();}

Output: Enter the value of age: - 18Can Vote

17. W.A.P to enter the value of a number whether it is odd or even?# include <stdio.h># include <conio.h>Void main (){Int num;Clrscr ();Printf (“Enter the value of num”);Scanf (“%d”, &num);If (num%2==0){Printf (“\n Number is even”);}Else{Printf (“\n Number is odd”);}Getch ();

Page 11: Best C Programming Solution

}Output: Enter the value of num 2

Even

18. W.A.P to enter the value of Maximum number?# include <stdio.h># include <conio.h>void main (){int a, b, max;clrscr ();printf (“Enter the value of a & b”);scanf (“%d%d”, &a, &b);if (a>b){printf (“\n a is max is %d”, a);}else{printf (“\n b is max is %d”, b);}getch ();}

Output: Enter the value of a=45,b=67B is max is 67

19. W.A.P to enter any alphabet to check vowel or consonant?# include <stdio.h># include <conio.h>void main ()

Page 12: Best C Programming Solution

{char alpha;printf (“enter any alphabet”);scanf (“%c”, &alphabet”);if (alpha==’a’||alpha==’e’||alpha==’i’||alpha==’o’||alpha==’u’)printf (“vowel”);elseprintf (“Consonant”);getch ();}

Output: Enter any alphabetR= Consonant

20. W.A.P to enter any age?

# include <stdio.h># include <conio.h>void main (){int age;clrscr ();printf (“enter any age”);scanf (“%d”, &age);if (age>1 && age<12)printf (“child”);else if (age>13 && age<50)printf (“mature”);elseprintf (“old”);getch ();}

Output: Enter any age=56

Page 13: Best C Programming Solution

Old21. W.A.P to enter any Month & check the season?

# include <stdio.h># include <conio.h>Void main (){Int m;Clrscr ();Printf (“Enter any month”);Scanf (“%d”, &m);If (m==12||m==1||m==2)Printf (“winter”);Else if (m==3||m==4||m==5)Printf (“spring”);Else if (m==6||m==7||m==8)Printf (“summer”);ElsePrintf (“autumn”);Getch ();}

Output: Enter any month= 4 Spring

22. W.A.P to enter the marks of 5 subjects & calculate its percentage & check position?# include <stdio.h># include <conio.h>void main (){int m1, m2, m3, m4, m5, p;clrscr ();

Page 14: Best C Programming Solution

printf (“enter the value of m1 & m2 & m3 & m4 & m5”);scanf (“%d%d%d%d%d”, &m1, &m2, &m3, &m4, &m5);p= (m1+m2+m3+m4+m5)/5;if (p>80)printf (“distinction”);else if (p>70 && p<80)printf (“1st position”);else if (p>60 && p<70)printf (“2nd position”);else if (p>30 && p<60)printf (“3rd position”);elseprintf (“fail”);getch ();}

Output: Enter the value of m1=1, m2=2, m3=3, m4=4, m5=5Fail

23. W.A.P to calculate max out of 3 numbers?# include <stdio.h># include <conio.h>void main (){int a, b, c;printf (“enter 3 nos.”);scanf (“%d%d%d”, &a, &b, &c);if (a>b){if (a>c)printf (“a is max”);elseprintf (“c is max”);

Page 15: Best C Programming Solution

}else{if (b>c)printf (“b is max”);elseprintf (“c is max”);}else[if (b>c)printf (“b is max”);elseprintf (“c is max”);}getch ();}

Output: Enter any 3 nos. 12, 16, 72

C is max.

Programs of Switch Control Constructs

It is an alternative to if else construct

24. W.A.P to enter color?# include <stdio.h># include <conio.h>Void main (){Char ch;

Page 16: Best C Programming Solution

Printf (“Color Menu”);Printf (“\n R for Red”);Printf (“\n B for Blue”);Printf (“\n G for Green”);Printf (“\n Enter your choice”);Scanf (“%c”, &ch);Switch (ch){Case ‘R’: printf (“Roses are Red”);

Break;Case ‘B’: printf (“Sky is blue”);

Break;Case ‘G’: printf (“Grass is Green”);

Break;Default: printf (“wrong choice”);}Getch ();}

Output: Enter your choice: RRoses are Red

25. W.A.P to enter the area of rect. & circle?# include <stdio.h># include <conio.h>Void main (){Int ch, l, b, r, a, p; Clrscr ();Printf (“Enter the value of l & b & r);Scanf (“%d%d%d”, &l, &b, &r);Switch (ch){Case 1: a=l*b;Printf (“\n Area is %d”, a);

Page 17: Best C Programming Solution

Break;Case 2: p=3.14*r*r;Printf (“\n Area of circle is %d”, p);

Break;Default: printf (“Wrong Choice”);}Getch ();}

Output: Enter the value of l=6, b=7, r=8Enter your choice 1

Answer: 42

26. W.A.P to enter the Addition, Subtraction, Division, Multiplication?# include <stdio.h># include <conio.h>void main (){char ch;int a, b, c;clrscr ();printf (“enter your choice”);scanf (“%c”, &ch);printf (“enter the value of a & b”);scanf (“%d%d”, &a, &b);switch (ch){case ‘+’: c=a+b;printf (“\n addition is %d”, c);

break;case ‘-‘: c=a-b;printf (“\n subtraction is %d”, c);

break;case ‘*’: c=a*b;printf (“\n multiplication is %d”, c);

break;case ‘/’: c=a/b;printf (“\n division is %d”, c);

Page 18: Best C Programming Solution

break;default: printf (“wrong choice”);}getch ();}

Output: Enter your choice 1Enter the value of a=5,b=6

Addition is 11.

27. W.A.P to check whether the entered alphabet is a vowel or consonant?# include <stdio.h># include <conio.h>Void main (){Char ch;Clrscr ();Printf (“Enter your choice”);Scanf (“%c”, &ch);Switch (ch){Case ‘a:Case ‘e’:Case ‘I’:Case ’o’:Case ‘u’:Printf (“Vowel”);

Break;Default: printf (“Consonant”):}Getch ();}

Output: Enter your choice: b

Page 19: Best C Programming Solution

Consonant28. W.A.P to enter the month & Season with switch?

# include <stdio.h># include <conio.h>Void main (){int m; Clrscr ();Printf (“Enter your choice”);Scanf (“%d”, &m);Switch (m){Case 12:Case 1:Case 2:Printf (“Winter”);

Break;Case 3:Case 4:Case 5:Printf (“Spring”);

Break;Case 6:Case 7:Case 8:Printf (“Summer”);

Break;Default: printf (“Autumn”);

Page 20: Best C Programming Solution

}Getch ();}

Output: Enter your choice 12Winter

Control Structures or Control Constructs

They are those constructs which control the flow of execution of the program. It is of 3 types: -

(i). For loop

(ii). While loop

(iii). Do While loop

29. W.A.P to find the sum of first five natural numbers?# include <stdio.h># include <conio.h>void main (){int i;clrscr ();printf (“the natural nos. are”);for (i=1; i<=5; i++)

Page 21: Best C Programming Solution

{printf (“%d”, i);}getch ();}

Output: The Natural Nos. are 1 2 3 4 5.

30. W.A.P to print first 10 even numbers?# include <stdio.h># include <conio.h>void main (){int i;clrscr ();printf (“the even nos. are”);for (i=2; i<=20; i+=2){printf (“%d”, i);}getch ();}

Output: The Natural numbers are 2,4,6,8,10,12,14,16,18,20

31. W.A.P to find the sum of first five natural numbers?# include <stdio.h># include <conio.h>void main (){int i, sum=0;clrscr ();

Page 22: Best C Programming Solution

printf (“the sums of first five natural nos. are”);for (i=1; i<5; i++){sum= sum+i;}printf (“%d”, sum);getch ();}

Output: The sum of first five Natural nos. are =15.

32. W.A.P to print first 100 odd nos.?# include <stdio.h># include <conio.h>void main (){int i;clrscr ();printf (“the odd nos. are”);for (i=1; i<=19; i+=2){printf (“%d”, i);}getch ();}

Output: The first 10 odd nos. are =1, 3, 5, 7, 9, 11, 13, 15, 17, 19.

Programs of For Loop(Fibonacci Series)

Page 23: Best C Programming Solution

33. W.A.P to enter the value of a & b?# include <stdio.h># include <conio.h>void main (){int a=0, b=1, c, i;for (i=1; i<=10; i++){printf (“%d”, a);c=a+b;a=b;b=c;}getch ();}

Output: 112358132134

34. W.A.P to find sum of n Numbers?# include <stdio.h># include <conio.h>void main (){int a, i, sum=0;clrscr ();printf (“enter the value of n”);scanf (“%d”, &n);for (i=1; i<=n; i++){sum =sum+i;

Page 24: Best C Programming Solution

}printf (“%d”, sum);getch ();}

Output: Enter the value of n=4Answer: 10

35. W.A.P to get factorial of no.?# include <stdio.h># include <conio.h>void main (){int num, fact=0, i;printf (“enter any no.”);scanf (“%d”, &num);for (i=num; i>=1; i--){fact =fact*i;}printf (“factorial of a no. is %d”, fact);getch ();}

Output: Enter any no. =4Fact. Of a no is 0.

36. W.A.P to make a table of any no.?# include <stdio.h># include <conio.h>void main (){

Page 25: Best C Programming Solution

int num, i, product;clrscr ();printf (“enter the value of num”);scanf (“%d”, &num);for (i=1; i<=10; i++){product = num*i;printf (“\n %d*%d=%d”, num, i, prod.);}getch ();}

Output: Enter the value of num 22*1=22*2=42*3=62*4=8

2*5=102*6=122*7=142*8=162*9=18

2*10=20

37. W.A.P to find the power of the number?# include <stdio.h># include <conio.h>Void main (){Int y, x, prod=1, I;clrscr ();

Page 26: Best C Programming Solution

printf (“Enter the value of x & y’);scanf (“%d%d”, &x, &y);for (i=1; i<=y; i++){prod =prod*x;}printf (“%d”, prod);getch ();}

Output: Enter the value of x=5, y=2: -25

38. W.A.P to find the sum of square of a number?# include <stdio.h># include <conio.h>Void main (){Int y, I, prod, sum=0;Clrscr ();For (i=1; i<=5; i++){Prod =i*i;Sum =sum + prod;}Printf (“%d”, sum);Getch ();}

Output: 55.39. W.A.P to find sum and average of n numbers?

# include <stdio.h># include <conio.h>Void main (){

Page 27: Best C Programming Solution

int n, i, num=0, average;clrscr ();printf (“Enter the value of n”);scanf (“%d”, &n);for (i=1; i<=n; i++){num = num + I;}printf (“Sum is %d”, num);average = num/n;printf (“\n Average is %d”, average);getch ();}

Output: Enter the value of n = 40Sum is 820

Average is 20.

40. W.A.P to find reverses of a number?# include <stdio.h># include <conio.h>void main (){int n, r=0;printf (“enter any no.”);scanf (“%d”, &n);while (n! =0){r=r*10;r=r+n%10;n=n/10;}

Page 28: Best C Programming Solution

printf (“reverse of a no. is %d”, r);getch ();}

Output: Enter any no. 123Reverse of a no. is 321.

41. W.A.P to find palindrome of a no.?# include <stdio.h># include <conio.h>Void main (){Int n, r=0, temp;Printf (“Enter any number”);Scanf (“%d”, &n);Temp=n;While (temp! =0){R=r*10;R=r+temp%10Temp=temp/10;}If (n==r)Printf (“No. is palindrome”);ElsePrintf (“No. is not palindrome”);Getch ();}

Output: Enter any number 121No. is palindrome

Or Enter any number 122

Page 29: Best C Programming Solution

No. is not palindrome

Difference b/w While Loop & Do While LoopWhile Loop Do While Loop

Condition is check first and then control goes into the loop.

Condition is checked but one time loop gets executed.

It is called entry control loops. It is called exit control loops.

42. Draw the Pattern: - 11111 22222 33333 44444 55555# include <stdio.h># include <conio.h>Void main (){Int i, j;Clrscr ();For (i=1; i<=5; i++){Printf (“\n”);For (j=1; j<=5; j++){Printf (“%d”, i);

Page 30: Best C Programming Solution

}}Getch ();}Output: 11111 22222 33333 44444 55555

43. Draw a pattern: - 1 22 333 4444 55555# include <stdio.h># include <conio.h>Void main (){Int i, j;Clrscr ();For (i=1; i<=5; i++){Printf (“\n”);For (j=1; j<=1; j++){Printf (“%d”, i);}

Page 31: Best C Programming Solution

}Getch ();}Output: 1 22 333 4444 55555

44. Draw the pattern: - 55555 4444 333 22 1# include <stdio.h># include <conio.h>Void main (){Int i, j;Clrscr ();For (i=5; i>=1; i--){Printf (“\n”);For (j=1; j<=I; j++){Printf (“%d”, i);}}

Page 32: Best C Programming Solution

Getch ();}Output: 55555 4444 333 22 1

45. Draw the pattern: - 12345 1234 123 12 1# include <stdio.h># include <conio.h>Void main (){Int i, j;Clrscr ();For (i=5; i>=1; i--){Printf (“\n”);For (j=1; j<=I; j++){Printf (“%d”, j);}}Getch ();

Page 33: Best C Programming Solution

}

Output: 12345 1234 123 12 1