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

Practical 2

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)
6 views

Practical 2

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/ 28

PRACTICAL 3- DECISION MAKING AND BRANCHING

STATEMENTS

Name:- Mayuresh Pandey Date: 30th Aug 2024 Roll no:-

1. Write a C program that accepts a year and finds whether year entered
is a leap year or not.
Input:-
#include <stdio.h>

void main()

int year;

printf("Enter a year: \t");

scanf("%d", &year);

if (year%4==0) {

printf("%d is a leap year.\n", year);

} else {

printf("%d is not a leap year.\n", year);

Output:-

Enter a year: 2024

2024 is a leap year.


2. Write a C program that accepts an age of a person and finds whether
he/she is eligible to vote or not and he/she is eligible to drive or not.
Input:-
#include <stdio.h>

void main() {

int age;

printf("Enter your age: \t");

scanf("%d", &age);

//If the person is eligible to vote

if (age >= 18) {

printf("You are eligible to vote.\n");

} else {

printf("You are not eligible to vote.\n");

if (age >= 18) {

printf("You are eligible to drive.\n");

} else {

printf("You are not eligible to drive.\n");

Output:-
Enter your age: 19

You are eligible to vote.

You are eligible to drive.


3. Write a C program to check whether number entered is odd or even.
Input:-

#include <stdio.h>

void main()

int number;

printf("Enter a number: \t");

scanf("%d", &number);

if (number % 2 == 0) {

printf("%d is an even number.\n", number);

} else {

printf("%d is an odd number.\n", number);

Output:-
Enter a number: 5

5 is an odd number.
4. Write a C program to check whether number entered is divisible by 3
or not.
Input:-
#include <stdio.h>

void main() {

int number;

printf("Enter a number: ");

scanf("%d", &number);

if (number % 3 == 0)

printf("%d is divisible by 3.\n", number);

else {

printf("%d is not divisible by 3.\n", number);

Output:-
Enter a number: 57

57 is divisible by 3.
5. Write a C program to check whether number entered is divisible by 7
or not.
Input:-

#include <stdio.h>

void main()

int number;

printf("Enter a number:\t ");

scanf("%d", &number);

if (number % 7 == 0)

printf("%d is divisible by 7.\n", number);

else

printf("%d is not divisible by 7.\n", number);

Output:-

Enter a number: 49

49 is divisible by 7.
6. Write a C program to check whether number entered is divisible by 3
or 5 or both.
Input:-
#include <stdio.h>

void main()

int number;

printf("Enter a number: \t");

scanf("%d", &number);

if (number % 3 == 0 && number % 5 == 0)

printf("%d is divisible by both 3 and 5.\n", number);

else if (number % 3 == 0)

printf("%d is divisible by 3 but not by 5.\n", number);

else if (number % 5 == 0)

printf("%d is divisible by 5 but not by 3.\n", number);

else

printf("%d is neither divisible by 3 nor by 5.\n", number);

Output:-
Enter a number: 42

42 is divisible by 3 but not by 5.


7. Write a C program to check whether number entered is positive,
negative or zero.
Input:-
#include <stdio.h>

void main() {

int number;

printf("Enter a number: \t");

scanf("%d", &number);

if (number > 0)

printf("%d is a positive number.\n", number);

else if (number < 0)

printf("%d is a negative number.\n", number);

else

printf("%d is zero.\n", number);

Output:-
Enter a number: 5

5 is a positive number.
8. Write a C program to check greater among two numbers.
Input:-
#include <stdio.h>

void main()

int a, b;

printf("Enter the first number: \t");

scanf("%d", &a);

printf("Enter the second number: \t");

scanf("%d", &b);

if (a > b)

printf("%d is greater than %d.\n", a, b);

else if (a < b)

printf("%d is greater than %d.\n",b,a);

else

printf("%d and %d are equal.\n", a,b);

Output:-
Enter the first number: 5

Enter the second number: 6

6 is greater than 5.
9. Write a C program to check greater among three numbers.
Input:-
#include <stdio.h>

void main()

int a,b,c;

printf("Enter the first number: ");

scanf("%d", &a);

printf("Enter the second number: ");

scanf("%d", &b);

printf("Enter the third number: ");

scanf("%d", &c);

if (a >= b && a >= c)

printf("%d is the greatest among the three numbers.\n", a);

else if (b >= a && b>= c)

printf("%d is the greatest among the three numbers.\n", b);

else

printf("%d is the greatest among the three numbers.\n", c);

Output:-
Enter the first number: 8
Enter the second number: 9
Enter the third number: 7
9 is the greatest among the three numbers.
10. Write a C program to read marks for a student from user and grade
the student according to the following
Marks Grades
Above 80 O
70-79 A+
60-69 A
55-59 B+
50-54 B
45-49 C
40-45 D
Below 40 Fail

Input:-
#include <stdio.h>

void main()

int marks;

printf("Enter the marks: ");

scanf("%d", &marks);

if (marks > 80)

printf("Grade: O\n");

else if (marks >= 70 && marks <= 79)

printf("Grade: A+\n");

else if (marks >= 60 && marks <= 69)

printf("Grade: A\n");

}
else if (marks >= 55 && marks <= 59)

printf("Grade: B+\n");

else if (marks >= 50 && marks <= 54)

printf("Grade: B\n");

else if (marks >= 45 && marks <= 49)

printf("Grade: C\n");

else if (marks >= 40 && marks <= 44)

printf("Grade: D\n");

else

printf("Grade: Fail\n");

Output:-
Enter the marks: 65

Grade: A
11. Write a C program to compute the roots of a quadratic equation
ax2+bx+c=0 .
The roots are given by equation. (-b√b2-4ac)/2a
Input:-

#include<stdio.h>

void main()

float a,b,c,d,r1,r2;

printf("Enter the value of a,b and c\n");

scanf("%f%f%f",&a,&b,&c);

d=b*b-4*a*c;

if(d==0)

printf("The roots are real and equal\n");

r1=-b/(2*a);

r2=-b/(2*a);

printf("Root1 is %f and Root2 is %f",r1,r2);

else if(d>0)

printf("Roots are real and different \n");

r1=(-b+sqrt(d))/(2*a);

r2=(-b-sqrt(d))/(2*a);

printf("Root1 is %f and Root2 is %f",r1,r2);

else

printf("Roots are imaginary");

}
Output:-

Enter the value of a,b and c

Roots are real and different

Root1 is -0.232408 and Root2 is -1.434259


12. BEST power distribution company charges its domestic consumers as
follows: Consumption Units Rate
0-100 Rs. 100
101-200 Rs.100+Rs. 5 per unit above 100 units
201-400 Rs.200+Rs.10 unit above 200 units
401-600 Rs.250+Rs.15 per unit above 400 units
601& above Rs.300+Rs.20 per unit above 600 units
Write a C program to read customer number and power consumed and prints
the amount to be paid by the customer.
Input:-
#include <stdio.h>

void main() {

int customerNumber, units;

float bill;

printf("Enter the customer number: ");

scanf("%d", &customerNumber);

printf("Enter the number of consumption units: ");

scanf("%d", &units);

if (units <= 100) {

bill = 100;

} else if (units <= 200) {

bill = 100 + (units - 100) * 5;

} else if (units <= 400) {

bill = 200 + (units - 200) * 10;

} else if (units <= 600) {

bill = 250 + (units - 400) * 15;

} else {

bill = 300 + (units - 600) * 20;

printf("Customer Number: %d\n", customerNumber);

printf("Total bill for %d units is Rs. %.2f\n", units, bill);


}
Output:-
Enter the customer number: 20

Enter the number of consumption units: 4000

Customer Number: 20

Total bill for 4000 units is Rs. 68300.00

13. The admission to a professional course if marks in subjects are as


follows:If marks in Maths >= 60 and marks in Physics >= 60 marks in
Chemistry >= 60 and total in all 3 subjects >=200 or total in Maths and
Physics>=150.
Write a C program to accept marks out of 100 for 3 subjects and finds the candidate
is eligible or not.
Input:-
#include <stdio.h>

void main() {

int math, physics, chemistry, totalAllSubjects, totalMathPhysics;

printf("Enter marks in Mathematics: ");

scanf("%d", &math);

printf("Enter marks in Physics: ");

scanf("%d", &physics);

printf("Enter marks in Chemistry: ");

scanf("%d", &chemistry);

totalAllSubjects = math + physics + chemistry;

totalMathPhysics = math + physics;

if ((math >= 60 && physics >= 60 && chemistry >= 60 && totalAllSubjects >= 200) ||

(totalMathPhysics >= 150)) {

printf("The candidate is eligible for admission.\n");

} else {

printf("The candidate is not eligible for admission.\n");

}
Output:-
Enter marks in Mathematics: 76

Enter marks in Physics: 54

Enter marks in Chemistry: 85

The candidate is not eligible for admission.

14. Write a C program to enter month number between 1 and 12 and print
the correct month name otherwise print the appropriate error message.
(switch – case)
Input:-
#include <stdio.h>
int main() {
char choice;
float num1, num2, result;
printf("Enter operation choice (A/B/C/D/E): ");
scanf(" %c", &choice);
printf("Enter two numbers: ");
scanf("%f %f", &num1, &num2);
switch(choice) {
case 'A':
result = num1 + num2;
printf("Result of addition: %.2f\n", result);
break;
case 'B':
result = num1 - num2;
printf("Result of subtraction: %.2f\n", result);
break;
case 'C':
result = num1 * num2;
printf("Result of multiplication: %.2f\n", result);
break;
case 'D':
if (num2 != 0) {
result = num1 / num2;
printf("Result of division: %.2f\n", result);
} else {
printf("Error: Division by zero is not allowed.\n");
}
break;
case 'E':
if (num2 != 0) {
result = (int)num1 % (int)num2;
printf("Result of modulus: %d\n", (int)result);
} else {
printf("Error: Modulus by zero is not allowed.\n");
}
break;
default:
printf("Error: Invalid choice. Please enter A, B, C, D, or E.\n");
break;
}
}
Output:-
Enter operation choice (A/B/C/D/E): A

Enter two numbers: 8 9

Result of addition: 17.00


15. Write a C program to enter week day number between 1 and 7 and
print the correct week day name otherwise print the appropriate error
message. (switch – case)
Input:-
#include <stdio.h>

void main() {

int day;

printf("Enter weekday number (1-7): ");

scanf("%d", &day);

switch(day) {

case 1:

printf("Monday\n");

break;

case 2:

printf("Tuesday\n");

break;

case 3:

printf("Wednesday\n");

break;

case 4:

printf("Thursday\n");

break;

case 5:

printf("Friday\n");

break;

case 6:

printf("Saturday\n");

break;

case 7:

printf("Sunday\n");

break;
default:

printf("Error: Invalid day number. Please enter a number between 1 and 7.\n");

break;

Output:-
Enter weekday number (1-7): 5

Friday

16. Write a C program to perform any one of operations mentioned below


by taking choice as character: (switch – case)
A - Addition of two numbers
B – Subtraction of two numbers
C - Multiplication of two numbers
D - Division of two numbers
E –Mod between two numbers
Otherwise display the error message.
Input:-
#include <stdio.h>

int main() {

char choice;

float num1, num2, result;

printf("Enter operation choice (A/B/C/D/E): ");

scanf(" %c", &choice);

printf("Enter two numbers: ");

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

switch(choice) {

case 'A':

result = num1 + num2;

printf("Result of addition: %.2f\n", result);

break;
case 'B':

result = num1 - num2;

printf("Result of subtraction: %.2f\n", result);

break;

case 'C':

result = num1 * num2;

printf("Result of multiplication: %.2f\n", result);

break;

case 'D':

if (num2 != 0) {

result = num1 / num2;

printf("Result of division: %.2f\n", result);

} else {

printf("Error: Division by zero is not allowed.\n");

break;

case 'E':

if (num2 != 0) {

result = (int)num1 % (int)num2;

printf("Result of modulus: %d\n", (int)result);

} else {

printf("Error: Modulus by zero is not allowed.\n");

break;

default:

printf("Error: Invalid choice. Please enter A, B, C, D, or E.\n");

break;

}
Output:-
Enter operation choice (A/B/C/D/E): C

Enter two numbers: 5

Result of multiplication: 40.00

17. Write a C program for the menu driven which perform any one
operation specified below. (switch – case)
1. Check whether no. is odd or even.
2. Check whether no. is positive, negative or 0.
3. Check whether no. is divisible by 5 or not.
Otherwise display the appropriate error message.
Input:-
#include <stdio.h>

void main() {

int choice, num;

printf("Menu:\n");

printf("1. Check whether number is odd or even\n");

printf("2. Check whether number is positive, negative, or 0\n");

printf("3. Check whether number is divisible by 5 or not\n");

printf("Enter your choice (1/2/3): ");

scanf("%d", &choice);

printf("Enter a number: ");

scanf("%d", &num);

switch(choice) {

case 1:

if (num % 2 == 0) {

printf("The number %d is even.\n", num);

} else {

printf("The number %d is odd.\n", num);


}

break;

case 2:

if (num > 0) {

printf("The number %d is positive.\n", num);

} else if (num < 0) {

printf("The number %d is negative.\n", num);

} else {

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

break;

case 3:

if (num % 5 == 0) {

printf("The number %d is divisible by 5.\n", num);

} else {

printf("The number %d is not divisible by 5.\n", num);

break;

default:

printf("Error: Invalid choice. Please enter 1, 2, or 3.\n");

break;

Output:-
Menu:

1. Check whether number is odd or even

2. Check whether number is positive, negative, or 0

3. Check whether number is divisible by 5 or not

Enter your choice (1/2/3): 2

Enter a number: -41

The number -41 is negative.


18. Write a C program to find area or perimeter of a rectangle by asking
choice from user. (switch – case)
Input:-
#include <stdio.h>

void main() {

int choice;

float length, width, result;

printf("Menu:\n");

printf("1. Calculate area of the rectangle\n");

printf("2. Calculate perimeter of the rectangle\n");

printf("Enter your choice (1/2): ");

scanf("%d", &choice);

printf("Enter the length of the rectangle: ");

scanf("%f", &length);

printf("Enter the width of the rectangle: ");

scanf("%f", &width);

switch(choice) {

case 1:

result = length * width;

printf("The area of the rectangle is: %.2f\n", result);

break;

case 2:

result = 2 * (length + width);

printf("The perimeter of the rectangle is: %.2f\n", result);

break;

default:

printf("Error: Invalid choice. Please enter 1 or 2.\n");

break;

}
Output:-
Menu:

1. Calculate area of the rectangle

2. Calculate perimeter of the rectangle

Enter your choice (1/2): 1

Enter the length of the rectangle: 8

Enter the width of the rectangle: 9

The area of the rectangle is: 72.00


19. Write a C program to find square of a number or cube of a number by
asking choice from user. (switch – case)

Input:-
#include <stdio.h>

void main() {

int choice;

float number, result;

printf("Menu:\n");

printf("1. Calculate the square of a number\n");

printf("2. Calculate the cube of a number\n");


printf("Enter your choice (1/2): ");
scanf("%d", &choice);
printf("Enter a number: ");
scanf("%f", &number);
switch(choice) {
case 1:
result = number * number;
printf("The square of %.2f is: %.2f\n", number, result);
break;
case 2:
result = number * number * number;
printf("The cube of %.2f is: %.2f\n", number, result);
break;
default:
printf("Error: Invalid choice. Please enter 1 or 2.\n");
break;
}
}
Output:-
Menu:

1. Calculate the square of a number

2. Calculate the cube of a number

Enter your choice (1/2): 1

Enter a number: 29

The square of 29.00 is: 841.00

20. Write a C program to read two numbers from the user and read the
choice between 1 & 3 and do the following: (switch – case)
1. Greater among of two numbers
2. Smallest among of two numbers
Input:-
#include <stdio.h>

void main() {

int choice;

float num1, num2;

printf("Menu:\n");

printf("1. Find the greater of the two numbers\n");


printf("2. Find the smallest of the two numbers\n");

printf("Enter your choice (1/2): ");

scanf("%d", &choice);

printf("Enter the first number: ");

scanf("%f", &num1);

printf("Enter the second number: ");

scanf("%f", &num2);

switch(choice) {

case 1:

if (num1 > num2) {

printf("The greater number is: %.2f\n", num1);

} else if (num2 > num1) {

printf("The greater number is: %.2f\n", num2);

} else {

printf("Both numbers are equal.\n");

break;

case 2:

if (num1 < num2) {

printf("The smallest number is: %.2f\n", num1);

} else if (num2 < num1) {

printf("The smallest number is: %.2f\n", num2);

} else {

printf("Both numbers are equal.\n");

break;

default:

printf("Error: Invalid choice. Please enter 1 or 2.\n");

break;

}
Output:-

Menu:
1. Find the greater of the two numbers
2. Find the smallest of the two numbers
Enter your choice (1/2): 1
Enter the first number: 9
Enter the second number: 5
The greater number is: 9.00

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy