0% found this document useful (0 votes)
2 views

Python Programming Unit – 2

The document provides an overview of decision statements and control structures in C programming, including simple if, if-else, else if ladder, nested if, switch statements, and loop control statements such as for, while, and do-while. It includes syntax, flowcharts, and examples for each type of statement, along with jump statements like break, continue, and goto. Additionally, it presents important programs demonstrating the Fibonacci series and factorial calculations.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Python Programming Unit – 2

The document provides an overview of decision statements and control structures in C programming, including simple if, if-else, else if ladder, nested if, switch statements, and loop control statements such as for, while, and do-while. It includes syntax, flowcharts, and examples for each type of statement, along with jump statements like break, continue, and goto. Additionally, it presents important programs demonstrating the Fibonacci series and factorial calculations.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 23

Dr. Md.

Fakhruddin Hasan Nizami Professor, Department of Mechanical Engineering


B.E., M. Tech. (R & A/C), B’Chain & C’Security, Ph.D. Methodist College of Engineering &
Technology

UNIT 2

Decision statements:
We have a number of situations where we may have to change the order of execution of statements
based on certain conditions or repeat a group of statements until certain specified conditions are met.

Simple if:
If statement is a two way decision statement and is used in conjunction with an expression. If the test
expression is true then the statement block after if is executed otherwise it is not executed.
Syntax:
if (test-expression)
{
Statements;
}
Flowchart:

Page 1
Dr. Md. Fakhruddin Hasan Nizami Professor, Department of Mechanical Engineering
B.E., M. Tech. (R & A/C), B’Chain & C’Security, Ph.D. Methodist College of Engineering &
Technology

Example:
Program to say which no is
greater. #include <stdio.h>
int main()
{
int a, b, c;
printf("Enter three numbers:
"); scanf("%d%d%d",
&a,&b,&c); if (a > b && a >
c)
printf("Biggest number is %d", a);
if (b > a && b > c)
printf("Biggest number is %d",
b); if (c > a && c > b)
printf("Biggest number is %d", c);
return 0;
}

if else:
If you have another set of statement to be executed if condition is false then if-else is used.
Syntax:
if (test-expression)
{
Statements;
}
else
{
Statements;
}

Page 2
Dr. Md. Fakhruddin Hasan Nizami Professor, Department of Mechanical Engineering
B.E., M. Tech. (R & A/C), B’Chain & C’Security, Ph.D. Methodist College of Engineering &
Technology

Flowchart:

Example:
Program to check if the number is odd or
even. #include <stdio.h>
int main()
{
int n;
printf(“Enter the number: ”);
scanf(“%d”,&n):
if (n%2==0)
printf(“ Number is even”);
else
printf(“Number is odd”);
return 0;
}

Page 3
Dr. Md. Fakhruddin Hasan Nizami Professor, Department of Mechanical Engineering
B.E., M. Tech. (R & A/C), B’Chain & C’Security, Ph.D. Methodist College of Engineering &
Technology

else if ladder:
The else if statement is a multi-way decision statement. If any test expression is true then the statement
block after if is executed and this terminates the whole chain.
Syntax:
if (test-expression)
{
Statements;
}
else if
{
Statements;
}
else
{
Statements;
}
Flowchart:

Page 4
Dr. Md. Fakhruddin Hasan Nizami Professor, Department of Mechanical Engineering
B.E., M. Tech. (R & A/C), B’Chain & C’Security, Ph.D. Methodist College of Engineering &
Technology

Example:
Program to check if number is divisible by 5 or
6. #include <stdio.h>
int main()
{
int n;
printf(“Enter a number:
”); scanf(“%d”,&n);
if (n%5==0)
printf(“ Number is divisible by
5”); if (n%6==0)
printf(“ Number is divisible by
6”); else
printf(“Number is not divisible by 5 or 6”);
return 0;
}

Nested if:
Nested if condition means if-within-if. There could be infinite if conditions inside an if condition.
Syntax:
if (test-expression)
{
if (test-expression)
{
Statements;
}
else
{
Statements;
}
}
else
{

Page 5
Dr. Md. Fakhruddin Hasan Nizami Professor, Department of Mechanical Engineering
B.E., M. Tech. (R & A/C), B’Chain & C’Security, Ph.D. Methodist College of Engineering &
Technology

Statements;
}
Flowchart:

Example:
Program to say which no is
greater. #include <stdio.h>
int main()
{
int a,b;
printf(“Enter two numbers”);
scanf(“%d%d”,&a,&b);
if (a>=b)
{
if (a>b)
printf(“ a is
greater”); else

Page 6
Dr. Md. Fakhruddin Hasan Nizami Professor, Department of Mechanical Engineering
B.E., M. Tech. (R & A/C), B’Chain & C’Security, Ph.D. Methodist College of Engineering &
Technology

printf(“Both are equal”);


}
else
printf(“b is greater”);
return 0;
}

Case statement – switch statement:


If for suppose we have more than one valid choices to choose from then we can use switch statement
in place of if statements.
Syntax:
switch(expression)
{
case value-1:
{
Statements;
break;
}
case value-2:
{
Statements;
break;
}
case value-n:
{
Statements;
break;
}
default:
}

Page 7
Dr. Md. Fakhruddin Hasan Nizami Professor, Department of Mechanical Engineering
B.E., M. Tech. (R & A/C), B’Chain & C’Security, Ph.D. Methodist College of Engineering &
Technology

Flowchart:

Example:
Program to Print Day Name of
Week. #include <stdio.h>
int main()
{
int n ;
printf(“Enter day(1-7) : ”);
scanf(“%d”,&n);
switch(n)
{
case 1 :
{
printf("sunday \n");
break;
}
case 2 :
{
printf("monday \n");
break;

Page 8
Dr. Md. Fakhruddin Hasan Nizami Professor, Department of Mechanical Engineering
B.E., M. Tech. (R & A/C), B’Chain & C’Security, Ph.D. Methodist College of Engineering &
Technology

}
case 3 :
{
printf("tuesday \n");
break;
}
case 4 :
{
printf("wednesday \n");
break;
}
case 5 :
{
printf("thursday \n");
break;
}
case 6 :
{
printf("friday \n");
break;
}
case 7 :
{
printf("Saturday \n");
break;
}
default :
{
printf("Not a valid day! \n");
}

Page 9
Dr. Md. Fakhruddin Hasan Nizami Professor, Department of Mechanical Engineering
B.E., M. Tech. (R & A/C), B’Chain & C’Security, Ph.D. Methodist College of Engineering &
Technology

Loop control statements:


Sometimes we require a set of statements to be executed repeatedly until a condition is met. We have
two types of looping structures. One in which condition is tested before entering the statement block
called entry control. The other in which condition is checked at exit called exit controlled loop.

for:
It is an entry control loop that provides a more concise structure.
Syntax:
for (initialization; test-expression; iteration)
{
Statements
}
Flowchart:

Example:
Program to give table of any number input by the
user. #include <stdio.h>

Page 10
Dr. Md. Fakhruddin Hasan Nizami Professor, Department of Mechanical Engineering
B.E., M. Tech. (R & A/C), B’Chain & C’Security, Ph.D. Methodist College of Engineering &
Technology

int main()
{
int n,i,t ;
printf(“Enter number: ”);
scanf(“%d”,&n);
for ( i=1 ; i<=10 ; i++ )
{
t=n*i;
printf(“%d x %d = %d \n” , n,i,t);
}
return 0;
}

while:
It is an entry controlled loop. The condition is evaluated and if it is true then body of loop is
executed. After execution of body the condition is once again evaluated and if is true, body is
executed once again. This goes on until test condition becomes false.

Syntax:
while(test condition)
{
Statements;
}

Flowchart:

Page 11
Dr. Md. Fakhruddin Hasan Nizami Professor, Department of Mechanical Engineering
B.E., M. Tech. (R & A/C), B’Chain & C’Security, Ph.D. Methodist College of Engineering &
Technology

Example:
Program to print even number till n
nos. #include <stdio.h>
int main()
{
int i=1, number;
printf("Enter the Limit Value : ");
scanf("%d", &number);
printf("Even Numbers between 1 and %d are :",
number); while (i <= number)
{
if ( i % 2 == 0 )
{
printf(" %d", i);
} i+
+;

Page 12
Dr. Md. Fakhruddin Hasan Nizami Professor, Department of Mechanical Engineering
B.E., M. Tech. (R & A/C), B’Chain & C’Security, Ph.D. Methodist College of Engineering &
Technology

}
return 0;
}

do while:
The while loop does not allow body to be executed if test condition is false. The do while is an exit
controlled loop and its body is executed at least once.
Syntax:
do
{
body
}
while(test condition);

Flowchart:

Page 13
Dr. Md. Fakhruddin Hasan Nizami Professor, Department of Mechanical Engineering
B.E., M. Tech. (R & A/C), B’Chain & C’Security, Ph.D. Methodist College of Engineering &
Technology

Example:
Program to print the Fibonacci series using do while loop.
#include<stdio.h>
int main()
{
int n,f,f1=-1,f2=1;
printf(" Enter The Number Of Terms:");
scanf("%d",&n);
do
{
f=f1+f2;
f1=f2;
f2=f;
printf(" \n %d",f);
n--;
}
while(n>=0);
return 0;
}

Jump statements:
Jump statement in c is used to interpret the flow of the program or escape a particular section of the
program. There are many more operations they can perform within the loops, switch statements, and
functions. There are few types of jump statements.

Break:
Break statement in c is a loop control statement that is used to terminate the loop. There are two
usages: Inside a Loop: If the break statement is using inside a loop along with the if statement then if
the condition becomes true the loop is immediately terminated and the next statement after the loop
starts executing by the program control.

Page 14
Dr. Md. Fakhruddin Hasan Nizami Professor, Department of Mechanical Engineering
B.E., M. Tech. (R & A/C), B’Chain & C’Security, Ph.D. Methodist College of Engineering &
Technology

Inside a Switch Case: If Break Statement in C is using inside a switch case after each switch case then
the break statement terminates a case after executing the case.

Syntax:
break;
Flowchart:

Example:
#include <stdio.h>
int main()
{
int c;
for (c=0 ; c<10 ; c++)
{
printf(“%d\n”,c);
if (c==6)
break;
}
return 0;
}
Output:

Page 15
Dr. Md. Fakhruddin Hasan Nizami Professor, Department of Mechanical Engineering
B.E., M. Tech. (R & A/C), B’Chain & C’Security, Ph.D. Methodist College of Engineering &
Technology

1 2 3 4 5 6

Continue:
The continue statement in C programming works somewhat like the break statement. Instead of
forcing termination, it forces the next iteration of the loop to take place, skipping any code in
between.
For the for loop, continue statement causes the conditional test and increment portions of the loop to
execute. For the while and do...while loops, continue statement causes the program control to pass to
the conditional tests.

Syntax:
continue;
Flowchart:

Example:
int main()
{
for (int i = 1; i <= 10; i++)
{

Page 16
Dr. Md. Fakhruddin Hasan Nizami Professor, Department of Mechanical Engineering
B.E., M. Tech. (R & A/C), B’Chain & C’Security, Ph.D. Methodist College of Engineering &
Technology

if (i == 6)

Page 17
Dr. Md. Fakhruddin Hasan Nizami Professor, Department of Mechanical Engineering
B.E., M. Tech. (R & A/C), B’Chain & C’Security, Ph.D. Methodist College of Engineering &
Technology

continue;
else
printf("%d \t ", i);
}
return 0;
}
Output:
1 2 3 4 5 7 8 9 10

Goto:
The goto statement is known as jump statement in C. goto is used to transfer the program control to a
predefined label. The goto statment can be used to repeat some part of the code for a particular
condition. It can also be used to break the multiple loops which can't be done by using a single break
statement.
Syntax:
label:
//some part of the code;
goto label;
Flowchart:

Example:
#include <stdio.h>
int main()

Page 18
Dr. Md. Fakhruddin Hasan Nizami Professor, Department of Mechanical Engineering
B.E., M. Tech. (R & A/C), B’Chain & C’Security, Ph.D. Methodist College of Engineering &
Technology

{
int num,i=1;
printf("Enter the number whose table you want to
print?"); scanf("%d",&num);
table:
printf("%d x %d = %d\
n",num,i,num*i); i++;
if(i<=10)
goto table;
}

Important programs:
1) Program to print the Fibonacci series.
#include <stdio.h>
int main()
{
int n,f1,f2,f3,i=2;
printf("Enter the nth term of the sequence:
"); scanf("%d",&n);
f1=0;
f2=1;
printf("%d\n",f1);
printf("%d\n",f2);
while (i<=n)
{
f3=f1+f2;
printf("%d\n",f3);
f1=f2;
f2=f3;
i++;
}
return 0;
}

Page 19
Dr. Md. Fakhruddin Hasan Nizami Professor, Department of Mechanical Engineering
B.E., M. Tech. (R & A/C), B’Chain & C’Security, Ph.D. Methodist College of Engineering &
Technology

2) Program to print the Factorial of a number.


#include <stdio.h>
int main()
{
int n, i;
unsigned long long fact = 1;
printf("Enter an integer: ");
scanf("%d", &n);
if (n < 0)
printf("Error! Factorial of a negative number doesn't
exist."); else
{
for (i = 1; i <= n; ++i)
{
fact *= i;
}
printf("Factorial of %d = %llu", n, fact);
}
return 0;
}

3) Program to print the sine series.


#include<stdio.h>
int main()
{
int i, n;
float x, sum, t;
printf(" Enter the value for x : ");
scanf("%f",&x);
printf(" Enter the value for n : ");
scanf("%d",&n);

Page 20
Dr. Md. Fakhruddin Hasan Nizami Professor, Department of Mechanical Engineering
B.E., M. Tech. (R & A/C), B’Chain & C’Security, Ph.D. Methodist College of Engineering &
Technology

// Converting ‘x’ to radian


value// x=x*3.14159/180;
t=x;
sum=x;
// Loop to calculate the value of Sine
// for(i=1;i<=n;i++)
{
t=(t*(-1)*x*x)/(2*i*(2*i+1));
sum=sum+t;
}
printf(" The value of Sin(%f) =
%.4f",x,sum); return 0;
}
4) Program to print the cosine series.
#include<stdio.h>
int main()
{
int i, n;
float x, sum=1, t=1;
printf(" Enter the value for x : ");
scanf("%f",&x);
printf(" Enter the value for n : ");
scanf("%d",&n);
//Converting ‘x’ to radian
value// x=x*3.14159/180;
// Loop to calculate the value of Cosine
// for(i=1;i<=n;i++)
{
t=t*(-1)*x*x/(2*i*(2*i-1));
sum=sum+t;
}
printf(" The value of Cos(%f) is : %.4f", x,
sum); return 0;

Page 21
Dr. Md. Fakhruddin Hasan Nizami Professor, Department of Mechanical Engineering
B.E., M. Tech. (R & A/C), B’Chain & C’Security, Ph.D. Methodist College of Engineering &
Technology

5) Program to find the sum of individual digits of a number.


#include<stdio.h>
int main()
{
int n,rem,sum=0;
printf(" Enter a number: ");
scanf("%d",&n);
while (n>0)
{
rem=n%10;
sum=sum+rem;
n=n/10;
}
printf("sum=%d",sum);
return 0;
}

6) Program to find the number is palindrome or not.


#include<stdio.h>
int main()
{
int n,rem,rev=0,a;
printf(" Enter a number: ");
scanf("%d",&n);
a=n;
while (n!=0)
{
rem=n%10;
rev=rev*10+rem;
n=n/10;
}

Page 22
Dr. Md. Fakhruddin Hasan Nizami Professor, Department of Mechanical Engineering
B.E., M. Tech. (R & A/C), B’Chain & C’Security, Ph.D. Methodist College of Engineering &
Technology

if (a==rev)
printf("Palindrome");
else
printf("Not Palindrome");
return 0;
}

7) Program to find the number is Armstrong or


#include<stdio.h>
int main()
{
int n,rem,sum=0,a;
printf(" Enter a number: ");
scanf("%d",&n);
a=n;
while (n!=0)
{
rem=n%10;
sum=sum+rem*rem*rem;
n=n/10;
}
if (a==sum)
printf("Armstrong number");
else
printf("Not an Armstrong number");
return 0;
}

********

Page 23

You might also like