0% found this document useful (0 votes)
11 views

1.2 Programs in The Class Selection - If Else and Switch

Uploaded by

aadhila9524
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)
11 views

1.2 Programs in The Class Selection - If Else and Switch

Uploaded by

aadhila9524
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/ 15

Practice all programs in your local machine or through online C compilers.

C Code Examples: https://www.programiz.com/c-programming/examples


Online C Compiler: https://www.programiz.com/c-programming/online-compiler/

SELECTION : a. IF ELSE
Q1. Input marks of a student in 3 subjects, calculate avg, ig avg is >60 print pass else print
fail.
#include <stdio.h>
int main()
{
float m1,m2,m3,avg;
printf("\n Enter marks in 3 subjects");
scanf("%f %f %f",&m1,&m2,&m3);
avg=(m1+m2+m3)/3;
printf("\n Average :%f",avg);
if(avg>=60)
printf("\n PASS");
else
printf("\n FAIL");
return 0;
}
Q.2 Input two angles and check if they are complimentary (a1+a2=90)
#include <stdio.h>
int main()
{
float a1,a2,sum;
printf("\n Enter two angels");
scanf("%f %f",&a1,&a2);
sum=a1+a2;
printf("\n Sum of angels : %f",sum);
if(sum==90)
printf("\n %f and %f is COMPLIMENTARY",a1,a2);
else if(sum<90)
printf("\n Acute angles");
else if(sum>90)
printf("\n Obtuce angles");
return 0;
}
Q.3 input your year of birth, find age (current 2023), if your age >=18 vote else not
#include <stdio.h>
#define current 2024
int main()
{
int year,age;
printf("\n Enter the year of birth :");
scanf("%d",&year);
age=current-year;
printf("\n Age : %d",age);
if(age>=18)
printf("\n ELIGIBLE TO VOTE");
else
printf("\n NOT ELIGIBLE TO VOTE");
return 0;
}
Q.4 positive/negative/zero
//WAP to check if the user input number is positive or negative
#include <stdio.h>
int main()
{
float num;
printf("\n Enter any number");
scanf("%f",&num);
if(num>0)
printf("\n positive");
else if(num<0)
printf("\n negative");
else
printf("\n Equal to zero");
return 0;
}
Q.5 odd or even
//WAP to chcek if the user input number is odd/even
#include <stdio.h>
int main()
{
int num;
printf("\n Enter any number");
scanf("%d",&num);
if(num%2==0)
printf("\n Even");
else
printf("\n Odd");
return 0;
}
Q.6 larger among 2 using if else ladder
//WAP to find larger among two numbers
#include <stdio.h>
int main()
{
float num1,num2;

printf("\n Enter any two number");


scanf("%f %f",&num1,&num2);
if(num1>num2)
printf("\n Bigger is : %f",num1);
else if(num2>num1)
printf("\n Bigger is : %f",num2);
else
printf("\n Both are equal");
return 0;
}

Q.7 Finding largest among three numbers


//WAP to find largest among three numbers
#include <stdio.h>
int main()
{
float num1,num2,num3;

printf("\n Enter any three number :");


scanf("%f %f %f",&num1,&num2,&num3);

if(num1>num2)
{
if(num1>num3)
printf("\n Largest is : %f",num1);
else
printf("\n Largest is : %f",num3);
}
else // num2 is larger
{
if(num2>num3)
printf("\n Largest is : %f",num2);
else
printf("\n Largest is : %f",num3);
}

return 0;
}

Q.8 Calculator using if else


//WAP to make a calculator
#include <stdio.h>
int main()
{
int num1,num2;
char op;

printf("\n Enter any two number :");


scanf("%d %d",&num1,&num2);
printf("\n Enter any operator + - * / % :");
//getchar();
scanf("%c",&op);

if(op=='+')
printf("\n Result is :%d", num1+num2);
else if(op=='-')
printf("\n Result is :%d", num1-num2);
else if(op=='*')
printf("\n Result is :%d", num1*num2);
else if(op=='/')
{
if (num2!=0)
printf("\n Result is :%d", num1/num2);
else
printf("\n Cant divide by zero");
}
else if(op=='%')
printf("\n Result is :%d", num1%num2);
else
printf("\n Invalid operator : %c",op);

return 0;
}

Q.9 Leap Year


#include <stdio.h>
int main() {
int year;
printf("Enter a year: ");
scanf("%d", &year);

// leap year if perfectly divisible by 400


if (year % 400 == 0) {
printf("%d is a leap year.", year);
}
// not a leap year if divisible by 100
// but not divisible by 400
else if (year % 100 == 0) {
printf("%d is not a leap year.", year);
}
// leap year if not divisible by 100
// but divisible by 4
else if (year % 4 == 0) {
printf("%d is a leap year.", year);
}
// all other years are not leap years
else {
printf("%d is not a leap year.", year);
}

return 0;
}
SELECTION: b. SWITCH
Q.1 Good morning/ good night (if else)
#include <stdio.h>
int main()
{
char op;

printf("\n Enter any char m or n");


scanf("%c",&op);

if(op=='m')
printf("\n Good morning");
else if(op=='n')
printf("\n Good night");
else
printf("\n Invalid character : %c",op);
return 0;
}
Good morning/ Good night (switch)
#include <stdio.h>

int main()
{
char a;
printf("\n Enter m or n");
scanf("%ch",&a);
switch(a)
{
case 'm': printf("\n Good morning"); break;
case 'n':printf("\n Good night"); break;
default: printf("\n Invalid character"); break;
}
return 0;
}
Q.2 Good morning/evening/night (if else ladder)
#include <stdio.h>

int main()
{
char ch;
printf("Enter m for morning, e for evening, n for night");
scanf("%c",&ch);
if(ch=='m'|| ch=='M')
{
printf("\n good morning");
printf("\n Monday");
}
else if(ch=='e'|| ch=='E')
{
printf("\n good evening");
printf("\n Tuesday");
}
else if(ch=='n'|| ch=='N')
{
printf("\n good night");
printf("\n saturday");
}
else
{
printf("\n BYE");
printf("\n Sunday");
}

return 0;
}
Good morning/evening/night (switch)
#include <stdio.h>

int main()
{
char ch;
printf("Enter m for morning, e for evening or n for night");
scanf("%c",&ch);
switch(ch)
{
case 'm':
case 'M':
printf("\n Good morning");
printf("\n Monday");
break;

case 'e':
case 'E':
printf("\n Good evening");
printf("\n Saturday");
break;
case 'N':
case 'n': printf("\n Good night");break;
default: printf("\n BYE"); break;
}
return 0;
}
Q.3 Calculator (switch)
#include <stdio.h>
int main()
{
int n1,n2;
char ch;
printf("Enter + for addition...");
scanf("%c",&ch);
printf("\n Enter two nos");
scanf("%d %d",&n1,&n2);
switch(ch)
{
case '+': printf("%d",n1+n2); break;
case '-':printf("%d",n1-n2); break;
case '*': printf("%d",n1*n2); break;
case '/':printf("%d",n1/n2); break;
case '%':printf("%d",n1%n2); break;

default: printf("\n BYE"); break;


}
return 0;
}
Q.4 Vowel / consonant (switch)
#include <stdio.h>
int main()
{
char ch;
printf("Enter any character a-z or A-Z");
scanf("%c",&ch);
switch(ch)
{
case 'a':
case 'A':
case 'e':
case 'E':
case 'i':
case 'I':
case 'o':
case 'O':
case 'u':
case 'U': printf("\n Vowel");break;
default: printf("\n Consonant"); break;
}
return 0;
}
Q.5 Minor/vote (switch) option 1
#include <stdio.h>

int main()
{
int age, eligible=0;
printf("Enter your age");
scanf("%d",&age);
if(age>=18) eligible=1;
switch(eligible)
{
case 0:printf("\n Minor");break;
case 1:printf("\n Can vote");break;
}

return 0;
}
Minor/vote (switch) option 2
#include <stdio.h>

int main()
{
int age;
printf("Enter your age");
scanf("%d",&age);
switch(age>=18)
{
case 0:printf("\n Minor");break;
case 1:printf("\n Can vote");break;
}

return 0;
}
TERNARY OPERATOR ? :
Q.1 Vote
#include <stdio.h>
#define current 2023
int main()
{
int year, age;
printf("\n Enter your year of birth");
scanf("%d",&year);
age=current-year;
printf("\n Age: %d",age);
(age>=18)? printf("\n CAN VOTE") : printf("\n CAN NOT VOTE");
/*if(age>=18) printf("\n CAN VOTE");
else printf("\n CAN NOT VOTE");*/
return 0;
}
Q.2 Even ODD
#include <stdio.h>

int main()
{
int num;
printf("\n Enter num");
scanf("%d",&num);
(num%2==0)? printf("\n EVEN"): printf("\n ODD");
return 0;
}
Q.3 Larger number
#include <stdio.h>
int main()
{
int num1,num2,res;
printf("\n Enter 2 num");
scanf("%d %d",&num1, &num2);
res=(num1>num2)? num1: num2;
printf("\n Largest : %d",res);
return 0;
}
All over programming in C
https://www.programiz.com/c-programming/examples

You might also like