Best C Programming Solution

yoginisharma1 777 views 33 slides Jul 26, 2016
Slide 1
Slide 1 of 33
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28
Slide 29
29
Slide 30
30
Slide 31
31
Slide 32
32
Slide 33
33

About This Presentation

Are u searching c programming solution? Now your search ends here. Yogini Sharma provides best solution in c and c++.
Batra Computer Center


Slide Content

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

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: Hello
2. 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 ();
}

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=3
B=4
C=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);
S=p*r*t/100;

printf (“\n Simple Interest is of %d”, s);
getch ();
}
Output: put value of p=1000, r=2, t=1
Simple 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=4
Answer 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;
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=80
Answer: - 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=6
Answer is: - Max No. is 9

9. W.A.P to enter any no. & click whether it is even or odd?
# include <stdio.h>
# 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=8
Even 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 ()
{

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;
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
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 7
7 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 ();
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: - 18
Can 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 ();
}

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=67
B 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 ()
{

char alpha;
printf (“enter any alphabet”);
scanf (“%c”, &alphabet”);
if (alpha==’a’||alpha==’e’||alpha==’i’||alpha==’o’||alpha==’u’)
printf (“vowel”);
else
printf (“Consonant”);
getch ();
}
Output: Enter any alphabet
R= 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”);
else
printf (“old”);
getch ();
}
Output: Enter any age=56
Old

21. 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”);
Else
Printf (“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 ();
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 (“1
st
position”);
else if (p>60 && p<70)
printf (“2
nd
position”);
else if (p>30 && p<60)
printf (“3
rd
position”);
else
printf (“fail”);
getch ();
}
Output: Enter the value of m1=1, m2=2, m3=3, m4=4, m5=5
Fail

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”);
else
printf (“c is max”);
}

else
{
if (b>c)
printf (“b is max”);
else
printf (“c is max”);
}
else
[
if (b>c)
printf (“b is max”);
else
printf (“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;
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: R
Roses 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);
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=8
Enter 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);
break;

default: printf (“wrong choice”);
}
getch ();
}
Output: Enter your choice 1
Enter 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

Consonant
28. 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”);

}
Getch ();
}
Output: Enter your choice 12
Winter

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++)

{
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 ();

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)

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;
}

printf (“%d”, sum);
getch ();
}
Output: Enter the value of n=4
Answer: 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. =4
Fact. Of a no is 0.

36. W.A.P to make a table of any no.?
# include <stdio.h>
# include <conio.h>
void main ()
{
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 2
2*1=2
2*2=4
2*3=6
2*4=8
2*5=10
2*6=12
2*7=14
2*8=16
2*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 ();
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 ()
{
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 = 40
Sum 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;
}
printf (“reverse of a no. is %d”, r);

getch ();
}
Output: Enter any no. 123
Reverse 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%10
Temp=temp/10;
}
If (n==r)
Printf (“No. is palindrome”);
Else
Printf (“No. is not palindrome”);
Getch ();
}
Output: Enter any number 121
No. is palindrome
Or
Enter any number 122
No. is not palindrome

Difference b/w While Loop & Do While Loop
While 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);
}
}

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);
}
}
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);
}
}
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 ();
}

Output: 12345
1234
123
12
1
Tags