Module II Answers
Module II Answers
1. a)simple if
b) if-else
c) else-if ladder
Conditional statements or Decision making statements help you to make a decision based on
certain conditions. These conditions are specified by a set of conditional statements having
boolean expressions which are evaluated to a boolean value true or false.
Simple if:
‘Simple if’ also known as One way or Single way selection statements
If a statement in C is used to control the program flow based on some condition, it’s used to
execute some statement code block if expression is evaluated to true, otherwise it will get
skipped. This is a simplest way to modify the control flow of the program.
Syntax:
if(expression)
//statement-block
The statement-block may be a single statement or a group of statements. If the expression is true
the statement-block will be executed. Otherwise the statement-block will be skipped and the
execution will jump out of the if expression.
#include<stdio.h>
main()
{
int num;
printf("enter a number: ");
scanf("%d",&num);
if(num%2==0)
{
printf("%d is even number",num);
}
}
OutPut:
Enter a number: 10
10 is even number
If-else
The if-else statement in C language is used to execute the code if condition is true or false. The
syntax of if-else statement is given below:
Syntax: if
(expression)
{
statement_block(1);
}
else
{
statement_block(2);
}
If the expression is true statement-block1 will be executed. Otherwise statement block2 will be
executed. In both cases control is transferred subsequent to statements after if statement.
Example:
#include<stdio.h>
main()
{
int num;
printf("enter a number:");
scanf("%d",&num);
if(num%2==0)
{
printf("%d is even number",num);
}
else
{
printf("%d is odd number",num);
}
}
OutPut:
Enter a number:15
15 is even number
Else-If ladder
In Multi way selection statements is used to execute one code from multiple
conditions.
In C language we have the following multi way selection statements.
Else if leader statement
Switch Statement.
The Else If Statement in C is very useful when we have to check several conditions. The
expression is tested from the top (of the ladder) downwards. As soon as the true condition is
found, the statement associated with it is executed.
Syntax:
if (condition 1)
statements 1
else if (condition 2)
statements 2
else if (condition 3)
statements 3
...........
else if (condition n)
statements n
else
default statements
Example:
#include<stdio.h>
main ( )
{
int num = 10 ;
if ( num > 0 )
printf ("\n Number is Positive");
else if ( num < 0 )
printf ("\n Number is Negative");
else
#include<stdio.h>
#include<conio.h>
main()
{
int n1,n2;
char choice;
clrscr();
printf("Menu \n '+' for addition \n '-' for subtraction \n '/' for division");
printf("\n '*' for multiplication \n '%' for remainder value \n \n");
printf("enter your choice: \n");
scanf("%c", &choice);
printf("\n enter two numbers:");
scanf("%d %d", &n1, &n2);
switch(choice)
{
case '+':
res=n1+n2;
printf("\n addition= %d", res);
break;
case '-':
res=n1-n2;
printf("\n subtraction=%d", res);
break;
case '/':
res=n1/n2;
printf("\n division=%d",res);
break;
case '*':
res=a*b;
printf("\n multiplication=%d", res);
break;
case '%':
res=n1%n2;
printf("\n remainder=%d", res);
break;
default:
printf(“invalid choice”)
}
getch();
return 0;
}
output:
Menu
'+' for addition
'-' for subtraction
'/' for division
'*' for multiplication
'%' for remainder value
addition=7
3. iterative/repetitive statements
Sometimes we want some part of our code to be executed more than once. We can either repeat
the code in our program or use loops instead.
For example we need to execute some part of code for a hundred times it is not practical to
repeat the code. Alternatively we can use our repeating code inside a loop.
A repetition statement (also called a looping statement or a loop) allows you to specify that a
program should repeat an action while some condition remains true.
While loop:
(While statement or Pretest loop statement or Entry Tested loop or
condition controlled loop.)
The while loop in C Programming is used to repeat a block of statements for
given number of times, until the given condition is False. While loop start with
the condition, if the condition is True then statements inside the while loop will
be executed. If the given condition is false then it won’t be executed at least
once. It means, while loop may execute zero or more time .
Syntax:
while(expressi
on)
{
block of
statement
}
Do-While:
(The DO Statement or Posttested loop or Exit Tested loop or condition controlled loop )
The Do While loop in C Programming will test the given condition at the end of loop so, Do
While loop executes the statements inside the code block at least once even if the given condition
Fails.
The While loop in C that we discussed in our previous test the condition before entering into the
code block. If the condition is True then only statements inside the loop will be executed
otherwise, statements will not be executed at least once. There are some situation where it is
necessary to perform some operations (execute some statements) first and then check for the
condition. In these cases we can go for Do While loop and the syntax of do while structure is:
Syntax:
FOR loop:
(The FOR statement or Counter Controlled Loop or pretested or entry
tested loops)
The For loop in C Programming is used to repeat a block of statements for given number of
times, until the given condition is False. For loop is one of the mostly used loop in any
programming language. Let us see the syntax of the for loop in C Programming.
Syntax:
In the above syntax of for loop there are three expressions separated by the semi-colons (;) and
the execution of these expressions are as follows:
Initialization: For loop starts with the initialization statement so, initialization of counters
variables is done first (For example counter = 1 or i = 1.). The initialization section is executed
only once at the beginning of the for loop.
Test Condition: The value of the counter variable is tested against the test condition. If the
condition is True then it will execute the statements inside the For loop. If the condition fails
then For loop will be terminated.
Increment and decrement operator: This expression is executed after the end of each iteration.
This operator helps to increase or decrease the counter variable as per our requirement
Example:
#include<stdio.h>
main()
{
int a;
for(a=1;a<=10;a++)
{
printf("%d\t",a);
}
}
o Step 7: Stop
Flow chart:
Source code:
#include<stdio.h>
#include<conio.h>
void main()
{
int num,sum,rem;
clrscr();
sum=0;
rem=0;
printf("enter a number to calculate sum of its individual digits: \n");
scanf("%d",&num);
while(num>0)
{
rem=num%10;
sum=sum+rem;
num=num/10;
}
printf("sum of digits of enterd number=%d \n",sum);
getch();
}
output:
enter a number to calculate sum of its individual digits:
123
sum of digits of entered number=6
Both “break” and “continue” are the ‘jump’ statements, that transfer control of the program to
another part of the program. The main difference between break and continue is that break is
used for immediate termination of loop. On the other hand, ‘continue’ terminate the current
iteration and resumes the control to the next iteration of the loop
The Break statement is used to exit from the The continue statement is not used to exit
loop constructs. from the loop constructs.
It terminates the execution of the remaining It terminates only the current iteration of the
iteration of the loop loop.
The break statement is usually used with the The continue statement is not used with the
switch statement, and it can also use it within switch statement, but it can be used within
the while loop, do-while loop, or the for-loop. the while loop, do-while loop, or for-loop.
When a break statement is encountered then When the continue statement is encountered
the control is exited from the loop construct then the control automatically passed from
immediately. the beginning of the loop statement.
Syntax: Syntax:
Break Statement Continue Statement
break; continue;
#include <stdio.h>
int main()
{
int i = 0, j = 0;
for (int i = 0; i < 5; i++) {
printf("i = %d, j = ", i);
for (int j = 0; j < 5; j++) {
if (j == 2)
break;
printf("\n");
}
return 0;
}
Output:
i = 0, j = 0 1
i = 1, j = 0 1
i = 2, j = 0 1
i = 3, j = 0 1
i = 4, j = 0 1
Explanation: In the above program the inner for loop always ends when the value of the
variable j becomes 2.
#include <stdio.h>
int main()
{
int i = 0, j = 0;
printf("\n");
}
return 0;
}
Output:
i = 0, j = 0 1 3 4
i = 1, j = 0 1 3 4
i = 2, j = 0 1 3 4
i = 3, j = 0 1 3 4
i = 4, j = 0 1 3 4
Explanation: In the above program the inner for loop always skip the iteration when the
value of the variable j becomes 2
}
Output:
Please Enter any positive integer
5
The Sum of Series for 5=55
Explanation:
calculating the Sum of the series 1²+2²+3²+4²+5² using below formula
Sum = (Number * (Number + 1) * (2 * Number + 1 )) / 6;
Sum = (5 * (5 + 1) * (2 * 5 +1)) / 6
Sum = (5 * 6 * 11) / 6
Sum = 330 /6
Sum = 55
[[[ 1*1+2*2+3*3+4*4+5*5=1+4+9+16+25=55]]]
* *
* * *
* * * *
Source code:
#include<stdio.h>
#include<conio.h>
void main()
{
int i, j, rows, k;
printf (" Enter a number to define the rows: \n ");
scanf("%d", &rows);
printf("\n");
for (i = 1; i <= rows; i++)
{
for (j = i; j < rows; j++)
{
printf(" ");
}
for (k = 1; k <= i; k++)
{
printf("*"); // print the Star
}
printf ("\n");
}
getch();
}
#include<stdio.h>
#include<conio.h>
int main()
{
char ch;
clrscr();
printf("enter any character:");
scanf("%c",&ch);
if((ch>='a' && ch<='z')||(ch>='A' && ch<='Z'))
{
printf("'%c' is alphabet",ch);
}
else if(ch>='0' && ch<='9')
{
printf("'%c' is digit",ch);
}
else
{
printf("'%c' is special character",ch);
}
return 0;
}
output:
enter any character:g
'g' is alphabet