CONDITIONAL STATEMENT IN C LANGUAGE
Flow chart of if else :-
* It is used to perform based on some conditions.
Types of statements:-
-> if statement.
-> if else statement.
-> if-else if ladder.
-> nestd if.
IF STATEMENT:-
#include <stdio.h>
int main(int argc, char const *argv[])
{
int age;
printf("Enter your Age");
scanf("%d,",&age);
printf("You have entered %d as your age\n", age);
if (age>18)
{
printf("you can vote!");
}
return 0;
}
IF-ELSE STATEMENT:-
#include <stdio.h>
int main(int argc, char const *argv[])
{
int age;
char a;
printf("Enter your age\n", age);
scanf("%d\a", &age);
printf("you entered %d as your age\n", age);
if (age >= 18)
{
printf("You are elligiable to take liscense");
}
else if (age < 18)
{
printf("You are not elligible to take liscense because your age is below 18\n", age);
}
return 0;
}
QUESTIONS:-
Make a program in which give student who passed in subject maths only or science only if any students pass in both math and science then give him/her 45 rupees
type 1:-
#include <stdio.h>
int main(int argc, char const *argv[])
{
int subject;
char a = "Science";
char b = "Maths";
char c = "maths and science";
printf("how many subject you are passing and enter valid subjets because we have only two subjects \n",subject);
scanf("%d",&subject);
printf("you passed in %d subjects\n",subject);
if (subject == 2)
{
printf("you get 45 rupees because you pass in both subjects(maths and science)");
}
else if (subject ==1)
{
printf("you get only 15 rupees because you passed in only one subject");
}
else if (subject >2)
{
printf("Please Enter valid number because we have only 2 subjects");
}
return 0;
}
Type 2:-
#include <stdio.h>
int main(int argc, char const *argv[])
{
int a;
printf(" Enter 1 for science\n Enter 2 for maths\n Enter 3 for both maths aand science\n");
scanf("%d",&a);
if (a==1)
{
printf("Congratulations you won 15 rupees because you pass in Science subject");
}
else if(a == 2)
{
printf("Congratulation you won 15 rupees because you pass in Maths subject");
}
else if (a == 3)
{
printf("OMG very very congratulation you won 45 rupees because you passed in maths and science both");
}
return 0;
}
NESTD if.
#include <stdio.h>int main(){int var1, var2;printf("Input the value of var1:");scanf("%d", &var1);printf("Input the value of var2:");scanf("%d",&var2);if (var1 != var2){printf("var1 is not equal to var2\n");//Nested if elseif (var1 > var2){printf("var1 is greater than var2\n");}else{printf("var2 is greater than var1\n");}}else{printf("var1 is equal to var2\n");}return 0;}