Program Control Structures - Decision Making & Branching
Question 01
Swap two values stored in two different variables.
C Code
#include <stdio.h>
int main()
{
int a, b, temp;
printf("Enter value of a: ");
scanf("%d", &a);
printf("Enter value of b: ");
scanf("%d", &b);
printf("Before swapping: a = %d, b = %d\n", a, b);
// Swapping logic
temp = a;
a = b;
b = temp;
printf("After swapping: a = %d, b = %d\n", a, b);
return 0;
}
Result
Enter value of a: 5
Enter value of b: 4
Before swapping: a = 5, b = 4
After swapping: a = 4, b = 5
Explanation
* Any value can be used for a and b when running the above code. According to the above C code, we entered '5' for 'a' and '4' for 'b'. After the swapping, 'a' become '10'and 'b' become '5', which is reflected in the output.
Question 02
Check whether an entered number is negative, positive or zero.
C Code
#include <stdio.h>
int main ()
{
int number;
printf ("Enter a number:");
Scanf ("%d", &number);
if (number > 0)
{
printf ("number is positive.\n");
}
else if (number < 0)
{
printf ("Number is negative.\n");
}
else
{
printf (" Number is zero.");
}
return 0;
}
Result
Enter a number : 1
Number is positive.
Enter a number : -5
Number is negative.
Enter a number : 0.1
Number is zero.
Explanation
*Any number can be entered. When check the entered number using an "if, else if, else" construct:
- if the number is greater than "0", it prints that the number is positive.
- else if the number is less than "0", it prints that number is negative.
- else the number is not greater than "0", or not less than "0", it prints the number is zero.
Question 03
Check whether an entered year is leap year or not.
C Code
#include <stdio.h>
int main()
{
int year;
printf("Enter a year: ");
scanf("%d", &year);
// Leap year condition
if ((year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0)))
{
printf("%d is a leap year.\n", year);
}
else
{
printf("%d is not a leap year.\n", year);
}
return 0;
}
Result
Enter a year: 2024
2024 is a leap year.
Enter a year: 2023
2024 is not a leap year.
Explanation
*Any year can be entered. According to the leap year rules:
- if the year is divisible by 400, it is a leap year.
- if the year is divisible by 4, but not divisible by 100, it is also a leap year.
If the conditions are met, it prints that the entered year is a leap year; otherwise it prints that it is not a leap year.
When you run this program and enter year, it will tell you whether the year is a leap year or not.
Question 04
Write a program that asks the user to type in two integer values at the terminal. Test these
two numbers to determine if the first is evenly divisible by the second, and then display
an appropriate message at the terminal.
C Code
#include <stdio.h>
int main()
{
int num1, num2;
printf("Please enter two integer numbers: ");
scanf("%d %d", &num1, &num2);
if (num2 == 0)
{
printf("Division by zero is not allowed.\n");
}
else if (num1 % num2 == 0)
{
printf("%d is evenly divisible by %d.\n", num1, num2);
}
else
{
printf("%d is not evenly divisible by %d.\n", num1, num2);
}
return 0;
}
Result
Please enter two integer numbers: 2
4
2 is not evenly divisible by 4.
Please enter two integer numbers: 4
2
4 is evenly divisible by 2.
Please enter two integer values: 4
0
Error.
Explanation
*This program asks for two numbers. Any value can be used for these integer values. It the checks if the second number is zero. If it is, display an error. Otherwise, it checks if the first number is evenly divisible by the second number, it prints evenly divisible. Otherwise, it prints not evenly divisible.
Question 05
Write a program that accepts two integer values typed in by the user. Display the result of
dividing the first integer by the second, to three-decimal-place accuracy. Remember to
have the program check for division by zero.
C Code
#include <stdio.h>
int main()
{
int num1, num2;
float result;
printf("Enter two integer numbers: ");
scanf("%d %d", &num1, &num2);
if (num2 == 0)
{
printf("Error: Division by zero is not allowed.\n");
}
else
{
result = (float)num1 / num2;
printf("%d divided by %d is %.3f.\n", num1, num2, result);
}
return 0;
}
Result
Enter two integer numbers: 0
4
0 divided by 4 is 0.000.
Enter two integer numbers: 4
0
Error: Division by zero is not allowed.
Enter two integer numbers: 3
2
3 divided by 2 is 1.500.
Explanation
*Any value can be used. This program asks for two numbers and checks if the second number is zero. If it is, it displays error. Otherwise, it divides the first number by the second number, using floating point division to ensure accuracy and prints the result to three decimal places.
Question 06
Write a program that takes an integer keyed in from the terminal and extracts and
displays each digit of the integer in English. So, if the user types in 932, the program
should display nine three two. Remember to display “zero” if the user types in just a 0.
C Code
#include <stdio.h>
void printDigitInEnglish(int digit)
{
switch(digit)
{
case 0:
printf("zero");
break;
case 1:
printf("one");
break;
case 2:
printf("two");
break;
case 3:
printf("three");
break;
case 4:
printf("four");
break;
case 5:
printf("five");
break;
case 6:
printf("six");
break;
case 7:
printf("seven");
break;
case 8:
printf("eight");
break;
case 9:
printf("nine");
break;
}
}
int main()
{
int number;
printf("Enter an integer number: ");
scanf("%d", &number);
if (number == 0)
{
printf("zero\n");
}
else
{
// Extract and print each digit in English
int reversedNumber = 0;
while (number > 0)
{
int digit = number % 10;
reversedNumber = reversedNumber * 10 + digit;
number /= 10;
}
while (reversedNumber > 0)
{
int digit = reversedNumber % 10;
printDigitInEnglish(digit);
/* Add space if there are more digits*/
if (reversedNumber / 10 > 0)
{
printf(" ");
}
reversedNumber /= 10;
}
printf("\n");
}
return 0;
}
Result
Enter an integer number: 456
four five six
Enter an integer number: 70
seven zero
Enter an integer number: 9
nine
Explanation
*This program first prompts the user to enter an integer number. It then check if the entered number is zero. If it ids, it print zero and terminates. Otherwise, it extracts each digit of the integer, reverses the order and prints each digit.
Question 07
Input marks of five subjects Physics, Chemistry, Biology, Mathematics and Computer.
Calculate percentage and grade according to following:
a. Percentage >= 90% : Grade A
b. Percentage >= 80% : Grade B
c. Percentage >= 70% : Grade C
d. Percentage >= 60% : Grade D
e. Percentage >= 40% : Grade E
f. Percentage < 40% : Grade F
C Code
#include <stdio.h>
int main()
{
float physics, chemistry, biology, mathematics, computer;
float percentage;
char grade;
/* Input marks of five subjects*/
printf("Enter marks of Physics: ");
scanf("%f", &physics);
printf("Enter marks of Chemistry: ");
scanf("%f", &chemistry);
printf("Enter marks of Biology: ");
scanf("%f", &biology);
printf("Enter marks of Mathematics: ");
scanf("%f", &mathematics);
printf("Enter marks of Computer: ");
scanf("%f", &computer);
/* Calculate percentage*/
percentage = (physics + chemistry + biology + mathematics + computer) / 5.0;
/* Determine grade*/
if (percentage >= 90)
{
grade = 'A';
}
else if (percentage >= 80)
{
grade = 'B';
}
else if (percentage >= 70)
{
grade = 'C';
}
else if (percentage >= 60)
{
grade = 'D';
}
else if (percentage >= 40)
{
grade = 'E';
}
else
{
grade = 'F';
}
/*Display percentage and grade*/
printf("Percentage: %.2f%%\n", percentage);
printf("Grade: %c\n", grade);
return 0;
}
Result
Enter marks of Physics: 85
Enter marks of Chemistry: 86
Enter marks of Biology: 88
Enter marks of Mathematics: 89
Enter marks of Computer: 90
Percentage: 87.60%
Grade: B
Explanation
This program first prompts the user to enter marks of five subjects: Physics, Chemistry, Biology, Mathematics and Computer. It then calculates the percentage using the formula '(Physics + Chemistry + Biology + Mathematics + Computer)/5.0'. Based on the calculated percentage, it determines the grade according to the specified criteria and prints the percentage and grade.
Question 08
Input basic salary of an employee and calculate its Gross salary according to following:
(note: HRA and DA are allowances)
a. Basic Salary <= 10000 : HRA = 20%, DA = 80%
b. Basic Salary <= 20000 : HRA = 25%, DA = 90%
c. Basic Salary > 20000 : HRA = 30%, DA = 95%
C Code
#include <stdio.h>
int main()
{
float basic salary, gross_salary;
float hra, da;
/*Input basic salary*/
printf("Enter basic salary: ");
scanf("%f", &basic_salary);
/*Calculate HRA and DA based on basic salary*/
if (basic_salary <= 10000)
{
hra = 0.2 * basic_salary;
da = 0.8 * basic_salary;
}
else if (basic_salary <= 20000)
{
hra = 0.25 * basic_salary;
da = 0.9 * basic_salary;
}
else
{
hra = 0.3 * basic_salary;
da = 0.95 * basic_salary;
}
/*Calculate gross salary*/
gross_salary = basic_salary + hra + da;
/*Display gross salary*/
printf("Gross Salary: %.2f\n", gross_salary);
return 0;
}
Result
Enter basic salary: 18000
Gross Salary: 38700.00
Explanation
*This program first prompts the user to enter the basic alary of an employee. Then it calculates the house rent allowance(hra) and dearness allowance (da) based on the provided basic salary. After that, it calculates the gross salary by adding the basic salary, hra and da. Finally it displays the gross salary.
Question 09
Write a program that acts as a simple “printing” calculator. The program should allow the
user to type in expressions of the form number operator: The following operators should
be recognized by the program: + - * / S E
The S operator tells the program to set the “accumulator” to the typed-in number.
The E operator tells the program that execution is to end.
The arithmetic operations are performed on the contents of the accumulator with the
number that was keyed in acting as the second operand. The following is a “sample run”
showing how the program should operate:
Begin Calculations
10 S Set Accumulator to 10
= 10.000000 Contents of Accumulator
2 / Divide by 2
= 5.000000 Contents of Accumulator
55 - Subtract 55
-50.000000
100.25 S Set Accumulator to 100.25
= 100.250000
4 * Multiply by 4
= 401.000000
0 E End of program
= 401.000000
End of Calculations.
Make certain that the program detects division by zero and also checks for unknown
operators.
C Code
Result
Explanation
Question 10
Input electricity unit charges and calculate total electricity bill according to the given
condition:
a. For first 50 units Rs. 0.50/unit
b. For next 100 units Rs. 0.75/unit
c. For next 100 units Rs. 1.20/unit
d. For unit above 250 Rs. 1.50/unit
e. An additional surcharge of 20% is added to the bill
C Code
#include <stdio.h>
int main()
{
float units, bill = 0, surcharge;
/*Input electricity units*/
printf("Enter the electricity units consumed: ");
scanf("%f", &units);
/*Calculate bill according to given conditions*/
if (units <= 50)
{
bill = units * 0.50;
}
else if (units <= 150)
{
bill = 50 * 0.50;
bill += (units - 50) * 0.75;
}
else if (units <= 250)
{
bill = 50 * 0.50 + 100 * 0.75;
bill += (units - 150) * 1.20;
}
else
{
bill = 50 * 0.50 + 100 * 0.75 + 100 * 1.20;
bill += (units - 250) * 1.50;
}
/*Calculate surcharge*/
surcharge = bill * 0.20;
/*Add surcharge to the bill*/
bill += surcharge;
printf("Total electricity bill: Rs. %.2f\n", bill);
return 0;
}
Result
Enter the electricity units consumed: 456
Total electricity bill: Rs. 634.80
Explanation
* Any value can be entered for the electricity units consumed. This program takes input for the electricity units consumed and calculates the bill according to the given conditions. Then it calculates the surcharge (20% of the bill) and adds it to the total bill. Finally, it displays the total electricity bill.
Question 11
An envelope manufacturing company hires people to make envelopes. They provide all
the raw material needed and pay at the following rates. Write a program to input the no of
envelopes made and to calculate and print the amount due
Envelopes Rate
1-1000 75 cents
1001-1500 1 rupee
1501-2000 1 rupee and 15 cents
2001- 1 rupee and 25 cents
C Code
#include <stdio.h>
int main()
{
int num_envelopes;
float amount_due = 0;
/*Input the number of envelopes made*/
printf("Enter the number of envelopes made: ");
scanf("%d", &num_envelopes);
/*Calculate amount due based on the number of envelopes*/
if (num_envelopes <= 1000)
{
amount_due = num_envelopes * 0.75;
}
else if (num_envelopes <= 1500)
{
amount_due = 1000 * 0.75 + (num_envelopes - 1000) * 1.0;
}
else if (num_envelopes <= 2000)
{
amount_due = 1000 * 0.75 + 500 * 1.0 + (num_envelopes - 1500) * 1.15;
}
else
{
amount_due = 1000 * 0.75 + 500 * 1.0 + 500 * 1.15 + (num_envelopes - 2000) * 1.25;
}
printf("Amount due: %.2f\n", amount_due);
return 0;
}
Result
Enter the number of envelopes made: 1265
Amount due: 1015.00
Explanation
*Any value can be entered for the number of envelop. This program takes input for the number of envelopes made and calculates the amount due based on the provided rates. Then it prints the amount due.
Question 12
Find the number of separate Notes and coins required to represent a given monetary
value. E,g, 2700 required 1 ➡ 2000 note, 1➡ 500 note and 2➡100 notes.
C Code
#include <stdio.h>
int main()
{
int amount, remaining_amount;
int notes_2000, notes_500, notes_100, coins_50, coins_20, coins_10, coins_5, coins_2, coins_1;
/*Input the monetary value*/
printf("Enter the monetary value: ");
scanf("%d", &amount);
/*Calculate the number of each denomination*/
notes_2000 = amount / 2000;
remaining_amount = amount % 2000;
notes_500 = remaining_amount / 500;
remaining_amount %= 500;
notes_100 = remaining_amount / 100;
remaining_amount %= 100;
coins_50 = remaining_amount / 50;
remaining_amount %= 50;
coins_20 = remaining_amount / 20;
remaining_amount %= 20;
coins_10 = remaining_amount / 10;
remaining_amount %= 10;
coins_5 = remaining_amount / 5;
remaining_amount %= 5;
coins_2 = remaining_amount / 2;
remaining_amount %= 2;
coins_1 = remaining_amount;
/*Output the results*/
printf("Number of notes and coins required:\n");
printf("2000 notes: %d\n", notes_2000);
printf("500 notes: %d\n", notes_500);
printf("100 notes: %d\n", notes_100);
printf("50 coins: %d\n", coins_50);
printf("20 coins: %d\n", coins_20);
printf("10 coins: %d\n", coins_10);
printf("5 coins: %d\n", coins_5);
printf("2 coins: %d\n", coins_2);
printf("1 coins: %d\n", coins_1);
return 0;
}
Result
Enter the monetary value: 25679
Number of notes and coins required:
2000 notes: 12
500 notes: 3
100 notes: 1
50 coins: 1
20 coins: 1
10 coins: 0
5 coins: 1
2 coins: 2
1 coins: 0
Explanation
*Any monetary value can be entered. This program takes input for the monetary value and the calculates the number of each denomination of notes and coins needed to represent that value. Finally, it prints out the number of notes and coins required.
Question 13
Display Age, Birthday, and Gender using a given National Identity Card number.
No comments:
Post a Comment