0% found this document useful (0 votes)
6 views13 pages

Assigenment #1

The document contains a series of programming exercises in C, each with a specific task such as calculating gross salary, converting distances, temperature conversions, and more. Each task includes a program code snippet and a sample result demonstrating the output for given inputs. The exercises cover various basic programming concepts including arithmetic operations, conditionals, loops, and input/output handling.

Uploaded by

raosaimriaz
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 views13 pages

Assigenment #1

The document contains a series of programming exercises in C, each with a specific task such as calculating gross salary, converting distances, temperature conversions, and more. Each task includes a program code snippet and a sample result demonstrating the output for given inputs. The exercises cover various basic programming concepts including arithmetic operations, conditionals, loops, and input/output handling.

Uploaded by

raosaimriaz
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/ 13

Q#1)

John’s basic salary is input through the keyboard. His dearness allowance is 40% of
basic salary, and house rent allowance is 20% of basic salary. Write a program to
calculate his gross salary.

PROGRAM:

#include <stdio.h>

int main()
{
float basic_salary, dearness_allowance, house_rent_allowance, gross_salary;

printf("Enter John's basic salary: "); scanf("%f", &basic_salary);

dearness_allowance = 0.4 * basic_salary;

house_rent_allowance = 0.2 * basic_salary;

gross_salary = basic_salary + dearness_allowance + house_rent_allowance;

printf("John's gross salary is: $%.2f\n", gross_salary);

return 0;
}

RESULT:

Enter John's basic salary: 60000


John's gross salary is: $96000.00

Q#2)
The distance between two cities (in km.) is input through the keyboard. Write a
program to convert and print this distance in meters, feet, inches and centimeters .

PROGRAM:

int main()
{
float distance_km, distance_m, distance_ft, distance_in, distance_cm;
printf("Enter the distance between the two cities (in kilometers): ");
scanf("%f", &distance_km);

distance_m = distance_km * 1000;

distance_ft = distance_km * 3280.84;

distance_in = distance_km * 39370.1;

distance_cm = distance_km * 100000

printf("Distance in meters: %.2f m\n", distance_m); printf("Distance in


feet: %.2f ft\n", distance_ft); printf("Distance in inches: %.2f in\n",
distance_in); printf("Distance in centimeters: %.2f cm\n", distance_cm);

return 0;
}

RESULT:

Enter the distance between the two cities (in kilometers): 200
Distance in meters: 200000.00 m
Distance in feet: 656168.00 ft
Distance in inches: 7874020.00 in
Distance in centimeters: 20000000.00 cm

Q#3)
Temperature of a city in Fahrenheit degrees is input through the keyboard. Write a
program to convert this temperature into Centigrade degrees.

PROGRAM:

int main()
{
float temp_fahrenheit, temp_celsius;

printf("Enter the temperature in Fahrenheit: "); scanf("%f",


&temp_fahrenheit);

temp_celsius = (temp_fahrenheit - 32) * 5 / 9;


printf("Temperature in Celsius: %.2f°C\n", temp_celsius);

return 0;
}

RESULT:

Enter the temperature in Fahrenheit: 104


Temperature in Celsius: 40.00░C

Q#4)
The length & breadth of a rectangle and radius of a circle are input through the
keyboard. Write a program to calculate the area & perimeter of the rectangle, and the
area & circumference of the circle.

PROGRAM:

int main()
{
float length, breadth, radius; float rectangle_area, rectangle_perimeter;
float circle_area, circle_circumference;

printf("Enter the length of the rectangle: "); scanf("%f", &length);


printf("Enter the breadth of the rectangle: ");
scanf("%f", &breadth);

rectangle_area = length * breadth;


rectangle_perimeter = 2 * (length + breadth);

printf("Enter the radius of the circle: ");


scanf("%f", &radius);

circle_area = M_PI * pow(radius, 2); circle_circumference = 2 * M_PI *


radius;

printf("\nRectangle:\n"); printf("Area: %.2f square units\n",


rectangle_area); printf("Perimeter: %.2f units\n\n", rectangle_perimeter);

printf("Circle:\n");
printf("Area: %.2f square units\n", circle_area);
printf("Circumference: %.2f units\n", circle_circumference);

return 0;
}
RESULT:

Enter the length of the rectangle: 23


Enter the breadth of the rectangle: 30
Enter the radius of the circle: 15

Rectangle:
Area: 690.00 square units
Perimeter: 106.00 units

Circle:
Area: 706.86 square units
Circumference: 94.25 units

Q#5)
Two numbers are input through the keyboard into two locations C and D. Write a
program to interchange the contents of C and D.

PROGRAM:

#include <stdio.h>

int main()
{ int C, D, temp;

printf("Enter the value for C: "); scanf("%d", &C);


printf("Enter the value for D: ");
scanf("%d", &D);

temp = C;
= D;
= temp;

printf("After swapping:\n"); printf("C = %d\n", C); printf("D = %d\n",


D);

return 0;
}

RESULT:

Enter the value for C: 40


Enter the value for D: 20 After swapping:
= 20
= 40

Q#6)
If a five-digit number is input through the keyboard, write a program to calculate the
sum of its digits.
(Hint: Use the modulus operator ‘%’)

PROGRAM:

int main()
{
int num, digit, sum = 0;

printf("Enter a five-digit number: ");


scanf("%d", &num);

digit = num % 10;


sum += digit;

num /= 10; digit = num % 10;


sum += digit;

num /= 10; digit = num % 10;


sum += digit;

num /= 10; digit = num % 10;


sum += digit;

num /= 10; digit = num % 10;


sum += digit;

printf("Sum of the digits: %d\n", sum);

return 0;
}
RESULT:

Enter a five-digit number: 12345


Sum of the digits: 15

Q#7)
If a five-digit number is input through the keyboard, write a program to reverse the
number.

PROGRAM:

int main() {
int original_num, reversed_num = 0;

printf("Enter a five-digit number: ");


scanf("%d", &original_num);

while (original_num > 0) { int digit = original_num % 10;


reversed_num = reversed_num * 10 + digit; original_num /= 10;
}

printf("Reversed number: %d\n", reversed_num);

return 0;
}
RESULT:

Enter a five-digit number: 12345


Reversed number: 54321

Q#8)
If a four-digit number is input through the keyboard, write a program to obtain the
sum of the first and last digit of this number.

PROGRAM:

int main() {
int num, first_digit, last_digit, sum;

printf("Enter a four-digit number: ");


scanf("%d", &num);
first_digit = num / 1000;

last_digit = num % 10;

sum = first_digit + last_digit;

printf("Sum of the first and last digits: %d\n", sum);

return 0;
}

RESULT:

Enter a four-digit number: 54321


Sum of the first and last digits: 55

Q#9)
In a town, the percentage of men is 52. The percentage of total literacy is 48. If total
percentage of literate men is 35 of the total population, write a program to find the
total number of illiterate men and women if the population of the town is 80,000.

PROGRAM:

int main() {
int total_population = 80000; int percent_men = 52; int percent_literacy =
48;
int percent_literate_men = 35;

int men = (percent_men * total_population) / 100;

int literate_men = (percent_literate_men * total_population) / 100;

int illiterate_men = men - literate_men;

int women = total_population - men;

int illiterate_women = percent_literacy * women / 100;

int total_illiterate = illiterate_men + illiterate_women;


printf("Total illiterate men: %d\n", illiterate_men); printf("Total illiterate
women: %d\n", illiterate_women); printf("Total illiterate people: %d\n",
total_illiterate);

return 0;
}

RESULT:

Total illiterate men: 13600


Total illiterate women: 18432
Total illiterate people: 32032

Q#10)
A cashier has currency notes of denominations 10, 50 and
100. If the amount to be withdrawn is input through the keyboard in hundreds, find the
total number of currency notes of each denomination the cashier will have to give to
the withdrawer.

PROGRAM:

int main() {
int amount_in_hundreds, num_100_notes, num_50_notes, num_10_notes;

printf("Enter the amount to be withdrawn (in hundreds): ");


scanf("%d", &amount_in_hundreds);
num_100_notes = amount_in_hundreds;

int remaining_amount = amount_in_hundreds % 100;

num_50_notes = remaining_amount / 50;

remaining_amount %= 50;

num_10_notes = remaining_amount / 10;

printf("Number of 100-rupee notes: %d\n", num_100_notes);


printf("Number of 50-rupee notes: %d\n", num_50_notes); printf("Number of
10-rupee notes: %d\n", num_10_notes);

return 0;
}
RESULT:

Enter the amount to be withdrawn (in hundreds): 650


Number of 100-rupee notes: 650
Number of 50-rupee notes: 1
Number of 10-rupee notes: 0

Q#11)
If the total selling price of 15 items and the total profit earned on them is input
through the

PROGRAM:

int main() {
float selling_price, profit, cost_price;

printf("Enter the total selling price of 15 items: ");


scanf("%f", &selling_price);
printf("Enter the total profit earned: ");
scanf("%f", &profit);

cost_price = (selling_price - profit) / 15;

printf("The cost price of one item is %.2f\n", cost_price);

return 0;
}

RESULT:

Enter the total selling price of 15 items: 1450


Enter the total profit earned: 600
The cost price of one item is 56.67

Q#12)
If a five-digit number is input through the keyboard, write a program to print a new
number by adding one to each of its digits. For example if the number that is input is
12391 then the output should be displayed as 23402.
PROGRAM:

int main() {
int originalNumber, newNumber = 0;
int digit, modifiedDigit;

printf("Enter a five-digit number: ");


scanf("%d", &originalNumber);

for (int i = 0; i < 5; ++i) {


digit = originalNumber % 10;
modifiedDigit = (digit + 1) % 10;
newNumber = newNumber * 10 + modifiedDigit;
originalNumber /= 10;
}

printf("New number after adding 1 to each digit: %d\n", newNumber);

return 0;
}

RESULT:

Enter a five-digit number: 78945


New number after adding 1 to each digit: 65098

Q#13)
While purchasing certain items, a discount of 10% is offered if the quantity purchased
is more than 1000. If quantity and price per item are input through the keyboard, write
a program to calculate the total expenses.

PROGRAM:

int main() {
int quantity;
float pricePerItem, totalExpenses;

printf("Enter the quantity: ");


scanf("%d", &quantity);

printf("Enter the price per item: ");


scanf("%f", &pricePerItem);

totalExpenses = quantity * pricePerItem;

if (quantity > 1000) {


totalExpenses *= 0.9;
}

printf("Total expenses: $%.2f\n", totalExpenses);

return 0;
}

RESULT:

Enter the quantity: 25


Enter the price per item: 12
Total expenses: $300.00

Q#14)
If his basic salary is less than Rs. 1500, then HRA = 10% of basic salary and DA = 90% of basic
salary. If his salary is either equal to or above Rs. 1500, then HRA = Rs. 500 and DA = 98% of basic
salary. If the employee's salary is input through the keyboard write a program to find his gross salary

PROGRAM:

int main() {
float basicSalary, hra, da, grossSalary;

printf("Enter the basic salary: ");


scanf("%f", &basicSalary);

if (basicSalary < 1500) {


hra = 0.10 * basicSalary;
da = 0.90 * basicSalary;
} else {
hra = 500;
da = 0.98 * basicSalary;
}
grossSalary = basicSalary + hra + da;

printf("Gross Salary: Rs. %.2f\n", grossSalary);

return 0;
}

RESULT:

Enter the basic salary: 14500


Gross Salary: Rs. 29210.00

Q#15)
If cost price and selling price of an item is input through the keyboard, write a
program to determine whether the seller has made profit or incurred loss. Also
determine how much profit he made loss he incurred

PROGRAM:

int main() {
float costPrice, sellingPrice, profitOrLoss;

printf("Enter cost price: ");


scanf("%f", &costPrice);

printf("Enter selling price: ");


scanf("%f", &sellingPrice);

profitOrLoss = sellingPrice - costPrice;

if (profitOrLoss > 0) {
printf("Congratulations! You made a profit of $%.2f\n", profitOrLoss);
} else if (profitOrLoss < 0) {
printf("Oops! You incurred a loss of $%.2f\n", -profitOrLoss);
} else {
printf("No profit, no loss. You broke even!\n");
}

return 0;
}
RESULT:

Enter cost price: 500


Enter selling price: 750
Congratulations! You made a profit of $250.00

You might also like