0% found this document useful (0 votes)
30 views40 pages

21PSP23 Notes-PDF 1IA19

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views40 pages

21PSP23 Notes-PDF 1IA19

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 40

Today’s Topic

✔Decision Making and Branching, Simple if Statement


Decision making in C

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

3. conditional operator statement (? : operator)

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

3.Nested if....else statement

4.Using else if statement


if 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
}

How if statement works?

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..else statement is:

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

2)Input the values for Number1 and Number2: 45 34


Number1 and Number2 are not equal
Write a C program to check whether a given number is even or odd
#include <stdio.h>
void main()
{
int num1, rem1;
printf("Input an integer : ");
scanf("%d", &num1);
rem1 = num1 % 2;
if (rem1 == 0)
{
printf("%d is an even integer\n", num1);
}
else
{
printf("%d is an odd integer\n", num1);
}

}
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)

printf("The 2nd Number is the greatest among three \n");


else
printf("The 3rd Number is the greatest among three \n");
}
Write a C program to check whether a triangle is Equilateral, Isosceles or Scalene.
#include <stdio.h>
int main()
{
int sidea, sideb, sidec;
//are three sides of a triangle /* * Reads all sides of a triangle */
printf("Input three sides of triangle: ");
scanf("%d %d %d", &sidea, &sideb, &sidec);
if(sidea==sideb && sideb==sidec) //check whether all sides are equal
{
printf("This is an equilateral triangle.\n");
}
else if(sidea==sideb || sidea==sidec || sideb==sidec)
//check whether two sides are equal
{
printf("This is an isosceles triangle.\n");
}
else
//check whether no sides are equal
{
printf("This is a scalene triangle.\n"); }
return 0;
}
Write a C program to read temperature in centigrade and display a suitable
message according to temperature state below.
Temp < 0 then Freezing weather
Temp 0-10 then Very Cold weather
Temp 10-20 then Cold weather
Temp 20-30 then Normal in Temp
Temp 30-40 then Its Hot
Temp >=40 then Its Very Hot
#include <stdio.h>
void main()
{
int tmp;
printf("Input days temperature : ");
scanf("%d",&tmp);
if(tmp<0)
printf("Freezing weather.\n");
else if(tmp<10) printf("Very cold weather.\n");
else if(tmp<20) printf("Cold weather.\n");
else if(tmp<30) printf("Normal in temp.\n");
else if(tmp<40) printf("Its Hot.\n");
else printf("Its very hot.\n");
}
C program to check whether a character is alphabet or not
Write a C program to input a character from user and check whether the
given character is alphabet or not using if else. How to check whether a
character is alphabet or not in C programming. Logic to check if a
character is alphabet or not in C program.

Step by step descriptive logic to check alphabets.


1.Input a character from user. Store it in some variable say ch.

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.

Logic to check vowels or consonants


English alphabets a, e, i, o and u both lowercase and uppercase are known as vowels.
Alphabets other than vowels are known as consonants.
Step by step descriptive logic to check vowels or consonant.
.Input a character from user. Store it in some variable say ch.

.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.

.If it is neither vowel nor consonant, then it is not alphabet.


Character in C is represented inside single quote. Do not forget to add single quote whenever
checking for character constant.
/** * C program to check whether a character is vowel or consonant */
#include <stdio.h>
int main()
{
char ch; /* Input character from user */
printf("Enter any character: ");
scanf("%c", &ch); /* Condition for vowel */
if(ch= ='a' || ch= ='e' || ch= ='i' || ch= ='o' || ch= ='u' || ch= ='A' || ch= ='E' ||
ch= ='I' || ch= ='O' || ch= ='U')
{
printf("'%c' is Vowel.", ch);
}
else if((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z'))
{
/* Condition for consonant */
printf("'%c' is Consonant.", ch);
}
else
{
/* * If it is neither vowel nor consonant * then it is not an alphabet. */
printf("'%c' is not an alphabet.", ch);
}
return 0;
}
C program to check whether a character is alphabet, digit or special character

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.

Logic to check alphabet, digit or special character


•A character is alphabet if it in between a-z or A-Z.
•A character is digit if it is in between 0-9.
•A character is special symbol character if it neither alphabet nor digit.

Step by step descriptive logic to check alphabet, digit or special character.


.Input a character from user. Store it in some variable say ch.

.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;
}

You might also like