














































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Lab report c programming . Tribhuwan University
Typology: Summaries
1 / 54
This page cannot be seen from the preview
Don't miss anything!















































1. WAP to print your name on the screen Title: Program to print your name on the screen Objective: To display a simple text message using printf() function. Theory: The printf() function in C is used to print text or variables on the screen Code: #include int main() { printf("My name is Dikshit Ghimire"); return 0; } **Output:
return 0; } Output:
3. WAP to find sum, difference, product and division (quotient & remainder) Title : Arithmetic operations on two numbers Objective : To perform basic arithmetic operations between two numbers. Theory: Arithmetic operators +, -, *, /, and % are used for addition, subtraction, multiplication, division, and modulus respectively. Code: #include int main() { int a, b; printf("Enter two numbers: "); scanf("%d %d", &a, &b); printf("Sum = %d\n", a + b); printf("Difference = %d\n", a - b); printf("Product = %d\n", a * b); printf("Quotient = %d\n", a / b); printf("Remainder = %d\n", a % b); return 0; } Output:
5. WAP to calculate area and circumference of a circle Title: Program to find area and circumference of a circle Objective: To calculate area and circumference using radius. Theory: Area = π × r × r Circumference = 2 × π × r Code: #include #define PI 3. int main() { float r, area, circ; printf("Enter radius of circle: "); scanf("%f", &r); area = PI * r * r; circ = 2 * PI * r; printf("Area = %.2f\n", area); printf("Circumference = %.2f", circ); return 0; } Output:
float p, t, r, si; printf("Enter Principal, Time and Rate: "); scanf("%f %f %f", &p, &t, &r); si = (p * t * r) / 100; printf("Simple Interest = %.2f", si); return 0; } Output:
9. WAP to demonstrate use of constant Title: Program to demonstrate constant usage Objective: To understand how to declare and use constants in C. Theory: The const keyword defines values that cannot be changed during program execution. Code: #include int main() { const float PI = 3.1416; float r = 5, area; area = PI * r * r; printf("Area of circle with radius %.2f = %.2f", r, area); return 0; } Output:
time(&now); days = difftime(now, birth_time) / (60 * 60 * 24); printf("---------------------------\n"); printf("You have lived approximately %.0f days.\n", days); printf("---------------------------\n"); return 0; } **Output:
printf("\033[0;37;40m"); int a, b, temp; printf("Enter two numbers: "); scanf("%d %d", &a, &b); printf("---------------------------\n"); printf("Before swapping: a = %d, b = %d\n", a, b); // Swapping using third variable temp = a; a = b; b = temp; printf("After swapping (using third variable): a = %d, b = %d\n", a, b); // Swapping back without third variable a = a + b; b = a - b; a = a - b; printf("After swapping (without third variable): a = %d, b = %d\n", a, b); printf("---------------------------\n"); return 0; } Output:
scanf("%f %f %f", &p, &r, &t); amount = p * pow((1 + r / 100), t); ci = amount - p; printf("---------------------------\n"); printf("Compound Interest = %.2f\n", ci); printf("Total Amount = %.2f\n", amount); printf("---------------------------\n"); return 0; } **Output:
Tax Rules: Married: Income Range Tax ≤ 450,000 1% of income 450,001 – 550,000 1% of first 450,000 + 10% of next 100, 550,001 – 750,000 1% of first 450,000 + 10% of next 100,000 + 20% of next 200, 750,001 – 1,300,000 previous + 30% of next 550, > 1,300,000 previous + 35% of remaining amount Un-married: Income Range Tax ≤ 400,000 1% of income 400,001 – 500,000 1% of first 400,000 + 10% of next 100, 500,001 – 750,000 previous + 20% of next 250, 750,001 – 1,300,000 previous + 30% of next 550, > 1,300,000 previous + 35% of remaining amount Female Discount: 10% off total tax. Code: #include #include int main() { printf("\033[0;37;40m"); // white text on black background char maritalStatus[10], gender[10]; float income, tax = 0, discount = 0; printf("Enter marital status (married/unmarried): ");
else tax = 400000 * 0.01 + 100000 * 0.10 + 250000 * 0.20 + 550000 * 0.30 + (income -
5) WAP to Test a Number for Multiples and Divisibility Title: Program to test whether a number is multiple of 5 and divisible by 7 but not by 11. Objective: To check divisibility conditions using logical operators in C. Theory: A number is: Multiple of 5 if divisible by 5. Divisible by 7 but not 11 if num % 7 == 0 && num % 11 != 0. Code: #include int main() { printf("\033[0;37;40m"); int num; printf("Enter a number: "); scanf("%d", &num); if (num % 5 == 0) printf("The number is a multiple of 5.\n"); else printf("The number is not a multiple of 5.\n");
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) printf("%d is a Leap Year.\n", year); else printf("%d is not a Leap Year.\n", year); return 0; } **Output:
printf("\033[0;37;40m"); float a, b, c, d, root1, root2, realPart, imagPart; printf("Enter coefficients a, b, c: "); scanf("%f %f %f", &a, &b, &c); d = bb - 4ac; if (d > 0) { root1 = (-b + sqrt(d)) / (2a); root2 = (-b - sqrt(d)) / (2a); printf("Roots are real and different.\nRoot1 = %.2f, Root2 = %.2f\n", root1, root2); } else if (d == 0) { root1 = root2 = -b / (2a); printf("Roots are real and equal.\nRoot1 = Root2 = %.2f\n", root1); } else { realPart = -b / (2a); imagPart = sqrt(-d) / (2a); printf("Roots are imaginary.\nRoot1 = %.2f + %.2fi\nRoot2 = %.2f - %.2fi\n", realPart, imagPart, realPart, imagPart); } return 0; } Output: