0% found this document useful (0 votes)
44 views4 pages

Lab Exercise Solution 4 &5

This document contains solutions to 6 programming exercises using C language. The exercises include programs to determine if a number is even or odd, find the greatest of 3 numbers, check if a year is a leap year, grade a subject based on marks scored, perform basic arithmetic operations based on user input, and display the day of the week based on a one-letter input. The solutions demonstrate the use of if-else statements and switch case statements to solve logic and conditional checking problems.

Uploaded by

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

Lab Exercise Solution 4 &5

This document contains solutions to 6 programming exercises using C language. The exercises include programs to determine if a number is even or odd, find the greatest of 3 numbers, check if a year is a leap year, grade a subject based on marks scored, perform basic arithmetic operations based on user input, and display the day of the week based on a one-letter input. The solutions demonstrate the use of if-else statements and switch case statements to solve logic and conditional checking problems.

Uploaded by

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

LAB EXERCISE SOLUTION

WEEK – 2 & 3

1. Program to find whether the given no is even or odd


Solution:
#include<stdio.h>
void main()
{
int num1;
printf("enter the number to check\n");
scanf("%d",&num1);
if(num1%2==0)
printf("the given no is even");
else
printf("the given no is odd");
getch();
}
2. Program to find greatest in 3 numbers
Solution:
#include<stdio.h>
void main()
{
int a,b,c;
printf("enter three numbers \n");
scanf("%d%d%d",&a,&b,&c);
if(a>b && a>c)
printf("a is big");
else if(b>c)
printf("b is big");
else
printf("c is big");
getch();
}
3. Program to find that entered year is leap year or not
Logic to do this program

Definition of leap year:

Rule 1: A year is called leap year if it is divisible by 400.


For example: 1600, 2000 etc leap year while 1500, 1700 are not leap year.
Rule 2: If year is not divisible by 400 as well as 100 but it is divisible by 4
then that year are also leap year.
For example: 2004, 2008, 1012 are leap year.

4. Write a C program to accept marks scored in a subject and print the result

as below.

If the marks scored is less than 35 print result as fail, if the marks scored is 35
and above and less than 50 print the result as III class, if the marks is 50 and
above and less than 60 print result as II class, if the marks is 60 and above
and less than 85 print result as I class and if the marks is 85 and above and
less than 100 print the result as distinction. [Use if-else statement]

Logic to do this program

Step 1: Get the five subject mark from the user and calculate the sum and
average and then check for the condition like

if(average>=35 && average<50)

print (“Third class)

Like the above coding continue remaining condition as stated in program.


5. Write a C program to do basic arithmetic operations on given two integers

by accepting the operator from the user. Use switch case statement to do
this program.
[Hint: the given operator is +, then program should give sum of given two
integers, if the operator is – , then calculate difference, if operator is * then
calculate product and if operator is /, then calculate quotient

Solution:
#include<stdio.h>
void main()
{
int first_no,second_no,result;
char option;
printf("enter the first number");
scanf("%d",&first_no);
printf("enter the second number");
scanf("%d",&second_no);
printf("enter the option + , -, *, /\n");
option=getch();
switch(option)
{
case '+':
result=first_no+second_no;
printf("addition of two numbers = %d",result);
break;
case '-':
result=first_no-second_no;
printf("subtraction of two numbers = %d",result);
break;
case '*':
result=first_no*second_no;
printf("multiplication of two numbers = %d",result);
break;
case '/':
result=first_no/second_no;
printf("division of two numbers = %d",result);
break;
default:
printf("operator is in out of option");
break;
}
getch();
}
6. Program to use switch statement. Display Monday to Sunday
Logic to do this program
If you type ‘M’ button then it has to display as Monday , ‘T’ display as
Tuesday , ‘W’ display as Wednesday, ‘T’ as Thursday, ‘F’ as Friday , ‘R’ as
Saturday and ‘S’ as Sunday.

You might also like