CP Lab Assignments Upto Module-2
CP Lab Assignments Upto Module-2
CP Lab Assignments Upto Module-2
#include <stdio.h>
#define PI 3.14159
int main()
{
float radius, 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;
return 0;
}
3. Find the average of 3 entered numbers.
#include <stdio.h>
int main()
{
float num1, num2, num3, 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;
return 0;
}
5. Swap the values of two variables using a third variable.
#include <stdio.h>
int main()
{
int a, b, temp;
return 0;
}
#include <stdio.h>
int main()
{
int 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;
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);
return 0;
}
#include <stdio.h>
int main()
{
float fahrenheit, 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;
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;
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;
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;
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;
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;
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;
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;
return 0;
}
19. Find the smallest number among three entered numbers using conditional operator.
#include <stdio.h>
int main()
{
int num1, num2, num3, 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;
return 0;
}
Module-2
1. Find greatest among two entered numbers.
#include <stdio.h>
int main()
{
int num1, num2;
return 0;
}
#include <stdio.h>
int main()
{
int num;
// Input a number
printf("Enter a number: ");
scanf("%d", &num);
#include <stdio.h>
int main()
{
char ch;
// Input a character
printf("Enter a character: ");
scanf("%c", &ch);
return 0;
}
#include <stdio.h>
int main()
{
int num;
// Input a number
printf("Enter a number: ");
scanf("%d", &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);
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);
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;
return 0;
}
#include <stdio.h>
int main()
{
int num1, num2, 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;
// 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;
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;
return 0;
}
12. Compute the real roots of a quadratic equation ax2 + bx + c=0 (given a, b and c) for the following
conditions:
#include <stdio.h>
#include <math.h> // for sqrt() function
int main()
{
float a, b, c, discriminant, root1, root2;
return 0;
}
13. Calculate the income tax payable by a person by entering the total taxable income as per the
below slabs:
#include <stdio.h>
int main()
{
float income, tax = 0;
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.
#include <stdio.h>
int main()
{
int dob_day, dob_month, dob_year;
int cd_day, cd_month, cd_year;
int years, months, days;
// 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;
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:
#include <stdio.h>
int main()
{
float purchaseAmount, discount = 0, 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:
#include <stdio.h>
int main()
{
int prevReading, currReading, unitsConsumed;
float billAmount = 0;
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;
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;
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);
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");
return 0;
}
Loops:
21. Print first n natural numbers using while loop.
#include <stdio.h>
int main()
{
int n, i = 1;
return 0;
}
#include <stdio.h>
int main()
{
int 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;
return 0;
}
#include <stdio.h>
int main()
{
int n, first = 0, second = 1, next;
// Start with 0 and 1, and generate the next number in the series
printf("%d %d ", first, second);
next = first + second;
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);
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;
return 0;
}
#include <stdio.h>
int main() {
int num1, num2;
return 0;
}
#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);
return 0;
}
#include <stdio.h>
int main()
{
int start, end, sum = 0;
return 0;
}
30. Check whether an entered number is a prime or composite number.
#include <stdio.h>
int main()
{
int num, i, isPrime = 1;
return 0;
}
#include <stdio.h>
int main()
{
int num, originalNum, reversedNum = 0, remainder;
// Input the number
printf("Enter a number: ");
scanf("%d", &num);
return 0;
}
#include <stdio.h>
#include <math.h>
int main()
{
int num, originalNum, remainder, result = 0, n = 0;
originalNum = 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;
printf("\n");
return 0;
}
34. Display the binary equivalent of an entered decimal number.
#include <stdio.h>
int main()
{
int num;
return 0;
}
#include <stdio.h>
#include <math.h>
int main()
{
long long binary;
int decimal = 0, remainder, i = 0;
return 0;
}
36. Print the following pattern:
12345
1234
123
12
1
#include <stdio.h>
int main()
{
int i, j;
return 0;
}
A
AB
ABC
ABC D
ABC DE
ABC DE F
#include <stdio.h>
int main()
{
int i, j;
1
123
12345
1234567
#include <stdio.h>
int main()
{
int i, j;
return 0;
}
39. Print the following pattern:
1
00
111
0000
11111
#include <stdio.h>
int main()
{
int i, j;
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)
return 0;
}
*****
***
*
42. Print all prime numbers in a given range.
#include <stdio.h>
int main()
{
int start, end;
if (num <= 1)
{
continue; // Skip numbers less than or equal to 1
}
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;
printf("\n");
return 0;
}
#include <stdio.h>
#include <math.h>
int main()
{
int lower, upper, i, num, remainder, digits, sum;
printf("\n");
return 0;
}
#include <stdio.h>
int main()
{
int lower, upper, i;
printf("\n");
return 0;
}