Day_2 C-Programming
Day_2 C-Programming
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.
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;
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.
(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();
}
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).
#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();
}
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.
24. Write a program to check weather a given number is prime number or not.
26. Write a program to find the Sum of all prime numbers from 1-1000
*****
*****
*****
*****
*****
**
***
****
*****
12
123
1234
12345
22
333
4444
55555
BB
CCC
DDDD
EEEEE
*****
****
***
**
1234
123
12
***
*****
*******
*********
23
456
7 8 9 10
BAB
ABABA
BABABAB
010
10101
0101010
ABCDEFGFEDCBA
ABCDEF FEDCBA
ABCDE EDCBA
ABCD DCBA
ABC CBA
AB BA
A A
**** ****
*** ***
** **
* *
01
010
0101
01010
BC
DEF
GHIJ
KLMNO
BAB
CBABC
DCBABCD
EDCBABCDE
1A2B3C4D5E
1A2B3C4D
1A2B3C
1A2B
1A
101
21012
3210123
432101234
0, 2, 4, 6, 8, 10
1, 2, 4, 8, 16, 32
1 + 2 + 3 + 4 + 5 + ... + n
1! + 2! + 3! + 4! + 5! + ... + n!