C programming lab report, Summaries of C programming

Lab report c programming . Tribhuwan University

Typology: Summaries

2024/2025

Uploaded on 05/11/2026

sambhav-2
sambhav-2 🇳🇵

1 document

1 / 54

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
LAB 1:
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 <stdio.h>
int main() {
printf("My name is Dikshit Ghimire");
return 0;
}
Output:
2. WAP to print name, address and roll no. using escape sequences
Title: Program to print name, address, and roll number using escape sequences
Objective: To understand the use of escape sequences like \n and \t.
Theory: Escape sequences are used to format output.
\n → new line
\t → tab space
Code:
#include <stdio.h>
int main() {
printf("Name:\tDikshit Ghimire\n");
printf("Address:\tKathmandu\n");
printf("Roll No:\t12");
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36

Partial preview of the text

Download C programming lab report and more Summaries C programming in PDF only on Docsity!

LAB 1:

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:

  1. WAP to print name, address and roll no. using escape sequences Title:** Program to print name, address, and roll number using escape sequences Objective: To understand the use of escape sequences like \n and \t. Theory: Escape sequences are used to format output.  \n → new line  \t → tab space Code: #include int main() { printf("Name:\tDikshit Ghimire\n"); printf("Address:\tKathmandu\n"); printf("Roll No:\t12");

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:

  1. WAP to swap two variables (with and without third variable) Title:** Program to swap two numbers Objective: To exchange values of two variables using both — (1) a third temporary variable, and (2) without using a third variable. Theory:  With third variable: Use a temporary variable to hold one value while swapping.  Without third variable: Use arithmetic operations (addition/subtraction or XOR). Code: #include int main() {

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:

  1. WAP to Calculate Tax Based on Marital Status, Annual Income, and Gender Title:** Program to calculate annual income tax based on marital status, income range, and gender. Objective: To determine the total income tax of an individual considering their marital status, annual income, and gender, applying the relevant tax slabs and discounts. Theory: The tax rate depends on both marital status and income slab. If the person is female, an additional 10% discount is applied to the total tax amount.

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 -

    • 0.35; } else { printf("Invalid marital status entered.\n"); return 0; } if (strcmp(gender, "female") == 0) { discount = tax * 0.10; tax -= discount; } printf("\n---------------------------\n"); printf("Total Tax Payable: Rs. %.2f\n", tax); if (discount > 0) printf("Discount Applied (10%%): Rs. %.2f\n", discount); printf("---------------------------\n"); return 0; } Output:

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:

  1. WAP to Find Roots of a Quadratic Equation Title:** Program to find the roots of quadratic equation ax² + bx + c = 0. Objective: To calculate real or imaginary roots using the discriminant formula. Theory:  Discriminant D = b² - 4ac  If D > 0 → real and distinct roots  If D = 0 → real and equal roots  If D < 0 → imaginary roots Code: #include #include int main() {

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: