0% found this document useful (0 votes)
45 views9 pages

Conditional Statements

Cse

Uploaded by

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

Conditional Statements

Cse

Uploaded by

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

// Simple if

#include <stdio.h>

int main(){

int number = 10;

if (number > 0) {

printf("The number is positive.\n");

return 0;

// if-else statement

#include <stdio.h>

int main()

int number = 5;

//Initiating the if-else statement

if (number % 2 == 0)

printf("The number is even.\n");

else

printf("The number is odd.\n");

return 0;

//Nested If

#include <stdio.h>

int main()
{

int age = 40;

int height = 185;

if (age >= 18){

if (height >= 160)

printf("You are eligible for the ride.\n");

} else {

printf("Sorry, you must be taller to go on the ride.\n");}

} else {

printf("Sorry, you must be at least 18 years old to go on the ride.\n");}

return 0;

//If-Else-If Ladder Statement

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

#include <stdio.h> // Include the standard input/output header file.


int main()

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

if (int1 == int2) // Check if Number1 is equal to Number2.

printf("Number1 and Number2 are equal\n"); // Print a message if


Number1 and Number2 are equal.

else

printf("Number1 and Number2 are not equal\n"); // Print a message


if Number1 and Number2 are not equal.

return 0;

Write a C program to find whether a given year is a leap year or not.


#include <stdio.h> // Include the standard input/output header file.

void main()
{
int chk_year; // Declare an integer variable 'chk_year'.

printf("Input a year :"); // Prompt the user to input a year.


scanf("%d", &chk_year); // Read and store the user's input in 'chk_year'.
if ((chk_year % 400) == 0) // Check if 'chk_year' is divisible by 400 with no
remainder.
printf("%d is a leap year.\n", chk_year); // Print a message indicating that
'chk_year' is a leap year.
else if ((chk_year % 100) == 0) // Check if 'chk_year' is divisible by 100
with no remainder.
printf("%d is not a leap year.\n", chk_year); // Print a message indicating
that 'chk_year' is not a leap year.
else if ((chk_year % 4) == 0) // Check if 'chk_year' is divisible by 4 with no
remainder.
printf("%d is a leap year.\n", chk_year); // Print a message indicating that
'chk_year' is a leap year.
else
printf("%d is not a leap year \n", chk_year); // Print a message indicating
that 'chk_year' is not a leap year.
}
Write a C program to read the age of a candidate and determine whether he is
eligible to cast his/her own vote.
#include <stdio.h> // Include the standard input/output header file.

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

printf("1st Number = %d,\t2nd Number = %d,\t3rd Number = %d\n", num1,


num2, num3); // Print the values of 'num1', 'num2', and 'num3'.

if (num1 > num2) // Check if 'num1' is greater than 'num2'.


{
if (num1 > num3) // Check if 'num1' is also greater than 'num3'.
{
printf("The 1st Number is the greatest among three. \n"); // Print a
message indicating that 'num1' is the greatest.
}
else
{
printf("The 3rd Number is the greatest among three. \n"); // Print a
message indicating that 'num3' is the greatest.
}
}
else if (num2 > num3) // If 'num1' is not the greatest, check if 'num2' is
greater than 'num3'.
printf("The 2nd Number is the greatest among three \n"); // Print a
message indicating that 'num2' is the greatest.
else
printf("The 3rd Number is the greatest among three \n"); // If none of the
above conditions are met, 'num3' must be the greatest.
}

You might also like