21PSP23 Notes-PDF 1IA19
21PSP23 Notes-PDF 1IA19
Decision making is about deciding the order of execution of statements based on certain
conditions or repeat a group of statements until certain specified conditions are met. C
language handles decision-making by supporting the following statements,
1. if statement
2. switch statement
4. goto statement
Decision making with if statement
The if statement may be implemented in different forms depending on the complexity of
conditions to be tested. The different forms are,
1.Simple if statement
2.if....else statement
if
(testExpression)
{
// statements
}
If Statement
The if statement is used to check some given condition and perform some operations
depending upon the correctness of that condition. It is mostly used in the scenario where we
need to perform the different operations for the different conditions. The syntax of the if
statement is given below.
if (expression)
{
//code to be executed
}
The if statement evaluates the test expression inside the parenthesis ().
•If the test expression is evaluated to true, statements inside the body of if are executed.
•If the test expression is evaluated to false, statements inside the body of if are not
executed.
// Program to display a number if it is negative
#include <stdio.h>
int main() { int number;
printf("Enter an integer: ");
scanf("%d", &number);
// true if number is less than 0
if (number < 0)
{
printf("You entered %d.\n", number);
}
printf(“ The C Programming Language\n");
return 0;
}
When the user enters -8, the test expression number<0 is evaluated to true.
Hence,
You entered -8 is displayed on the screen
When the user enters 5, the test expression number<0 is evaluated to false and the statement
inside the body of if is not executed
C if...else Statement
The if statement may have an optional else block. The syntax of the
if (test expression)
{
// run code if test
expression is true
}
else
{ // run code if test
expression is false
}
How if...else statement works?
If the test expression is evaluated to true,
•statements inside the body of if are
executed.
•statements inside the body of else are
skipped from execution.
---------------------------------------------------------
•If the test expression is evaluated to false,
statements inside the body of else are executed
•statements inside the body of if skipped from
execution
Example 2: if...else statement
// Check whether an integer is odd or even
#include <stdio.h>
int main()
{
int number;
printf("Enter an integer: ");
scanf("%d", &number);
// True if the remainder is 0
if (number%2 == 0)
{
printf("%d is an even integer.",number);
}
else
{
printf("%d is an odd integer.",number);
}
return 0
}
C if...else Ladder
The if...else statement executes two different codes depending upon whether
the test expression is true or false.
Sometimes, a choice has to be made from more than 2 possibilities.
The if...else ladder allows you to check between multiple test expressions and
execute different statements.
Syntax is as shown below
if (test expression1)
{
// statement(s)
}
else if(test expression2) {
// statement(s)
}
else if (test expression3)
{
// statement(s)
} . . else
{
// statement(s)
}
// Program to relate two integers using =, > or < symbol
#include <stdio.h>
int main()
{
int number1, number2;
printf("Enter two integers: ");
scanf("%d %d", &number1, &number2);
//checks if the two integers are equal.
if(number1 == number2)
{
printf("Result: %d = %d",number1,number2);
}
//checks if number1 is greater than number2.
else if (number1 > number2)
{
printf("Result: %d > %d", number1, number2);
}
//checks if both test expressions are false
else
{
printf("Result: %d < %d",number1, number2);
}
return 0;
}
Program to calculate the grade of the student according to the specified marks
#include <stdio.h>
int main()
{
int marks;
printf("Enter your marks?");
scanf("%d",&marks);
if(marks > 85 && marks <= 100)
{
printf("Congrats ! you scored grade A ...");
}
else if (marks > 60 && marks <= 85)
{
printf("You scored grade B + ...");
}
else if (marks > 40 && marks <= 60)
{
printf("You scored grade B ...");
}
else if (marks > 30 && marks <= 40)
{
printf("You scored grade C ...");
}
else
{
printf("Sorry you are not pass...");
}
}
1 )Write a C program to accept two integers and check whether they are equal or not.
C Code:
#include <stdio.h>
void main()
{
int int1, int2;
printf("Input the values for Number1 and Number2 : ");
scanf("%d%d", &int1, &int2);
if (int1 == int2)
printf("Number1 and Number2 are equal\n");
else
printf("Number1 and Number2 are not equal\n");
}
Sample Output:
1)Input the values for Number1 and Number2 : 15 15
Number1 and Number2 are equal
}
Sample Output:
1) Input an integer : 15
15 is an odd integer
2) Input an Integer 46
46 is an even number
Write a C program to find whether a given year is a leap year or not.
Write a C program to find whether a given year is a leap year or not.
#include <stdio.h>
void main()
{
int chk_year;
printf("Input a year :");
scanf("%d", &chk_year);
if ((chk_year % 400) == 0)
printf("%d is a leap year.\n", chk_year);
else if ((chk_year % 100) == 0)
printf("%d is a not leap year.\n", chk_year);
else if ((chk_year % 4) == 0)
printf("%d is a leap year.\n", chk_year);
else printf("%d is not a leap year \n", chk_year);
}
Write a C program to read the age of a candidate and determine whether it is
eligible for casting his/her own vote.
C Code :
#include <stdio.h>
void main()
{
int vote_age;
printf("Input the age of the candidate : ");
scanf("%d",&vote_age);
if (vote_age<18)
{
printf("Sorry, You are not eligible to caste your
vote.\n");
printf("You would be able to caste your vote after
%d year.\n",18-vote_age);
}
Else
printf("Congratulation! You are eligible for casting
your vote.\n");
}
Write a C program to find the largest of three numbers.
#include <stdio.h>
void main()
{
int num1, num2, num3;
printf("Input the values of three numbers : ");
scanf("%d %d %d", &num1, &num2, &num3);
printf("1st Number = %d,\t2nd Number = %d,\t3rd Number = %d\n", num1, num2, num3); if (num1 >
num2)
{
if (num1 > num3)
{
printf("The 1st Number is the greatest among three. \n");
}
else
{
printf("The 3rd Number is the greatest among three. \n");
}
}
else if (num2 > num3)
2.Check if((ch >= 'a') && (ch <= 'z')) or if((ch >= 'A') &&
(ch <= 'Z')). Then it is alphabet otherwise not.
C program to check vowel or consonant
Write a C program to check whether an alphabet is vowel or consonant using if else. How to
check vowels and consonants using if else in C programming. C Program to input a character
from user and check whether it is vowel or consonant. Logic to check vowel or consonant in C
programming.
.Check conditions for vowel i.e. if(ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u'), then
it is vowel.
.If character is alphabet but not vowel then it is consonant. Means check ch >= 'a' && ch <=
'z' then, it is consonant.
Write a C program to input a character from user and check whether given character is
alphabet, digit or special character using if else. How to check if a character is alphabet,
digits or any other special character using if else in C programming. Logic to check
alphabet, digit or special character in C programming.
.First check if character is alphabet or not. A character is alphabet if((ch >= 'a' && ch <=
'z') || (ch >= 'A' && ch <= 'Z')).
.Next, check condition for digits. A character is digit if(ch >= '0' && ch <= '9').
.Finally, if a character is neither alphabet nor digit, then character is a special character.
/** * C program to check alphabet, digit or special character */
#include <stdio.h>
int main()
{
char ch; /* Input character from user */
printf("Enter any character: ");
scanf("%c", &ch);
/* Alphabet check */
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;
}
#include <stdio.h>
int main()
{
char ch; /* Input a character from user */
printf("Enter any character: ");
scanf("%c", &ch);
if((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z'))
{
printf("Character is an ALPHABET.");
}
else
{
printf("Character is NOT ALPHABET.");
}
return 0;
}
Write a C program to check whether a character is an alphabet, digit or special character.
#include <stdio.h>
int main()
{
char sing_ch;
printf("Input a character: ");
scanf(“%c” ,&sing_ch); /* Checks whether it is an alphabet */
if((sing_ch>='a' && sing_ch<='z') || (sing_ch>='A' && sing_ch<='Z'))
{
printf("This is an alphabet.\n");
}
else if(sing_ch>='0' && sing_ch<='9') /* whether it is digit
{
printf("This is a digit.\n");
}
else /* Else special character */
{
printf("This is a special character.\n");
}
}
Let's write a program to illustrate the use of nested if-else
#include<stdio.h>
int main()
{
int num=1;
if(num<10)
{
if(num==1)
{
printf("The value is:%d\n",num);
Nested if else
}
statement
else
{
printf("The value is greater than 1");
}
}
else
{
printf("The value is greater than 10");
}
return 0;
}
Example: else-if is used when multipath decisions are required.
#include<stdio.h>
int main()
{
int marks=83;
if(marks>75)
{
printf("First class");
}
else if(marks>65)
{
printf("Second class");
}
else if(marks>55)
{
printf("Third class");
}
else
{
printf("Fourth class");
}
return 0;
}