Day_2 C-Programming
Conditional Program Execution
(RECAP OF PREVIOUS DAY)
if, else-if, nested if-else, switch statements, use of break, and default with
switch, Concept of loops, for, while and do while, multiple loop variables, use
of break and continue statements, nested loop.
Conditional Statements: In C Programming we want the program to be executed sequentially.
We want a set of instruction to be executed at one time and an entirely set of instruction to be
executed in another situation. In this type of situation will have to use the decision control structure.
In C Language decision making capabilities are supported by following statements.
if statement
switch statement
Conditional Operator
if statements: The if statement is a decision making statements. It is two-way decision statement
and used in combination with an expression.
Syntax:
if(test expression)
{
Statement-block;
}
Statement-X;
if else Statement: In this, the body of if is executed if the result of expression is true, otherwise the
else block is executed and then the control is transferred to the next statement.
Syntax:
if(test expression)
{
statement block-1;
}
else
{
statement block-2;
}
statement-x;
Nested if else Statement: When an if statement is written inside another if statement, it is called
a nested if statement.
Syntax:
if(test expression-1)
{
if(test expression-2)
{
statement block-1;
}
else
{
statement block-2;
}
}
else
{
if(test expression-3)
{
statement block-3;
}
else
{
statement block-4;
}
}
Statement-x;
else if Ladder: It is a multiway branching statement. It start by checking the first logical expression.
If the expression is true, then the body of first if is executed. Otherwise the next expression is
evaluated and so on. When an expression is found to be true, the statements associated with it are
executed and then the control is transferred.
Syntax:
if(test expression-1)
{
Statement block-1
}
else if(test expression-2)
{
Statement block-2
}
else if(test expression-3)
{
Statement block-3
}
else
{
statement block-4
}
Statement-x;
Write a program to check whether a number is even or odd.
#include<stdio.h>
#include<conio.h>
void main()
{
int n;
printf(“Enter a Number”);
scanf(“%d”,&n);
if(n%2==0)
printf(“Number is Even”);
else
printf(“Number is odd”);
getch();
}
Write a program to check whether a year is a leap year or not.
#include<stdio.h>
#include<conio.h>
void main()
{
int y;
printf(“Enter a Year”);
scanf(“%d”,&y);
if((y%4==0 && y%100!=0)||(y%400==0))
printf(“Year is a leap year”);
else
printf(“Year is not a leap year”);
getch();
}
Write a program to convert an uppercase letter into lowercase and vice-versa.
#include<stdio.h>
#include<conio.h>
void main()
{
char ch;
printf(“Eneter a character”);
scanf(“%c”,&ch);
if(ch>=’A’ && ch<=’Z’ )
{
ch=ch+32;
printf(“%c”,ch);
}
else if(ch>=’a’ && ch<=’z’)
{
ch=ch-32;
printf(“%c”,ch);
}
else
printf(“Invalid Character”);
getch(); }
Write a program to find the largest number among three numbers.
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
printf(“Enter three Numbers”);
scanf(“%d%d%d”,&a,&b,&c);
if(a>b && a>c)
printf(“%d is largest”,a);
else if(b>a && b>c)
printf(“%d is largest”,b);
else
printf(“%d is largest”,c);
getch();
}
Write a program to check whether a number is positive, negative or zero.
#include<stdio.h>
#include<conio.h>
void main()
{
int n;
printf(“Enter Number”);
scanf(“%d”,&n);
if(n>0)
printf(“Number is Positive”);
else if(n<0)
printf(“Number is Positive”);
else
printf(“Number is Zero”);
getch();
}
Write a program to find the largest number among three numbers
#include<stdio.h>
#include<conio.h>
void main()
{
int a, b, c, x;
printf(“Enter three numbers”);
scanf(“%d%d%d”,&a,&b,&c);
x=a>b?a>c:a:c:b>c?b:c;
printf(“Largest number=%d”,x);
getch();
}
Switch Statement: The switch statement is a multiway decision statement. The switch
statement test the value of a given variable or expression against a list of case values and when a
match is found a block of statement associated with that case is executed.
Syntax: switch(expression)
{
caes value1: block-1;
break;
caes value2: block-2;
break;
caes value3: block-3;
break;
default: block-1;
break;
}
Advantages of Switch
(i). Easier to debug.
(ii). Easier to read, understand, and maintain.
(iii).Faster execution.
(iv).More efficient (destination can be computed by looking up in table).
Limitations (Disadvantages) of switch statement
(i). It doesn't work with floats, strings, etc.
(ii). Relational and logical operators are not allowed in switch case.
(iii).It doesn't work with variable conditions i.e. case values cannot be variable.
Write a program to simulate calculator using switch statement.
#include<stdio.h>
#include<conio.h>
void main()
{
float a,b,c;
char op;
printf(“Enter two numbers”);
scanf(“%f%f”,&a,&b);
fflush(stdin);
printf(“Enter Operator”);
scanf(“%c”,&op);
switch(op)
{
case ‘+’: c=a+b;
printf(“Addition=%f”,c);
break;
case ‘-’: c=a-b;
printf(“Subtraction=%f”,c);
break;
case ‘*’: c=a*b;
printf(“Multiplication=%f”,c);
break;
case ‘/’: c=a/b;
printf(“Division=%f”,c);
break;
default: printf(“Invalid Operator”);
break;
}
getch();
}
Write a menu driven program to calculate the area of different geometrical figures such as
rectangle, square, circle and triangle.
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
float a,b,c,s,area;
int n;
printf(“Enter:\n1:For rectangle\n2:For square\n3:For circle\n4:For triangle\n”);
scanf(“%d”,&n);
switch(n)
{
case 1: printf(“Enter length and breadth of rectangle”);
scanf(“%f%f”,&a,&b);
area=a*b;
printf(“Area of Rectanle:%f”,area);
break;
case 2: printf(“Enter the side of square”);
scanf(“%f”,&a);
area=a*a;
printf(“Area of Square:%f”,area);
break;
case 3: printf(“Enter the radius of circle”);
scanf(“%f”,&a);
area= 3.14*a*a;
printf(“Area of Circle:%f”,area);
break;
case 4: printf(“Enter the three sides of triangle ”);
scanf(“%f%f%f”,&a,&b,&c);
s=(a+b+c)/2;
area=sqrt(s*(s-a)*(s-b)*(s-c));
printf(“Area of Triangle:%f”,area);
break;
default: printf(“Invalide Choice”);
break;
}
getch();
}
Difference between switch and if statement
switch if
1) In switch statement the condition is tested 1) In multiple if statements the conditions are to
only once and jumps to required block. be checked as many times the if statements
are written.
2) A switch statement is generally best to use
when you have more than two conditional 2) A if else statement can take any kind of value
expressions based on a single variable of in conditional expression.
numeric & character type.
3) All operators are allowed in if else statement.
3) Logical operators are not allowed in case.
Looping: In looping, a sequence of statements are executed until some condition for termination
of the loop are satisfied.
A program loop consist of two parts- one known as control statement and other known as body of
loop. Depending on position of control statements there are two types of loop.
(1) Entry controlled loop
(2) Exit controlled loop
(1) Entry controlled loop: In this type of loop the control conditions are tested before the
body of loop is executed. Example: for and while.
(2) Exit controlled loop: In this type of loop the control conditions are tested at the end of the
body of loop. Example: do while.
for loop: It is an entry controlled loop. This loop is used when the number of iterations are
known in advance.
Syntax: for(initialization; condition; increment/decrement)
{
Body of loop;
}
while loop: It is an entry controlled loop. This loop is used when the number of iterations
are not known in advance.
Syntax: while(condition)
{
Body of loop;
}
do while loop: It is an exit controlled loop. In this loop the body of loop is always executed at
least one time even if the condition is false for the first time .
Syntax: do
{
Body of loop;
}
while(condition);
Differentiate among for, while and do while loop
for loop while loop do-while loop
(i). It is an entry controlled (i). It is an entry controlled (i). It is an exit controlled loop.
loop. loop.
(ii). Its body may not be (ii). Its body may not be (ii). Its body is always executed
executed even once if the executed even once if the at least once even if the
condition is false. condition is false. condition is false initially.
(iii). It is used when the (iii). It is used when the number (iii). It is used when the number
number of iterations is of iterations is not known in of iterations is not known in
known in advance. advance. advance.
Write a program to find the sum of digits of an integer number.
#include<stdio.h>
#include<conio.h>
void main()
{
int n,d,s=0;
printf(“Eneter the number”);
scanf(“%d”,&n);
while(n>0)
{
d=n%10;
s=s+d;
n=n/10;
}
printf(“The Sum of digits:=%d”,s);
getch();
}
Write a program to find the sum of digit square of an integer number.
#include<stdio.h>
#include<conio.h>
void main()
{
int n,d,s=0;
printf(“Eneter the number”);
scanf(“%d”,&n);
while(n>0)
{
d=n%10;
s=s+d*d;
n=n/10;
}
printf(“The Sum of Digits Square:=%d”,s);
getch();
}
Write a program to find the reverse of an integer number.
#include<stdio.h>
#include<conio.h>
void main()
{
int n,d,s=0;
printf(“Eneter the number”);
scanf(“%d”,&n);
while(n>0)
{
d=n%10;
s=s*10+d;
n=n/10;
}
printf(“The Reverse of Number:=%d”,s);
getch();
}
Write a program to check whether an integer number is palindrome or not.
#include<stdio.h>
#include<conio.h>
void main()
{
int n,d,s=0,t;
printf(“Eneter the number”);
scanf(“%d”,&n);
t=n;
while(n>0)
{
d=n%10;
s=s*10+d;
n=n/10;
}
if(s==t)
printf(“Number is Palindrome Number”);
else
printf(“Number is not Palindrome Number”);
getch();
}
Write a program to check whether an integer number is Armstrong or not.
#include<stdio.h>
#include<conio.h>
void main()
{
int n,d,s=0,t;
printf(“Eneter the number”);
scanf(“%d”,&n);
t=n;
while(n>0)
{
d=n%10;
s=s+d*d*d;
n=n/10;
}
if(s==t)
printf(“Number is Armstrong Number”);
else
printf(“Number is not Armstrong Number”);
getch();
}
Write a program to check whether an integer number is Prime number or not.
#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,c=0;
printf(“Eneter the number”);
scanf(“%d”,&n);
for(i=1;i<=n;i++)
{
if(n%i==0)
c++;
}
if(c==2)
printf(“Number is Prime Number”);
else
printf(“Number is not Prime Numbe”);
getch();
}
Write a program to print all Prime number between 1 and 100.
#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,c;
for(n=2;n<100;n++)
{
c=0;
for(i=1;i<=n;i++)
{
if(n%i==0)
c++;
}
if(c==2)
printf(“%d\t”,n);
}
getch();
}
Unconditional or Jumping Statements: The jump statements unconditionally transfer the
control from one place to another place in a program. The jumping statements are-
1. break
2. continue
3. goto
4. return
5. exit
break Statement: The break statement transfers the control to outside a loop or switch
statement.
continue Statement: The continue statement transfers the control to the beginning of loop. It
transfer the control to the increment/decrement in for loop and to the condition in while and do
while loop.
Difference between break and continue statements
break continue
1. It is used to exit from switch case or loop. 1. It is used to go to the beginning of a loop.
2. It transfer the control to the first 2. It transfer the control to the iteration in
statement after the loop. for loop and to the condition in while and
do while loop.
3. It is used with loop and switch. 3. It is used with loop.
Example: Example:
void main() void main()
{ {
int i; int i;
for(i=1;i<=10;i++) for(i=1;i<=10;i++)
{ {
if(i==5) if(i==5)
break; continue;
printf(“%d”,i); printf(“%d”,i);
} }
getch(); getch();
} }
goto Statement: The goto statement is used to transfer the control from one place to another
in a program. This statement uses a label.
Syntax:
Statement-1;
if(condition)
{
goto label;
}
Statement-2;
label:
Statement-3;
Where goto is a keyword and label is an identifier (user defined name).
Write a program to print 1 to 10 numbers using goto statement.
#include<stdio.h>
#include<conio.h>
void main()
{
int i=1;
start:
printf(“%d”,i);
if(i<10)
{
i++;
goto start;
}
getch();
}
Write a program to calculate the square root of a number.
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
float n,s;
read:
printf(“Enter a number”);
scanf(“%d”,&n);
if(n<0)
goto read;
s=sqrt(n)
printf(“The square root of %f=%f”,n,s);
getch();
}
return: This statement is used to transfer the control from the called function to the calling
function with or without result.
exit(): This function is used to exit the control from the program execution.
Write a program to find the sum of following series:
(i) 11 + 22 + 33 + 44 + ----------nn.
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int n,i,s=0;
printf(“Eneter the number of terms”);
scanf(“%d”,&n);
for(i=1;i<=n;i++)
{
s=s+pow(i,i);
}
printf(“The Sum of the Series:=%d”,s);
getch();
}
(ii) X1 + X2 + X3 + X4 + ----------Xn.
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int n,i,s=0;
printf(“Eneter the number and range”);
scanf(“%d%d”,&x,&n);
for(i=1;i<=n;i++)
{
s=s+pow(x,i);
}
printf(“The Sum of the Series:=%d”,s);
getch();
}
(iii) 1 + 2 – 3 + 4 – 5---------n.
#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,s=1,sign= -1;
printf(“Eneter the range”);
scanf(“%d”,&n);
for(i=2;i<=n;i++)
{
sign=sign*(-1);
s=s+(i*sign);
}
printf(“The Sum of the Series:=%d”,s);
getch();
}
(iv) X1 + X2 – X3 + X4 + X5--------- Xn.
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int n,i,s,x,sign= -1;
printf(“Eneter the range and value of x”);
scanf(“%d%d”,&n,&x);
for(i=2;i<=n;i++)
{
sign=sign*(-1);
s=s+(pow(x,i)*sign);
}
printf(“The Sum of the Series:=%d”,s);
getch();
}
(v) 1/1! + 2/2! + 3/3! + ------------n/n!.
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int n,i;
float s=0;
printf(“Eneter the range”);
scanf(“%d”,&n);
for(i=1;i<=n;i++)
{
s=s+(i/(float)fact(i));
}
printf(“The Sum of the Series:=%f”,s);
getch();
}
int fact(int n)
{
if(n==0)
return 1;
else
return(n*fact(n-1));
}
Write a program to print the following sequence of integers.
(i) 0,5,10,15,20,25
#include<stdio.h>
#include<conio.h>
void main()
{
int i;
for(i=0;i<=25;i=i+5)
{
printf(“%d,”,i);
}
getch();
}
(ii) 0,2,4,6,8,10
#include<stdio.h>
#include<conio.h>
void main()
{
int i;
for(i=0;i<=10;i=i+2)
{
printf(“%d,”,i);
}
getch();
}
(iii) 1,2,4,8,16,32
#include<stdio.h>
#include<conio.h>
void main()
{
int i;
for(i=0;i<=32;i=i*2)
{
printf(“%d,”,i);
}
getch();
}
(iv) -8 -6 -4 -2 0 2 4 6 8
#include<stdio.h>
#include<conio.h>
void main()
{
int i;
for(i=-8;i<=8;i=i+2)
{
printf(“%d”,i);
}
getch();
}
Nested Loop: A loop which is inside the body of another loop is called nested loop.
Write a program to print following patterns.
(i). *
* *
* * *
* * * *
* * * * *
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j;
for(i=1;i<=5;i++)
{
for(j=1;j<=i;j++)
{
printf(“*”);
}
printf(“\n”);
}
getch();
}
(ii). A
B A
A B A
B A B A
A B A B A
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j;
for(i=1;i<=5;i++)
{
for(j=1;j<=i;j++)
{
if((i+j)%2==0)
printf(“A”);
else
printf(“B”);
}
printf(“\n”);
}
getch();
}
(iii). 1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j;
for(i=1;i<=5;i++)
{
for(j=1;j<=i;j++)
{
printf(“%d\t”,i);
}
printf(“\n”);
}
getch();
}
(iv).5
5 4
5 4 3
5 4 3 2
5 4 3 2 1
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j;
for(i=5;i>=1;i--)
{
for(j=1;j<=i;j++)
{
printf(“%d\t”,i);
}
printf(“\n”);
}
getch();
}
(v). *
***
*****
*******
*********
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,k;
for(i=1;i<=5;i++)
{
for(j=5;j>i;j--)
printf(“ ”);
for(k=1;k<=2*i-1;k++)
printf(“*”);
printf(“\n”);
}
getch();
}
List of programs to practice (Home work)
1. Write a program to display numbers 1 to 10.
2. Write a program to display all even numbers from 1 to 20
3. Write a program to display all odd numbers from 1 to 20
4. Write a program to print all the Numbers Divisible by 7 from 1 to 100.
5. Write a program to print table of 2.
6. Write a program to print table of 5.
7. Write a program to print table of any number.
8. Write a program to print table of 5 in following format.
5X1=5
5 X 2 = 10
5 X 3 = 15
9. Write a program to Find the Sum of first 50 Natural Numbers using for Loop.
10. Write a program to calculate factorial of a given number using for loop.
11. Write a program to calculate factorial of a given number using while loop.
12. Write a program to calculate factorial of a given number using do-while loop.
13. Write a program to count the sum of digits in the entered number.
14. Write a program to find the reverse of a given number.
15. Write a program to Check whether a given Number is Perfect Number.
16. Write a program to Print Armstrong Number from 1 to 1000.
17. Write a program to Compute the Value of X ^ N
18. Write a program to Calculate the value of nCr
19. Write a program to generate the Fibonacci Series
20. Write a program to Print First 10 Natural Numbers
21. Write a program to check whether a given Number is Palindrome or Not
22. Write a program to Check whether a given Number is an Armstrong Number
23. Write a program to Check Whether given Number is Perfect or Not
24. Write a program to check weather a given number is prime number or not.
25. Write a program to print all prime numbers from 50-500
26. Write a program to find the Sum of all prime numbers from 1-1000
27. Write a program to display the following pattern:
*****
*****
*****
*****
*****
28. Write a program to display the following pattern:
**
***
****
*****
29. Write a program to display the following pattern:
12
123
1234
12345
30. Write a program to display the following pattern:
22
333
4444
55555
31. Write a program to display the following pattern:
BB
CCC
DDDD
EEEEE
32. Write a program to display the following pattern:
*****
****
***
**
33. Write a program to display the following pattern:
12345
1234
123
12
34. Write a program to display the following pattern:
***
*****
*******
*********
35. Write a program to display the following pattern (Pascal Triangle):
41. Write a program to display the following pattern:
23
456
7 8 9 10
36. Write a program to display the following pattern:
BAB
ABABA
BABABAB
37. Write a program to display the following pattern:
010
10101
0101010
38. Write a program to display the following pattern:
ABCDEFGFEDCBA
ABCDEF FEDCBA
ABCDE EDCBA
ABCD DCBA
ABC CBA
AB BA
A A
39. Write a program to display the following pattern:
**********
**** ****
*** ***
** **
* *
40. Write a program to display the following pattern:
01
010
0101
01010
41. Write a program to display the following pattern:
42. Write a program to display the following pattern:
BC
DEF
GHIJ
KLMNO
43. Write a program to display the following pattern:
A
BAB
CBABC
DCBABCD
EDCBABCDE
44. Write a program to display the following pattern:
1A2B3C4D5E
1A2B3C4D
1A2B3C
1A2B
1A
45. Write a program to display the following pattern:
101
21012
3210123
432101234
46. Write a program to print the following sequence of integers.
0, 5, 10, 15, 20, 25
47. Write a program to print the following sequence of integers.
0, 2, 4, 6, 8, 10
48. Write a program to Find the Sum of A.P Series.
49. Write a program to Find the Sum of G.P Series.
50. Write a program to Find the Sum of H.P Series.
51. Write a program to print the following sequence of integers.
1, 2, 4, 8, 16, 32
52. Write a program to print the following sequence of integers.
-8, -6, -4, -2, 0, 2, 4, 6, 8
53. Write a program to find the Sum of following Series:
1 + 2 + 3 + 4 + 5 + ... + n
54. Write a program to find the Sum of following Series:
(1*1) + (2*2) + (3*3) + (4*4) + (5*5) + ... + (n*n)
55. Write a program to find the Sum of following Series:
(1) + (1+2) + (1+2+3) + (1+2+3+4) + ... + (1+2+3+4+...+n)
56. Write a program to find the Sum of following Series:
1! + 2! + 3! + 4! + 5! + ... + n!
57. Write a program to find the Sum of following Series:
(1^1) + (2^2) + (3^3) + (4^4) + (5^5) + ... + (n^n)
58. Write a program to find the Sum of following Series:
(1!/1) + (2!/2) + (3!/3) + (4!/4) + (5!/5) + ... + (n!/n)
59. Write a program to find the Sum of following Series:
[(1^1)/1] + [(2^2)/2] + [(3^3)/3] + [(4^4)/4] + [(5^5)/5] + ... + [(n^n)/n]
60. Write a program to find the Sum of following Series:
[(1^1)/1!] + [(2^2)/2!] + [(3^3)/3!] + [(4^4)/4!] + [(5^5)/5!] + ... + [(n^n)/n!]
61. Write a program to find the Sum of following Series:
1/2 - 2/3 + 3/4 - 4/5 + 5/6 - ...... upto n terms
62. Write a program to print the following Series:
1, 2, 3, 6, 9, 18, 27, 54, ... upto n terms
63. Write a program to print the following Series:
2, 15, 41, 80, 132, 197, 275, 366, 470, 587
64. Write a program to print the following Series:
1, 3, 8, 15, 27, 50, 92, 169, 311