1. Swap two values stored in two different variables.
Answer :
#include <stdio.h>
int main()
{
int x, y, temp;
printf("Enter 2 numbers: \n");
scanf("%i %i", &x, &y);
printf("num 1= %i\n", x);
printf("num 2= %i\n", y);
temp = x;
x = y;
y = temp;
printf("After swapping: \n");
printf("num 1= %i\n", x);
printf("num 2= %i\n", y);
return 0;
}
2. Check whether an entered number is negative, positive or zero.
Answer :
#include <stdio.h>
int main ()
{
int num;
printf("Enter a number: ");
scanf("%i", &num);
if (num>0)
{
printf("The number you entered is a positive number");
}
else if (num<0)
{
printf("The number you entered is a negative number");
}
else if (num==0)
{
printf("The number you entered is zero");
}
return 0;
}
3. Check whether an entered year is leap year or not.
Answer :
#include <stdio.h>
int main ()
{
int year;
printf("Enter a year: \n");
scanf("%i", &year);
if (year % 4 == 0)
{
printf("The year you entered is a leap year");
}
else
{
printf("The year you entered is not a leap year");
}
return 0;
}
4. 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.
Answer :
#include
<stdio.h>
int main
(void)
{
int number1,number2;
printf("Enter the firs
number");
scanf("%i",&number1);
printf("Enter the second
number");
scanf("%i",&number2);
if (number2 !=0){
if (number1 % number2 ==
0)
{
printf("%i
is evenly divisible by %i\n",number1,number2);
} else {
printf("%i
is not evenely divisible by %i\n",number1,number2);
}
} else
{
printf ("Number 2
is zero");
}
return 0;
}
5. 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.
Answer :
#include
<stdio.h>
int main()
{
int
num1, num2;
float result;
printf("Enter the first integer: ");
scanf("%i",
&num1);
printf("Enter
the second integer: ");
scanf("%i", &num2);
if (num2 != 0) {
result = (float)num1 / num2;
printf("Result of %i divided by %i is:
%.3f\n", num1, num2, result);
}
else {
printf("Error: Cannot divide by zero.
Please enter a non-zero second integer.\n");
}
return 0;
}
6. 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.
Answer
#include
<stdio.h>
int main()
{
int
number, digit;
printf("Enter
an integer: ");
scanf("%d",
&number);
if
(number == 0) {
printf("zero\n");
}
else {
while
(number > 0) {
digit
= number % 10;
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;
}
number /= 10;
}
}
printf("\n"); // Add a newline for better formatting
return 0;
}
7. 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
Answer
#include
<stdio.h>
int main()
{
float physics, chemistry, biology,
mathematics, computer,totalMarks,percentage;
printf("Enter marks for Physics:
");
scanf("%f", &physics);
printf("Enter marks for Chemistry:
");
scanf("%f", &chemistry);
printf("Enter marks for Biology:
");
scanf("%f", &biology);
printf("Enter marks for Mathematics:
");
scanf("%f", &mathematics);
printf("Enter marks for Computer:
");
scanf("%f", &computer);
totalMarks = physics + chemistry + biology
+ mathematics + computer;
percentage = (totalMarks / 500) * 100;
printf("Percentage: %.2f%%\n",
percentage);
printf("Grade: ");
if (percentage >= 90) {
printf("A\n");
} else if (percentage >= 80) {
printf("B\n");
} else if (percentage >= 70) {
printf("C\n");
} else if (percentage >= 60) {
printf("D\n");
} else if (percentage >= 40) {
printf("E\n");
} else {
printf("F\n");
}
return 0;
}
8. 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%
Answer
#include
<stdio.h>
int main()
{
float
basicSalary;
float hra, da, grossSalary;
printf("Enter
the basic salary: ");
scanf("%f",
&basicSalary);
if
(basicSalary <= 10000) {
hra
= 0.2 * basicSalary;
da
= 0.8 * basicSalary;
} else
if (basicSalary <= 20000) {
hra = 0.25 * basicSalary;
da
= 0.9 * basicSalary;
}
else {
hra = 0.3 * basicSalary;
da
= 0.95 * basicSalary;
}
grossSalary
= basicSalary + hra + da;
printf("HRA:
%.2f\n", hra);
printf("DA:
%.2f\n", da);
printf("Gross Salary: %.2f\n",
grossSalary);
return
0;
}
9. 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.
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
Answer
#include
<stdio.h>
int main()
{
float unitCharges, totalBill;
printf("Enter the electricity unit
charges: ");
scanf("%f", &unitCharges);
if (unitCharges <= 50) {
totalBill = unitCharges * 0.50;
} else if (unitCharges <= 150) {
totalBill = 50 * 0.50 + (unitCharges -
50) * 0.75;
} else if (unitCharges <= 250) {
totalBill = 50 * 0.50 + 100 * 0.75 +
(unitCharges - 150) * 1.20;
} else {
totalBill = 50 * 0.50 + 100 * 0.75 +
100 * 1.20 + (unitCharges - 250) * 1.50;}
totalBill += totalBill * 0.20;
printf("Total Electricity Bill: Rs.
%.2f\n", totalBill);
return 0;
}
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
Answer
#include
<stdio.h>
int main()
{
int numEnvelopes;
float amountDue;
printf("Enter the number of envelopes
made: ");
scanf("%i", &numEnvelopes);
if (numEnvelopes <= 1000) {
amountDue = numEnvelopes * 0.75;
} else if (numEnvelopes <= 1500) {
amountDue = 1000 * 0.75 + (numEnvelopes
- 1000) * 1.0;
} else if (numEnvelopes <= 2000) {
amountDue = 1000 * 0.75 + 500 * 1.0 +
(numEnvelopes - 1500) * 1.15;
} else {
amountDue = 1000 * 0.75 + 500 * 1.0 +
500 * 1.15 + (numEnvelopes - 2000) * 1.25;
}
printf("Amount Due: %.2f
rupees\n", amountDue);
return 0;
}
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 2100 notes.
Answer
#include
<stdio.h>
int main()
{
int amount, notes2000, notes500, notes100,
coins50, coins25, coins10, coins5, coins1;
printf("Enter the monetary value in
rupees: ");
scanf("%d", &amount);
notes2000 = amount / 2000;
amount %= 2000;
notes500
= amount / 500;
amount %= 500;
notes100 = amount / 100;
amount %= 100;
coins50 = amount / 50;
amount %= 50;
coins25 = amount / 25;
amount %= 25;
coins10 = amount / 10;
amount %= 10;
coins5 = amount / 5;
amount %= 5;
coins1 = amount;
printf("Number of Notes and
Coins:\n");
printf("2000 Rupee Notes: %i\n",
notes2000);
printf("500 Rupee Notes: %i\n",
notes500);
printf("100 Rupee Notes: %i\n",
notes100);
printf("50 Rupee Coins: %i\n",
coins50);
printf("25 Rupee Coins: %i\n",
coins25);
printf("10 Rupee Coins: %i\n",
coins10);
printf("5 Rupee Coins: %i\n",
coins5);
printf("1 Rupee Coins: %i\n", coins1);
return 0;
}
13. Display Age, Birthday, and Gender using a given National Identity Car