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

C_codes

Uploaded by

srinidhi.k2022
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)
10 views

C_codes

Uploaded by

srinidhi.k2022
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/ 5

Q1.

If question
Code:
int main()
{
int age;
printf("Enter your age ");
scanf("%d",&age);
if(age>=18)
printf("Eligible for voting");
return 0;
};
}

Output:
Enter your age 18
Eligible for voting

Q2. If else question


Code:

#include <stdio.h>

int main()
{
int age;
printf("Enter your age ");
scanf("%d",&age);
if(age>=18)
printf("Eligible for voting");
else
printf("Not eligible for voting");
return 0;
}

Output:
Enter a number -56
Number is negative

Q3. Else If Question:


Code:
#include <stdio.h>

int main()
{
int num;
printf("Enter a number ");
scanf("%d",&num);
if(num>0)
printf("Number is positive");
else if(num<0)
printf("Number is negative");
else
printf("Number is zero");
return 0;
}

Output:
Enter a number -56
Number is negative

Enter a number 0
Number is zero

Enter a number 2
Number is positive

Question 4: Nested if else


Code:
#include <stdio.h>

int main()
{
int a,b,c;
printf("Entrer Three numbers");
scanf("%d %d %d",&a,&b,&c);
if(a>c)
{ if(a>c)
printf("a:%d is the largest \n",a);
}
else if(b>c)
printf("b: %d is teh largest\n",b);
else
printf("c: %d is the largest\n",c);
return 0;
}

Output:
Entrer Three numbers 12 23 45
c: 45 is the largest

Question 5:Switch
Code:
#include <stdio.h>

int main()
{
int day;
printf("Enter the day number ");
scanf("%d",&day);
switch(day)
{
case 1:
printf("The day is Monday");
break;
case 2:
printf("The day is Tuesday");
break;
case 3:
printf("The day is Wednesday");
break;
case 4:
printf("The day is Thursday");
break;
case 5:
printf("The day is Friday");
break;
case 6:
printf("The day is Saturday");
break;
case 7:
printf("The day is Sunday");
break;
default:
}
return 0;
}

Output:
Enter the day number 32
The day is Wednesday

Question6: Passed or failed


Code:
#include <stdio.h>

int main()
{
int marks;
printf("Enter your marks ");
scanf("%d",&marks);
if(marks>50)
printf("Passed");
else if(marks<50)
printf("Failed");
return 0;
}

Output:
Enter your marks 56
Passed

Question 7:Average marks and grade calculation


Code:
#include <stdio.h>

int main()
{
int a,b,c,d,e;
float avg;
printf("Enter 5 Subject marks ");
scanf("%d %d %d %d %d",&a,&b,&c,&d,&e);
avg=(a+b+c+d+e)/5;
printf("Your average is %f \n",avg);
if (avg<=100 && avg>=90)
printf("S grade");
else if (avg<=89 && avg>=80)
printf("A grade");
else if (avg<=79 && avg>=70)
printf("B grade");
else if (avg<=69 && avg>=60)
printf("C grade");
else if (avg<=59 && avg>=50)
printf("D grade");
else if (avg<50)
printf("Failed");

return 0;
}

Output:
Enter 5 Subject marks 34 67 89 76 99
Your average is 73.000000
B grade

Question 8:
Code:
#include <stdio.h>

int main()
{
int a,b,c,d;
printf("Enter a 3 digit number ");
scanf("%d",&a);
b=a%10;
c=a/100;
d=c+b;
printf("The sum of first and last number is %d",d);
return 0;
}
Output:
Enter a 3 digit number 619
The sum of first and last number is 15

Question 9:
Code: #include <stdio.h>

int main()
{
char alpha;
printf("Enter an alphabet ");
scanf("%s",&alpha);
switch(alpha)
{
case 'a':
printf("1");
break;
case 'b':
printf("2");
break;
case 'c':
printf("3");
break;
case 'd':
printf("4");
break;
case 'e':
printf("5");
break;
case 'f':
printf("6");
break;
case 'g':
printf("7");
break;
case 'h':
printf("8");
break;
case 'i':
printf("9");
break;
case 'j':
printf("10");
break;
default:
}
return 0;
}

You might also like