Conditional Statements
Conditional Statements
#include <stdio.h>
int main(){
if (number > 0) {
return 0;
// if-else statement
#include <stdio.h>
int main()
int number = 5;
if (number % 2 == 0)
else
return 0;
//Nested If
#include <stdio.h>
int main()
{
} else {
} else {
return 0;
#include <stdio.h>
int main()
int day = 3;
if (day == 1)
printf("Sunday\n");
else if (day == 2)
printf("Monday\n");
else if (day == 3)
{
printf("Tuesday\n");
else if (day == 4)
printf("Wednesday\n");
else if (day == 5)
printf("Thursday\n");
else if (day == 6)
printf("Friday\n");
else if (day == 7)
printf("Saturday\n");
else
printf("Invalid day\n");}
return 0;
Write a C program to accept two integers and check whether they are equal or
not.
int int1, int2; // Declare two integer variables 'int1' and 'int2'.
printf("Input the values for Number1 and Number2 : "); // Prompt the
user to input values for Number1 and Number2.
scanf("%d %d", &int1, &int2); // Read and store the user's input in
'int1' and 'int2'.
else
return 0;
void main()
{
int chk_year; // Declare an integer variable 'chk_year'.
void main()
{
int vote_age; // Declare an integer variable 'vote_age' to store the age of the
candidate.
printf("Input the age of the candidate : "); // Prompt the user to input the age
of the candidate.
scanf("%d",&vote_age); // Read and store the user's input in 'vote_age'.
if (vote_age<18) // Check if 'vote_age' is less than 18.
{
printf("Sorry, You are not eligible to caste your vote.\n"); // Print a message
indicating the candidate is not eligible to vote.
printf("You would be able to caste your vote after %d year.\n",18-vote_age);
// Print the number of years until the candidate is eligible to vote.
}
else
printf("Congratulation! You are eligible for casting your vote.\n"); // Print a
message indicating the candidate is eligible to vote.
}
Write a C program to read the value of an integer m and display the value of n is
1 when m is larger than 0, 0 when m is 0 and -1 when m is less than 0.
#include <stdio.h> // Include the standard input/output header file.
void main()
{
int m,n; // Declare two integer variables 'm' and 'n'.
printf("Input the value of m :"); // Prompt the user to input the value of 'm'.
scanf("%d",&m); // Read and store the user's input in 'm'.
if(m!=0) // Check if 'm' is not equal to 0.
{
if(m>0)
n=1; // If 'm' is greater than 0, set 'n' to 1.
else
n=-1; // If 'm' is less than 0, set 'n' to -1.
}
else
n=0; // If 'm' is equal to 0, set 'n' to 0.
printf("The value of m = %d \n",m); // Print the value of 'm'.
printf("The value of n = %d \n",n); // Print the value of 'n'.
}
Write a C program to accept the height of a person in centimeters and categorize
the person according to their height.
#include <stdio.h> // Include the standard input/output header file.
void main()
{
float PerHeight; // Declare a floating-point variable 'PerHeight' to store the
height of the person.
printf("Input the height of the person (in centimetres) :"); // Prompt the user
to input the height in centimeters.
scanf("%f", &PerHeight); // Read and store the user's input in 'PerHeight'.
if (PerHeight < 150.0) // Check if 'PerHeight' is less than 150.0.
printf("The person is Dwarf. \n"); // Print a message indicating that the
person is a dwarf.
else if ((PerHeight >= 150.0) && (PerHeight < 165.0)) // Check if
'PerHeight' is between 150.0 and 165.0.
printf("The person is average heighted. \n"); // Print a message indicating
that the person has an average height.
else if ((PerHeight >= 165.0) && (PerHeight <= 195.0)) // Check if
'PerHeight' is between 165.0 and 195.0.
printf("The person is taller. \n"); // Print a message indicating that the
person is taller.
else
printf("Abnormal height.\n"); // Print a message indicating that the height
is abnormal.
}
Write a C program to find the largest of three numbers.
#include <stdio.h> // Include the standard input/output header file.
void main()
{
int num1, num2, num3; // Declare three integer variables 'num1', 'num2', and
'num3'.
printf("Input the values of three numbers : "); // Prompt the user to input
three numbers.
scanf("%d %d %d", &num1, &num2, &num3); // Read and store the user's
input in 'num1', 'num2', and 'num3'.