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

CP Lab Assignments Upto Module-2

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

CP Lab Assignments Upto Module-2

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

Module-1

1. Find the area of a circle.

#include <stdio.h>
#define PI 3.14159

int main()
{
float radius, area;

// Input the radius from the user


printf("Enter the radius of the circle: ");
scanf("%f", &radius);

// Calculate the area


area = PI * radius * radius;

// Output the result


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

return 0;
}

2. Enter the length and breadth of a rectangle and find the area.
#include <stdio.h>

int main()
{
float length, breadth, area;

// Input the length and breadth from the user


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

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


scanf("%f", &breadth);

// Calculate the area


area = length * breadth;

// Output the result


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

return 0;
}
3. Find the average of 3 entered numbers.

#include <stdio.h>

int main()
{
float num1, num2, num3, average;

// Input three numbers from the user


printf("Enter the first number: ");
scanf("%f", &num1);

printf("Enter the second number: ");


scanf("%f", &num2);

printf("Enter the third number: ");


scanf("%f", &num3);

// Calculate the average


average = (num1 + num2 + num3) / 3;

// Output the result


printf("The average of the three numbers is: %.2f\n", average);

return 0;
}

4. Enter the CGPA obtained by a student and find the equivalent percentage of marks.

#include <stdio.h>

int main()
{
float cgpa, percentage;

// Input the CGPA from the user


printf("Enter the CGPA obtained by the student: ");
scanf("%f", &cgpa);

// Convert CGPA to percentage


percentage = cgpa * 9.5;

// Output the equivalent percentage


printf("The equivalent percentage of marks is: %.2f%%\n", percentage);

return 0;
}
5. Swap the values of two variables using a third variable.

#include <stdio.h>

int main()
{
int a, b, temp;

// Input the values of a and b


printf("Enter the value of a: ");
scanf("%d", &a);

printf("Enter the value of b: ");


scanf("%d", &b);

// Display the values before swapping


printf("\nBefore swapping: a = %d, b = %d\n", a, b);

// Swap the values using a temporary variable


temp = a;
a = b;
b = temp;

// Display the values after swapping


printf("After swapping: a = %d, b = %d\n", a, b);

return 0;
}

6. Swap the values of two variables without using a third variable.

#include <stdio.h>

int main()
{
int a, b;

// Input the values of a and b


printf("Enter the value of a: ");
scanf("%d", &a);

printf("Enter the value of b: ");


scanf("%d", &b);

// Display the values before swapping


printf("\nBefore swapping: a = %d, b = %d\n", a, b);

// Swap the values without using a third variable


a = a + b; // Step 1: a becomes the sum of a and b
b = a - b; // Step 2: b becomes the original value of a
a = a - b; // Step 3: a becomes the original value of b

// Display the values after swapping


printf("After swapping: a = %d, b = %d\n", a, b);

return 0;
}

7. Find the sum of the individual digits of any entered three-digit positive number.

#include <stdio.h>

int main()
{
int number, digit1, digit2, digit3, sum;

// Input a three-digit number from the user


printf("Enter a three-digit positive number: ");
scanf("%d", &number);

// Extract the digits


digit1 = number / 100; // First digit (hundreds place)
digit2 = (number / 10) % 10; // Second digit (tens place)
digit3 = number % 10; // Third digit (units place)

// Calculate the sum of the digits


sum = digit1 + digit2 + digit3;

// Output the result


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

return 0;
}

8. Enter the principal, time, and rate of interest. Calculate the simple interest.

#include <stdio.h>

int main()
{
float principal, time, rate, simpleInterest;

// Input the principal, time (in years), and rate (in percentage) from the user
printf("Enter the principal amount: ");
scanf("%f", &principal);

printf("Enter the time (in years): ");


scanf("%f", &time);
printf("Enter the rate of interest (in percentage): ");
scanf("%f", &rate);

// Calculate the simple interest using the formula: SI = (P * T * R) / 100


simpleInterest = (principal * time * rate) / 100;

// Output the simple interest


printf("The simple interest is: %.2f\n", simpleInterest);

return 0;
}

9. Convert any input temperature in Fahrenheit to Celsius.

#include <stdio.h>

int main()
{
float fahrenheit, celsius;

// Input temperature in Fahrenheit from the user


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

// Convert Fahrenheit to Celsius using the formula: C = (F - 32) * 5/9


celsius = (fahrenheit - 32) * 5 / 9;

// Output the result


printf("The equivalent temperature in Celsius is: %.2f°C\n", celsius);

return 0;
}

10. Find the percentage of marks obtained by a student by entering the marks secured by the student
in 5 subjects. Consider the total mark in each subject is 100.

#include <stdio.h>

int main()
{
float subject1, subject2, subject3, subject4, subject5, totalMarks, percentage;

// Input marks for 5 subjects from the user


printf("Enter the marks obtained in subject 1: ");
scanf("%f", &subject1);

printf("Enter the marks obtained in subject 2: ");


scanf("%f", &subject2);
printf("Enter the marks obtained in subject 3: ");
scanf("%f", &subject3);

printf("Enter the marks obtained in subject 4: ");


scanf("%f", &subject4);

printf("Enter the marks obtained in subject 5: ");


scanf("%f", &subject5);

// Calculate the total marks obtained


totalMarks = subject1 + subject2 + subject3 + subject4 + subject5;

// Calculate the percentage


percentage = (totalMarks / 500) * 100;

// Output the percentage


printf("The percentage of marks obtained is: %.2f%%\n", percentage);

return 0;
}

11. Calculate the gross salary of an employee by entering the basic salary. DA is 42%, HRA is 30%
of the basic salary and a fixed other allowance (OA) of Rs. 2000. (Gross salary=Basic+
DA+HRA+OA)

#include <stdio.h>

int main()
{
float basic_salary, DA, HRA, OA = 2000, gross_salary;

// Input basic salary


printf("Enter the basic salary: ");
scanf("%f", &basic_salary);

// Calculate DA, HRA, and Gross Salary


DA = 0.42 * basic_salary;
HRA = 0.30 * basic_salary;
gross_salary = basic_salary + DA + HRA + OA;

// Output the gross salary


printf("Gross Salary = %.2f\n", gross_salary);

return 0;
}
12. Convert an input total number of days into corresponding number of years, months and
remaining days. Consider 1 month=30 days.
(Example- Input days: 450 days; Output: 1 year, 2 months, 25 days)

#include <stdio.h>

int main()
{
int total_days, years, months, days;

// Input total number of days


printf("Enter total number of days: ");
scanf("%d", &total_days);

// Calculate years, months, and remaining days


years = total_days / 365; // 1 year = 365 days
months = (total_days % 365) / 30; // 1 month = 30 days
days = (total_days % 365) % 30; // Remaining days

// Output the result


printf("%d years, %d months, %d days\n", years, months, days);

return 0;
}

13. Input some quantity weight in grams and calculate the corresponding weight in Kilograms and
remaining grams (Example- Input weight: 1500 grams; Output: 1 KG 500 grams)

#include <stdio.h>

int main()
{
int total_grams, kilograms, grams;

// Input weight in grams


printf("Enter the weight in grams: ");
scanf("%d", &total_grams);

// Calculate kilograms and remaining grams


kilograms = total_grams / 1000; // 1 kilogram = 1000 grams
grams = total_grams % 1000; // Remaining grams

// Output the result


printf("%d KG %d grams\n", kilograms, grams);

return 0;
}
14. Input the time in seconds and calculate the corresponding hours, minutes, and remaining
seconds. (Exa: Input time in seconds: 3665; Output: 1 Hour 1 minute 5 seconds).

#include <stdio.h>

int main()
{
int total_seconds, hours, minutes, seconds;

// Input time in seconds


printf("Enter time in seconds: ");
scanf("%d", &total_seconds);

// Calculate hours, minutes, and remaining seconds


hours = total_seconds / 3600; // 1 hour = 3600 seconds
minutes = (total_seconds % 3600) / 60; // 1 minute = 60 seconds
seconds = total_seconds % 60; // Remaining seconds

// Output the result


printf("%d Hour(s) %d minute(s) %d second(s)\n", hours, minutes, seconds);

return 0;
}

15. Calculate the distance between two points (x1, y1) and (x2, y2) for any entered values of x1,y1
and x2, y2.

#include <stdio.h>
#include <math.h>

int main()
{
float x1, y1, x2, y2, distance;

// Input the coordinates of the first point (x1, y1)


printf("Enter the coordinates of the first point (x1, y1): ");
scanf("%f %f", &x1, &y1);

// Input the coordinates of the second point (x2, y2)


printf("Enter the coordinates of the second point (x2, y2): ");
scanf("%f %f", &x2, &y2);

// Calculate the distance using the distance formula


distance = sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1));

// Output the result


printf("The distance between the points (%.2f, %.2f) and (%.2f, %.2f) is %.2f\n", x1, y1, x2, y2,
distance);
return 0;
}

16. Calculate area of a triangle by Herron’s method for any entered values of a, b and c.

#include <stdio.h>
#include <math.h>

int main()
{
float a, b, c, s, area;

// Input the lengths of the sides of the triangle


printf("Enter the lengths of the sides of the triangle (a, b, c): ");
scanf("%f %f %f", &a, &b, &c);

// Calculate the semi-perimeter (s)


s = (a + b + c) / 2;

// Calculate the area using Heron's formula


area = sqrt(s * (s - a) * (s - b) * (s - c));

// Output the area


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

return 0;
}

17. Print the size of various basic data types (char, int, float, double) in C.

#include <stdio.h>

int main()
{
// Print the size of various data types
printf("Size of char: %lu byte(s)\n", sizeof(char));
printf("Size of int: %lu byte(s)\n", sizeof(int));
printf("Size of float: %lu byte(s)\n", sizeof(float));
printf("Size of double: %lu byte(s)\n", sizeof(double));

return 0;
}
18. Calculate the total bill to be paid by a customer entering the price of 3 products and their
quantities. Include a 10% tax on total bill amount for calculation of the amount to be paid by the
customer.

#include <stdio.h>

int main()
{
float price1, price2, price3;
int qty1, qty2, qty3;
float total, tax, final_amount;

// Input price and quantity for product 1


printf("Enter the price and quantity of product 1: ");
scanf("%f %d", &price1, &qty1);

// Input price and quantity for product 2


printf("Enter the price and quantity of product 2: ");
scanf("%f %d", &price2, &qty2);

// Input price and quantity for product 3


printf("Enter the price and quantity of product 3: ");
scanf("%f %d", &price3, &qty3);

// Calculate total price


total = (price1 * qty1) + (price2 * qty2) + (price3 * qty3);

// Calculate 10% tax on the total bill


tax = total * 0.10;

// Calculate the final amount to be paid (total + tax)


final_amount = total + tax;

// Output the total bill and final amount


printf("Total bill (before tax): %.2f\n", total);
printf("Tax (10%%): %.2f\n", tax);
printf("Final amount to be paid: %.2f\n", final_amount);

return 0;
}
19. Find the smallest number among three entered numbers using conditional operator.

#include <stdio.h>

int main()
{
int num1, num2, num3, smallest;

// Input three numbers


printf("Enter three numbers: ");
scanf("%d %d %d", &num1, &num2, &num3);

// Using conditional operator to find the smallest number


smallest = (num1 < num2) ? (num1 < num3 ? num1 : num3) : (num2 < num3 ? num2 : num3);

// Output the smallest number


printf("The smallest number is: %d\n", smallest);

return 0;
}

20. Enter the total price of some food order by a customer in a restaurant. The restaurant charges a
12% GST on total amount. If the total amount exceeds RS 1000, the restaurant offers a 5%
discount. Otherwise no discount is provided. Use conditional operator for discount calculation.
Print the final amount payable by the customer.
#include <stdio.h>
int main()
{
float total_price, gst, discount, final_amount;

// Input the total price of the food order


printf("Enter the total price of the food order: ");
scanf("%f", &total_price);

// Calculate 12% GST on the total price


gst = total_price * 0.12;

// Calculate discount using conditional operator


discount = (total_price > 1000) ? (total_price * 0.05) : 0;

// Calculate the final amount to be paid (total price + GST - discount)


final_amount = total_price + gst - discount;

// Output the final amount to be paid


printf("Final amount payable by the customer: %.2f\n", final_amount);

return 0;
}
Module-2
1. Find greatest among two entered numbers.

#include <stdio.h>

int main()
{
int num1, num2;

// Input two numbers


printf("Enter two numbers: ");
scanf("%d %d", &num1, &num2);

// Conditional statement to find the greatest number


if (num1 > num2)
{
printf("The greatest number is: %d\n", num1);
} else
{
printf("The greatest number is: %d\n", num2);
}

return 0;
}

2. Check whether an entered number is even or odd.

#include <stdio.h>
int main()
{
int num;

// Input a number
printf("Enter a number: ");
scanf("%d", &num);

// Conditional statement to check if the number is even or odd


if (num % 2 == 0)
{
printf("The number %d is even.\n", num);
} else
{
printf("The number %d is odd.\n", num);
}
return 0;
}
3. Check whether an entered character is a vowel or not.

#include <stdio.h>

int main()
{
char ch;

// Input a character
printf("Enter a character: ");
scanf("%c", &ch);

// Conditional statement to check if the character is a vowel


if (ch == 'a' || ch == 'A' || ch == 'e' || ch == 'E' || ch == 'i' || ch == 'I' || ch == 'o' || ch == 'O' || ch == 'u' ||
ch == 'U')
{
printf("The character %c is a vowel.\n", ch);
} else {
printf("The character %c is not a vowel.\n", ch);
}

return 0;
}

4. Check whether an entered number is divisible by 3 and 5.

#include <stdio.h>

int main()
{
int num;

// Input a number
printf("Enter a number: ");
scanf("%d", &num);

// Conditional statement to check divisibility by 3 and 5


if (num % 3 == 0 && num % 5 == 0) {
printf("The number %d is divisible by both 3 and 5.\n", num);
} else {
printf("The number %d is not divisible by both 3 and 5.\n", num);
}

return 0;
}
5. Check whether an entered year is a leap year or not.

#include <stdio.h>

int main()
{
int year;

// Input a year
printf("Enter a year: ");
scanf("%d", &year);

// Conditional statement to check leap year


if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
{
printf("The year %d is a leap year.\n", year);
}
else
{
printf("The year %d is not a leap year.\n", year);
}

return 0;
}

6. Check whether any entered number is positive, negative or zero using nested if-else statement

#include <stdio.h>

int main()
{
int num;

// Input a number
printf("Enter a number: ");
scanf("%d", &num);

// Nested if-else statement to check if the number is positive, negative, or zero


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");
}
}

return 0;
}

7. Check whether two entered numbers are equal or the first number is greater than the second
number or the second number is greater than the first number using nested if-else statement.

#include <stdio.h>

int main()
{
int num1, num2;

// Input two numbers


printf("Enter the first number: ");
scanf("%d", &num1);
printf("Enter the second number: ");
scanf("%d", &num2);

// Using nested if-else statements to compare the numbers


if (num1 == num2) {
printf("Both numbers are equal.\n");
} else {
if (num1 > num2) {
printf("The first number is greater than the second number.\n");
} else {
printf("The second number is greater than the first number.\n");
}
}

return 0;
}

8. Find greatest among 3 entered numbers using nested if-else statement.

#include <stdio.h>

int main()
{
int num1, num2, num3;

// Input three numbers


printf("Enter the first number: ");
scanf("%d", &num1);
printf("Enter the second number: ");
scanf("%d", &num2);
printf("Enter the third number: ");
scanf("%d", &num3);

// Using nested if-else statements to find the greatest number


if (num1 >= num2) {
if (num1 >= num3) {
printf("The greatest number is: %d\n", num1);
} else {
printf("The greatest number is: %d\n", num3);
}
} else {
if (num2 >= num3) {
printf("The greatest number is: %d\n", num2);
} else {
printf("The greatest number is: %d\n", num3);
}
}

return 0;
}

9. Find smallest among three entered numbers using else if ladder. Use logical AND operator to
combine multiple conditions.

#include <stdio.h>

int main()
{
int num1, num2, num3;

// Input three numbers


printf("Enter the first number: ");
scanf("%d", &num1);
printf("Enter the second number: ");
scanf("%d", &num2);
printf("Enter the third number: ");
scanf("%d", &num3);

// Using else if ladder with logical AND to find the smallest number
if (num1 <= num2 && num1 <= num3) {
printf("The smallest number is: %d\n", num1);
} else if (num2 <= num1 && num2 <= num3) {
printf("The smallest number is: %d\n", num2);
} else {
printf("The smallest number is: %d\n", num3);
}

return 0;
}

10. Enter mark obtained by a student in a subject and print the respective grade using else-if ladder
statement. Consider the following grading system:
Score out of 100 Marks Grade
90 & above up to 100 O
80 & above but less than 90 E 70 & above but less than 80 A
60 & above but less than 70 B
50 & above but less than 60 C
40 & above but less than 50 D
Less than 40 or Absent in Exam U

#include <stdio.h>

int main()
{
int marks;

// Input the marks obtained by the student


printf("Enter the marks obtained (out of 100): ");
scanf("%d", &marks);

// Using else-if ladder to assign grades based on the marks


if (marks >= 90 && marks <= 100) {
printf("Grade: O (Outstanding)\n");
} else if (marks >= 80 && marks < 90) {
printf("Grade: E (Excellent)\n");
} else if (marks >= 70 && marks < 80) {
printf("Grade: A (Very Good)\n");
} else if (marks >= 60 && marks < 70) {
printf("Grade: B (Good)\n");
} else if (marks >= 50 && marks < 60) {
printf("Grade: C (Average)\n");
} else if (marks >= 40 && marks < 50) {
printf("Grade: D (Pass)\n");
} else if (marks < 40 || marks == -1) {
printf("Grade: U (Unsatisfactory or Absent)\n");
} else {
printf("Invalid marks entered!\n");
}

return 0;
}
11. Enter the cost price and selling price of a product and check profit or loss. Also, print the
percentage of loss or profit for the product.

#include <stdio.h>

int main()
{
float costPrice, sellingPrice, profit, loss, percentage;

// Input cost price and selling price


printf("Enter the cost price of the product: ");
scanf("%f", &costPrice);
printf("Enter the selling price of the product: ");
scanf("%f", &sellingPrice);

// Check if there is profit or loss


if (sellingPrice > costPrice) {
// Profit case
profit = sellingPrice - costPrice;
percentage = (profit / costPrice) * 100;
printf("You made a profit of %.2f\n", profit);
printf("Profit percentage: %.2f%%\n", percentage);
} else if (sellingPrice < costPrice) {
// Loss case
loss = costPrice - sellingPrice;
percentage = (loss / costPrice) * 100;
printf("You incurred a loss of %.2f\n", loss);
printf("Loss percentage: %.2f%%\n", percentage);
} else {
// No profit, no loss
printf("There is no profit or loss. The cost price and selling price are the same.\n");
}

return 0;
}

12. Compute the real roots of a quadratic equation ax2 + bx + c=0 (given a, b and c) for the following
conditions:

i) No solution, if both a and b are zero.


ii) Only one root if determinant (b2 - 4ac)=0
iii) No real roots if b2 - 4ac < 0
iv) Otherwise there are 2 real roots

#include <stdio.h>
#include <math.h> // for sqrt() function

int main()
{
float a, b, c, discriminant, root1, root2;

// Input values for a, b, and c


printf("Enter the coefficient a: ");
scanf("%f", &a);
printf("Enter the coefficient b: ");
scanf("%f", &b);
printf("Enter the constant c: ");
scanf("%f", &c);

// Case i: No solution if both a and b are zero


if (a == 0 && b == 0) {
printf("No solution, both a and b are zero.\n");
}
// Case ii: Only one root if discriminant is zero
else if (a == 0 && b != 0) {
// Linear equation, only one root
root1 = -c / b;
printf("This is a linear equation. The root is: %.2f\n", root1);
}
else {
// Calculate the discriminant (b^2 - 4ac)
discriminant = (b * b) - (4 * a * c);

// Case iii: No real roots if discriminant < 0


if (discriminant < 0) {
printf("No real roots, discriminant is negative.\n");
}
// Case ii: Only one root if discriminant == 0
else if (discriminant == 0) {
root1 = -b / (2 * a);
printf("There is only one real root: %.2f\n", root1);
}
// Case iv: Two real roots if discriminant > 0
else {
root1 = (-b + sqrt(discriminant)) / (2 * a);
root2 = (-b - sqrt(discriminant)) / (2 * a);
printf("There are two real roots: %.2f and %.2f\n", root1, root2);
}
}

return 0;
}
13. Calculate the income tax payable by a person by entering the total taxable income as per the
below slabs:

Taxable Income range (in RS) Tax%


0 – 3,00000 0
3,00001 – 7,00000 5
7,00001- 10,00000 10
10,00001- 12,00000 15
12,00001 – 15,00000 20
Greater than 15,00000 30

#include <stdio.h>

int main()
{
float income, tax = 0;

// Input the total taxable income


printf("Enter your total taxable income (in Rs): ");
scanf("%f", &income);

// Calculate the income tax based on the income slab


if (income <= 300000) {
tax = 0; // No tax for income up to Rs. 3,00,000
}
else if (income <= 700000) {
tax = (income - 300000) * 0.05; // 5% tax for income between Rs. 3,00,001 to Rs. 7,00,000
}
else if (income <= 1000000) {
tax = (700000 - 300000) * 0.05 + (income - 700000) * 0.10; // 10% for income between Rs.
7,00,001 to Rs. 10,00,000
}
else if (income <= 1200000) {
tax = (700000 - 300000) * 0.05 + (1000000 - 700000) * 0.10 + (income - 1000000) * 0.15; //
15% for income between Rs. 10,00,001 to Rs. 12,00,000
}
else if (income <= 1500000) {
tax = (700000 - 300000) * 0.05 + (1000000 - 700000) * 0.10 + (1200000 - 1000000) * 0.15 +
(income - 1200000) * 0.20; // 20% for income between Rs. 12,00,001 to Rs. 15,00,000
}
else {
tax = (700000 - 300000) * 0.05 + (1000000 - 700000) * 0.10 + (1200000 - 1000000) * 0.15 +
(1500000 - 1200000) * 0.20 + (income - 1500000) * 0.30; // 30% for income above Rs. 15,00,000
}

// Output the total tax


printf("The total income tax payable is: Rs. %.2f\n", tax);

return 0;
}

14. Calculate the age of a person, given date of birth (DOB) and current date (CD). A date is
represented as three integers (say dd, mm, and yyyy). The result to be printed as YY years, MM
months, DD days. Consider a month consist of 30 days.

Example: Input DOB: 29/10/1980, CD: 27/8/2011


Output: Age: 30 years 9 Months and 28 days

#include <stdio.h>

int main()
{
int dob_day, dob_month, dob_year;
int cd_day, cd_month, cd_year;
int years, months, days;

// Input Date of Birth (DOB)


printf("Enter your Date of Birth (dd mm yyyy): ");
scanf("%d %d %d", &dob_day, &dob_month, &dob_year);

// Input Current Date (CD)


printf("Enter the Current Date (dd mm yyyy): ");
scanf("%d %d %d", &cd_day, &cd_month, &cd_year);

// Calculate days
if (cd_day < dob_day) {
cd_day += 30; // Borrow 30 days from the previous month
cd_month--; // Decrease the current month by 1
}
days = cd_day - dob_day;

// Calculate months
if (cd_month < dob_month) {
cd_month += 12; // Borrow 12 months from the previous year
cd_year--; // Decrease the current year by 1
}
months = cd_month - dob_month;

// Calculate years
years = cd_year - dob_year;

// Output the result


printf("Age: %d years, %d months, and %d days\n", years, months, days);

return 0;
}
15. Enter the total purchase amount for a customer and calculate the amount payable by the
customer after discount, if a shopping mall announced the following discounts on total purchase
amount:

Purchase amount (RS) Discount


< 1000 -
1000-3000 5%
3001-6000 7%
6001-10000 10%
Above 10000 Rs 2000

#include <stdio.h>

int main()
{
float purchaseAmount, discount = 0, amountPayable;

// Input the total purchase amount


printf("Enter the total purchase amount (in Rs): ");
scanf("%f", &purchaseAmount);

// Calculate the discount based on the purchase amount


if (purchaseAmount >= 1000 && purchaseAmount <= 3000) {
discount = purchaseAmount * 0.05; // 5% discount for amount between 1000 and 3000
}
else if (purchaseAmount >= 3001 && purchaseAmount <= 6000) {
discount = purchaseAmount * 0.07; // 7% discount for amount between 3001 and 6000
}
else if (purchaseAmount >= 6001 && purchaseAmount <= 10000) {
discount = purchaseAmount * 0.10; // 10% discount for amount between 6001 and 10000
}
else if (purchaseAmount > 10000) {
discount = 2000; // Flat Rs. 2000 discount for amount above 10000
}

// Calculate the final amount payable after discount


amountPayable = purchaseAmount - discount;

// Output the discount and the amount payable


printf("Discount: Rs. %.2f\n", discount);
printf("Amount payable: Rs. %.2f\n", amountPayable);

return 0;
}
16. Enter the previous month and current month meter reading. Calculate the electric bill amount
for any customer as per the following rules:

First 100 units : Rs 3.20 per unit.


Next 200 units : Rs 5.40 per unit.
Remaining units : Rs 6 per unit

#include <stdio.h>

int main()
{
int prevReading, currReading, unitsConsumed;
float billAmount = 0;

// Input previous and current month meter readings


printf("Enter the previous month's meter reading: ");
scanf("%d", &prevReading);
printf("Enter the current month's meter reading: ");
scanf("%d", &currReading);

// Calculate units consumed


unitsConsumed = currReading - prevReading;

// Check if units consumed is valid


if (unitsConsumed < 0) {
printf("Invalid meter readings. Current reading cannot be less than the previous reading.\n");
return 1;
}

// Calculate the bill amount based on the number of units consumed


if (unitsConsumed <= 100) {
billAmount = unitsConsumed * 3.20; // First 100 units at Rs 3.20 per unit
}
else if (unitsConsumed <= 300) {
billAmount = 100 * 3.20 + (unitsConsumed - 100) * 5.40; // Next 200 units at Rs 5.40 per unit
}
else {
billAmount = 100 * 3.20 + 200 * 5.40 + (unitsConsumed - 300) * 6.00; // Remaining units at Rs
6 per unit
}

// Output the total bill amount


printf("Total units consumed: %d\n", unitsConsumed);
printf("Electric bill amount: Rs %.2f\n", billAmount);

return 0;
}
17. Given a number between 1 to 7, print the day name (Monday- Sunday) using switch-case
statement.

#include <stdio.h>

int main()
{
int day;

// Input the number representing the day of the week


printf("Enter a number between 1 and 7: ");
scanf("%d", &day);

// Use switch-case to print the day name


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("Invalid input! Please enter a number between 1 and 7.\n");
break;
}

return 0;
}
18. Given a number 0-9, print the corresponding English word for the number using switch-case
statement.

#include <stdio.h>

int main()
{
int number;

// Input the number between 0 and 9


printf("Enter a number between 0 and 9: ");
scanf("%d", &number);

// Use switch-case to print the corresponding English word


switch(number) {
case 0:
printf("Zero\n");
break;
case 1:
printf("One\n");
break;
case 2:
printf("Two\n");
break;
case 3:
printf("Three\n");
break;
case 4:
printf("Four\n");
break;
case 5:
printf("Five\n");
break;
case 6:
printf("Six\n");
break;
case 7:
printf("Seven\n");
break;
case 8:
printf("Eight\n");
break;
case 9:
printf("Nine\n");
break;
default:
printf("Invalid input! Please enter a number between 0 and 9.\n");
break;
}
return 0;
}

19. Check whether an entered character is a vowel or not using switch-case statement.

#include <stdio.h>

int main()
{
char ch;

// Input a character
printf("Enter a character: ");
scanf("%c", &ch);

// Convert character to lowercase to handle both uppercase and lowercase vowels


ch = (ch >= 'A' && ch <= 'Z') ? ch + 32 : ch;

// Use switch-case to check if the character is a vowel


switch(ch) {
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
printf("The character '%c' is a vowel.\n", ch);
break;
default:
printf("The character '%c' is not a vowel.\n", ch);
break;
}

return 0;
}

20. Create a menu-driven program using switch–case statement that will ask the user to enter two
numbers and an operator (+, -, *, /). When the user enters a valid choice, the program prints the
result of the respective operation as per the entered choice, otherwise prints an error message.

#include <stdio.h>

int main() {
float num1, num2, result;
char operator;

// Display menu
printf("Enter two numbers and an operator (+, -, *, /):\n");
printf("Available operations:\n");
printf("1. Addition (+)\n");
printf("2. Subtraction (-)\n");
printf("3. Multiplication (*)\n");
printf("4. Division (/)\n");

// Input two numbers and an operator


printf("Enter the first number: ");
scanf("%f", &num1);
printf("Enter the second number: ");
scanf("%f", &num2);
printf("Enter the operator (+, -, *, /): ");
scanf(" %c", &operator); // Space before %c to consume any leftover newline character

// Perform the operation based on the entered operator


switch(operator) {
case '+':
result = num1 + num2;
printf("Result: %.2f\n", result);
break;
case '-':
result = num1 - num2;
printf("Result: %.2f\n", result);
break;
case '*':
result = num1 * num2;
printf("Result: %.2f\n", result);
break;
case '/':
if (num2 != 0) {
result = num1 / num2;
printf("Result: %.2f\n", result);
} else {
printf("Error! Division by zero is not allowed.\n");
}
break;
default:
printf("Invalid operator! Please enter one of +, -, *, or /.\n");
break;
}

return 0;
}
Loops:
21. Print first n natural numbers using while loop.

#include <stdio.h>

int main()
{
int n, i = 1;

// Input the value of n


printf("Enter a number n: ");
scanf("%d", &n);

// Print first n natural numbers using while loop


printf("The first %d natural numbers are:\n", n);
while (i <= n) {
printf("%d ", i);
i++;
}
printf("\n");

return 0;
}

22. Print the multiplication table of any entered number.

#include <stdio.h>

int main()
{
int num, i;

// Input the number for which the multiplication table is to be printed


printf("Enter a number: ");
scanf("%d", &num);

// Print the multiplication table of the entered number


printf("Multiplication table of %d is:\n", num);
for(i = 1; i <= 10; i++) {
printf("%d x %d = %d\n", num, i, num * i);
}

return 0;
}
23. Find sum of digits of an entered number using while loop.

#include <stdio.h>

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

// Input the number


printf("Enter a number: ");
scanf("%d", &num);

// Use a while loop to find the sum of digits


while (num != 0) {
digit = num % 10; // Extract the last digit
sum += digit; // Add the digit to the sum
num /= 10; // Remove the last digit
}

// Print the sum of the digits


printf("The sum of digits is: %d\n", sum);

return 0;
}

24. Find Fibonacci series up to an entered number using do-while loop.

Test data: Entered number=90


Output series: 0 1 1 2 3 5 8 13 21 34 55 89

#include <stdio.h>

int main()
{
int n, first = 0, second = 1, next;

// Input the number


printf("Enter a number: ");
scanf("%d", &n);

// Print the Fibonacci series up to the entered number


printf("Fibonacci series up to %d is:\n", n);

// Start with 0 and 1, and generate the next number in the series
printf("%d %d ", first, second);
next = first + second;

// Use a do-while loop to print the Fibonacci series


do {
printf("%d ", next);
first = second;
second = next;
next = first + second;
} while (next <= n);

printf("\n");

return 0;
}

25. Find the sum of all user entered numbers until the sum exceeds 100 using do while loop.

#include <stdio.h>

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

// Do-while loop to keep entering numbers until the sum exceeds 100
do {
printf("Enter a number: ");
scanf("%d", &num);

sum += num; // Add the entered number to the sum


printf("Current sum: %d\n", sum);
} while (sum <= 100); // Continue until the sum exceeds 100

printf("The final sum is: %d\n", sum);

return 0;
}

26. Find the factorial of any user entered number using for loop.

#include <stdio.h>

int main()
{
int num, i;
long long factorial = 1;

// Input the number


printf("Enter a number: ");
scanf("%d", &num);

// Check for negative numbers


if (num < 0) {
printf("Factorial is not defined for negative numbers.\n");
} else {
// Use a for loop to calculate the factorial
for(i = 1; i <= num; i++) {
factorial *= i; // Multiply the current factorial value by i
}

// Print the factorial


printf("Factorial of %d is: %lld\n", num, factorial);
}

return 0;
}

27. Find the GCD of two entered numbers.

#include <stdio.h>

int gcd(int a, int b)


{
// Euclidean algorithm to find GCD
while (b != 0) {
int temp = b;
b = a % b;
a = temp;
}
return a;
}

int main() {
int num1, num2;

// Input the two numbers


printf("Enter two numbers: ");
scanf("%d %d", &num1, &num2);

// Call the gcd function


printf("The GCD of %d and %d is: %d\n", num1, num2, gcd(num1, num2));

return 0;
}

28. Print all odd numbers in a given range.

#include <stdio.h>

int main()
{
int start, end;
// Input the range (start and end values)
printf("Enter the start of the range: ");
scanf("%d", &start);
printf("Enter the end of the range: ");
scanf("%d", &end);

// Print all odd numbers in the given range


printf("Odd numbers between %d and %d are:\n", start, end);
for(int i = start; i <= end; i++) {
if (i % 2 != 0) {
printf("%d ", i);
}
}
printf("\n");

return 0;
}

29. Find sum of all even numbers in a given range.

#include <stdio.h>

int main()
{
int start, end, sum = 0;

// Input the range (start and end values)


printf("Enter the start of the range: ");
scanf("%d", &start);
printf("Enter the end of the range: ");
scanf("%d", &end);

// Calculate the sum of all even numbers in the given range


for (int i = start; i <= end; i++) {
if (i % 2 == 0) { // Check if the number is even
sum += i; // Add the even number to the sum
}
}

// Print the sum of even numbers


printf("Sum of even numbers between %d and %d is: %d\n", start, end, sum);

return 0;
}
30. Check whether an entered number is a prime or composite number.

#include <stdio.h>

int main()
{
int num, i, isPrime = 1;

// Input the number from the user


printf("Enter a number: ");
scanf("%d", &num);

// Edge case: numbers less than 2 are not prime


if (num <= 1)
{
isPrime = 0;
}

// Check for divisibility from 2 to num/2


for (i = 2; i <= num / 2; i++)
{
if (num % i == 0)
{
isPrime = 0; // If divisible by any number, it's not prime
break; // Exit the loop early
}
}

// Output the result


if (isPrime == 1)
{
printf("%d is a prime number.\n", num);
}
else
{
printf("%d is a composite number.\n", num);
}

return 0;
}

31. Check whether an entered number is a palindrome number or not.

#include <stdio.h>

int main()
{
int num, originalNum, reversedNum = 0, remainder;
// Input the number
printf("Enter a number: ");
scanf("%d", &num);

// Store the original number to compare later


originalNum = num;

// Reverse the number


while (num != 0) {
remainder = num % 10; // Get the last digit
reversedNum = reversedNum * 10 + remainder; // Build the reversed number
num /= 10; // Remove the last digit from num
}

// Check if the number is a palindrome


if (originalNum == reversedNum) {
printf("%d is a palindrome number.\n", originalNum);
} else {
printf("%d is not a palindrome number.\n", originalNum);
}

return 0;
}

32. Check whether an entered number is an Armstrong number or not.

#include <stdio.h>
#include <math.h>

int main()
{
int num, originalNum, remainder, result = 0, n = 0;

// Input the number


printf("Enter a number: ");
scanf("%d", &num);

// Store the original number


originalNum = num;

// Find the number of digits in the original number


while (originalNum != 0) {
originalNum /= 10;
n++;
}

originalNum = num;

// Calculate the sum of each digit raised to the power of n


while (originalNum != 0) {
remainder = originalNum % 10;
result += pow(remainder, n);
originalNum /= 10;
}

// Check if the sum is equal to the original number


if (result == num) {
printf("%d is an Armstrong number.\n", num);
} else {
printf("%d is not an Armstrong number.\n", num);
}

return 0;
}

33. Print all natural numbers in descending order up to 1 from an entered number except the
numbers divisible by 7 (use continue statement)

#include <stdio.h>

int main()
{
int num;

// Input the number


printf("Enter a number: ");
scanf("%d", &num);

// Print natural numbers in descending order except those divisible by 7


printf("Natural numbers in descending order (except multiples of 7) are:\n");
for (int i = num; i >= 1; i--) {
if (i % 7 == 0) {
continue; // Skip the numbers divisible by 7
}
printf("%d ", i);
}

printf("\n");

return 0;
}
34. Display the binary equivalent of an entered decimal number.

#include <stdio.h>

int main()
{
int num;

// Input the decimal number


printf("Enter a decimal number: ");
scanf("%d", &num);

// Check if the number is zero


if (num == 0) {
printf("Binary equivalent: 0\n");
} else {
// Print binary equivalent of the decimal number
printf("Binary equivalent: ");
int binary[32]; // Array to store the binary number
int i = 0;

// Convert decimal to binary


while (num > 0) {
binary[i] = num % 2; // Store the remainder (either 0 or 1)
num = num / 2; // Divide number by 2
i++;
}

// Print binary digits in reverse order


for (int j = i - 1; j >= 0; j--) {
printf("%d", binary[j]);
}
printf("\n");
}

return 0;
}

35. Display the decimal equivalent of an entered binary number.

#include <stdio.h>
#include <math.h>

int main()
{
long long binary;
int decimal = 0, remainder, i = 0;

// Input the binary number


printf("Enter a binary number: ");
scanf("%lld", &binary);

// Convert binary to decimal


while (binary != 0) {
remainder = binary % 10; // Get the last digit (either 0 or 1)
decimal += remainder * pow(2, i); // Add the corresponding power of 2
binary /= 10; // Remove the last digit from binary number
i++; // Move to the next power of 2
}

// Print the decimal equivalent


printf("Decimal equivalent: %d\n", decimal);

return 0;
}
36. Print the following pattern:

12345
1234
123
12
1

#include <stdio.h>
int main()
{
int i, j;

// Outer loop for number of rows (5 rows)


for (i = 5; i >= 1; i--) {
// Inner loop for printing numbers in each row
for (j = 1; j <= i; j++) {
printf("%d ", j);
}
printf("\n"); // Move to the next line after each row
}

return 0;
}

37. Print the following pattern:

A
AB
ABC
ABC D
ABC DE
ABC DE F

#include <stdio.h>
int main()
{
int i, j;

// Outer loop for the number of rows (6 rows)


for (i = 1; i <= 6; i++) {
// Inner loop for printing characters in each row
for (j = 1; j <= i; j++) {
// Print the character corresponding to 'A' + (j - 1)
printf("%c ", 'A' + j - 1);
}
printf("\n"); // Move to the next line after each row
}
return 0;
}

38. Print the following pattern:

1
123
12345
1234567

#include <stdio.h>
int main()
{
int i, j;

// Outer loop for the number of rows (4 rows)


for (i = 1; i <= 4; i++) {
// Inner loop for printing numbers in each row
for (j = 1; j <= (2 * i - 1); j++) {
printf("%d ", j);
}
printf("\n"); // Move to the next line after each row
}

return 0;
}
39. Print the following pattern:

1
00
111
0000
11111

#include <stdio.h>
int main()
{
int i, j;

// Outer loop for number of rows (5 rows)


for (i = 1; i <= 5; i++) {
// Inner loop for printing numbers in each row
for (j = 1; j <= i; j++) {
// Print 1 if the row number is odd, otherwise print 0
if (i % 2 != 0) {
printf("1 ");
} else {
printf("0 ");
}
}
printf("\n"); // Move to the next line after each row
}

return 0;
}
40. Print the following pattern:

1
121
12321

#include <stdio.h>
int main()
{
int n = 3; // Number of rows (you can change this if you want more lines)

// Loop for each row


for (int i = 1; i <= n; i++)
{
// Print leading spaces for each row
for (int j = 1; j <= n - i; j++)
{
printf(" "); // Print space
}

// Print increasing numbers


for (int j = 1; j <= i; j++)
{
printf("%d ", j); // Print numbers in increasing order
}

// Print decreasing numbers


for (int j = i - 1; j >= 1; j--)
{
printf("%d ", j); // Print numbers in decreasing order
}

printf("\n"); // Move to the next line after each row


}

return 0;
}

41. Print the following pattern:

*****
***
*
42. Print all prime numbers in a given range.

#include <stdio.h>

int main()
{
int start, end;

// Input the range from the user


printf("Enter the start of the range: ");
scanf("%d", &start);
printf("Enter the end of the range: ");
scanf("%d", &end);

printf("Prime numbers between %d and %d are:\n", start, end);

// Loop through the range and check for prime numbers


for (int num = start; num <= end; num++)
{
int isPrime = 1; // Assume the number is prime

if (num <= 1)
{
continue; // Skip numbers less than or equal to 1
}

// Check if num is divisible by any number from 2 to num/2


for (int i = 2; i <= num/2; i++)
{
if (num % i == 0)
{
isPrime = 0; // num is not prime
break; // No need to check further
}
}

// If the number is prime, print it


if (isPrime)
{
printf("%d ", num);
}
}

printf("\n");
return 0;
}
43. Print all palindrome numbers in a given range.

#include <stdio.h>

int main()
{
int lower, upper, i, num, reversed, remainder;

// Input the range


printf("Enter the lower limit: ");
scanf("%d", &lower);
printf("Enter the upper limit: ");
scanf("%d", &upper);

printf("Palindrome numbers in the range %d to %d are:\n", lower, upper);

// Loop through the range


for (i = lower; i <= upper; i++) {
num = i;
reversed = 0;

// Reverse the number


while (num != 0) {
remainder = num % 10;
reversed = reversed * 10 + remainder;
num /= 10;
}

// Check if the number is a palindrome


if (i == reversed) {
printf("%d ", i);
}
}

printf("\n");

return 0;
}

44. Print all Armstrong numbers in a given range.

#include <stdio.h>
#include <math.h>

int main()
{
int lower, upper, i, num, remainder, digits, sum;

// Input the range


printf("Enter the lower limit: ");
scanf("%d", &lower);
printf("Enter the upper limit: ");
scanf("%d", &upper);

printf("Armstrong numbers in the range %d to %d are:\n", lower, upper);

// Loop through the range


for (i = lower; i <= upper; i++) {
num = i;
sum = 0;

// Find the number of digits in the number


digits = (int)log10(num) + 1;

// Calculate the sum of the power of digits


while (num != 0) {
remainder = num % 10;
sum += pow(remainder, digits);
num /= 10;
}

// Check if the number is an Armstrong number


if (i == sum) {
printf("%d ", i);
}
}

printf("\n");

return 0;
}

45. Print all numbers in a range that are divisible by 5 and 7

#include <stdio.h>

int main()
{
int lower, upper, i;

// Input the range


printf("Enter the lower limit: ");
scanf("%d", &lower);
printf("Enter the upper limit: ");
scanf("%d", &upper);

printf("Numbers divisible by 5 and 7 in the range %d to %d are:\n", lower, upper);


// Loop through the range
for (i = lower; i <= upper; i++) {
// Check if the number is divisible by both 5 and 7
if (i % 5 == 0 && i % 7 == 0) {
printf("%d ", i);
}
}

printf("\n");

return 0;
}

You might also like