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

C Programming Assignment

The document contains C programming lab assignments covering various topics from basic I/O to arrays and functions. Each day includes multiple programming tasks with sample code to calculate areas, handle conditional statements, perform loops, and manipulate arrays. The assignments are structured to enhance understanding of fundamental programming concepts and problem-solving skills.

Uploaded by

nitonor476
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)
21 views13 pages

C Programming Assignment

The document contains C programming lab assignments covering various topics from basic I/O to arrays and functions. Each day includes multiple programming tasks with sample code to calculate areas, handle conditional statements, perform loops, and manipulate arrays. The assignments are structured to enhance understanding of fundamental programming concepts and problem-solving skills.

Uploaded by

nitonor476
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

C Programming Lab Assignments

PPS (Principles of Programming and Software)


Solutions with Questions — Lab Day 1 to Day 7

DAY 1 — Basic I/O, Expressions & Menu-Driven Programs

Q1. Write a program in C to calculate area and perimeter of a circle. Radius is user input.
#include <stdio.h>
#define PI 3.14159

int main() {
float radius, area, perimeter;
printf("Enter radius: ");
scanf("%f", &radius);
area = PI * radius * radius;
perimeter = 2 * PI * radius;
printf("Area = %.2f\n", area);
printf("Perimeter = %.2f\n", perimeter);
return 0;
}

Q2. Calculate perimeter of a triangle. 3 sides are given as input.


#include <stdio.h>

int main() {
float a, b, c, perimeter;
printf("Enter three sides: ");
scanf("%f %f %f", &a, &b, &c);
perimeter = a + b + c;
printf("Perimeter = %.2f\n", perimeter);
return 0;
}

Q3(A). Make the arithmetic operations (i) Add (ii) Subtract (iii) Divide (iv) Multiply on user input
numbers.
#include <stdio.h>

int main() {
float a, b;
printf("Enter two numbers: ");
scanf("%f %f", &a, &b);
printf("Add = %.2f\n", a + b);
printf("Subtract = %.2f\n", a - b);
printf("Divide = %.2f\n", a / b);
printf("Multiply = %.2f\n", a * b);
return 0;
}

Q3(B). Update the above program as a menu-driven program.


#include <stdio.h>

int main() {
float a, b;
int choice;
printf("Enter two numbers: ");
scanf("%f %f", &a, &b);
printf("1. Add 2. Subtract 3. Divide 4. Multiply\n");
printf("Enter choice: ");
scanf("%d", &choice);
switch (choice) {
case 1: printf("Result = %.2f\n", a + b); break;
case 2: printf("Result = %.2f\n", a - b); break;
case 3:
if (b != 0) printf("Result = %.2f\n", a / b);
else printf("Division by zero!\n");
break;
case 4: printf("Result = %.2f\n", a * b); break;
default: printf("Invalid choice\n");
}
return 0;
}
DAY 2 — Conditional Statements (if-else / switch)

Q1. Input marks of 5 subjects. Calculate percentage and display grade.


Grade criteria: >=90%: A | >=80%: B | >=70%: C | >=60%: D | >=40%: E | <40%: F
#include <stdio.h>

int main() {
float phy, chem, bio, math, comp, total, percent;
printf("Enter marks for Physics, Chemistry, Biology, Math, Computer: ");
scanf("%f %f %f %f %f", &phy, &chem, &bio, &math, &comp);
total = phy + chem + bio + math + comp;
percent = total / 5.0;
printf("Percentage = %.2f%%\n", percent);
if (percent >= 90) printf("Grade: A\n");
else if (percent >= 80) printf("Grade: B\n");
else if (percent >= 70) printf("Grade: C\n");
else if (percent >= 60) printf("Grade: D\n");
else if (percent >= 40) printf("Grade: E\n");
else printf("Grade: F\n");
return 0;
}

Q2. Input basic salary of an employee and calculate Gross salary.


Rules: Salary <=10000: HRA=20%, DA=80% | <=20000: HRA=25%, DA=90% | >20000: HRA=30%,
DA=95%
#include <stdio.h>

int main() {
float basic, hra, da, gross;
printf("Enter basic salary: ");
scanf("%f", &basic);
if (basic <= 10000) { hra = 0.20 * basic; da = 0.80 * basic; }
else if (basic <= 20000) { hra = 0.25 * basic; da = 0.90 * basic; }
else { hra = 0.30 * basic; da = 0.95 * basic; }
gross = basic + hra + da;
printf("HRA = %.2f, DA = %.2f\n", hra, da);
printf("Gross Salary = %.2f\n", gross);
return 0;
}

Q3. Input electricity units and calculate total bill with 20% surcharge.
Rate: First 50 units: Rs.0.50 | Next 100: Rs.0.75 | Next 100: Rs.1.20 | Above 250: Rs.1.50
#include <stdio.h>

int main() {
int units;
float bill = 0;
printf("Enter units consumed: ");
scanf("%d", &units);
if (units <= 50) bill = units * 0.50;
else if (units <= 150) bill = 50*0.50 + (units-50)*0.75;
else if (units <= 250) bill = 50*0.50 + 100*0.75 + (units-150)*1.20;
else bill = 50*0.50 + 100*0.75 + 100*1.20 + (units-250)*1.50;
bill += bill * 0.20; // 20% surcharge
printf("Total Bill = Rs. %.2f\n", bill);
return 0;
}
DAY 3 — Loops (for / while / do-while)

Q1. Ask user to input 4 numbers between 1 to 100 and print max and min values.
#include <stdio.h>

int main() {
int n, max, min;
printf("Enter number 1: "); scanf("%d", &n);
max = min = n;
for (int i = 2; i <= 4; i++) {
printf("Enter number %d: ", i); scanf("%d", &n);
if (n > max) max = n;
if (n < min) min = n;
}
printf("Max = %d, Min = %d\n", max, min);
return 0;
}

Q2. Print elements of arithmetic series and find the sum. (a0, d and n are user input)
#include <stdio.h>

int main() {
int a0, d, n, sum = 0, term;
printf("Enter first term (a0), common difference (d), and count (n): ");
scanf("%d %d %d", &a0, &d, &n);
printf("AP Series: ");
for (int i = 0; i < n; i++) {
term = a0 + i * d;
printf("%d ", term);
sum += term;
}
printf("\nSum = %d\n", sum);
return 0;
}

Q3. Sum and average of 10 numbers input through keyboard.


#include <stdio.h>

int main() {
int num, sum = 0;
for (int i = 1; i <= 10; i++) {
printf("Enter number %d: ", i); scanf("%d", &num);
sum += num;
}
printf("Sum = %d, Average = %.2f\n", sum, sum / 10.0);
return 0;
}

Q4. Print patterns:


(i) Star left-aligned triangle (ii) Number staircase (iii) Number right-aligned triangle
#include <stdio.h>

int main() {
int n = 4;
// (i) Star pattern
printf("Pattern (i):\n");
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) printf("* ");
printf("\n");
}
// (ii) Number staircase
printf("Pattern (ii):\n");
for (int i = 1; i <= n; i++) {
printf("1");
for (int j = 2; j <= i; j++) printf("%d", j);
printf("\n");
}
// (iii) Right-aligned number triangle
printf("Pattern (iii):\n");
for (int i = 1; i <= n; i++) {
for (int s = 1; s <= n - i; s++) printf(" ");
for (int j = 1; j <= i; j++) printf("%d ", i);
printf("\n");
}
return 0;
}
DAY 4 — Loops: Continue, Functions Basics, Number Problems (Date:
04/02/26)

Q1. WAP in C to find square of positive numbers only using continue statement.
#include <stdio.h>

int main() {
int n;
for (int i = -5; i <= 5; i++) {
if (i <= 0) continue;
printf("Square of %d = %d\n", i, i * i);
}
return 0;
}

Q2. WAP in C to find factorial of a number through user input.


#include <stdio.h>

int main() {
int n;
long long fact = 1;
printf("Enter a number: "); scanf("%d", &n);
for (int i = 1; i <= n; i++) fact *= i;
printf("Factorial of %d = %lld\n", n, fact);
return 0;
}

Q3. WAP in C to swap two numbers with and without third variable. Program must be
menu-driven.
#include <stdio.h>

int main() {
int a, b, choice, temp;
printf("Enter two numbers: "); scanf("%d %d", &a, &b);
printf("1. Swap with 3rd var 2. Swap without 3rd var\nChoice: ");
scanf("%d", &choice);
if (choice == 1) {
temp = a; a = b; b = temp;
} else {
a = a + b; b = a - b; a = a - b;
}
printf("After swap: a = %d, b = %d\n", a, b);
return 0;
}

Q4. WAP in C to check if given number is prime. Also show all primes between two user input
numbers.
#include <stdio.h>

int isPrime(int n) {
if (n < 2) return 0;
for (int i = 2; i * i <= n; i++)
if (n % i == 0) return 0;
return 1;
}

int main() {
int n, lo, hi;
printf("Enter a number: "); scanf("%d", &n);
printf("%d is %sprime\n", n, isPrime(n) ? "" : "not ");
printf("Enter range (low high): "); scanf("%d %d", &lo, &hi);
printf("Primes between %d and %d: ", lo, hi);
for (int i = lo; i <= hi; i++) if (isPrime(i)) printf("%d ", i);
printf("\n");
return 0;
}

Q5. Enter a 3-digit number. Separate the digits and print each. Also reverse and print in
numeric and in words.
#include <stdio.h>

char* digitWord(int d) {
char* words[] = {"Zero","One","Two","Three","Four",
"Five","Six","Seven","Eight","Nine"};
return words[d];
}

int main() {
int num, h, t, u, rev;
printf("Enter 3-digit number: "); scanf("%d", &num);
h = num / 100; t = (num / 10) % 10; u = num % 10;
printf("Digits: %d %d %d\n", h, t, u);
printf("In words: %s %s %s\n", digitWord(h), digitWord(t), digitWord(u));
rev = u * 100 + t * 10 + h;
printf("Reversed number: %d\n", rev);
printf("Reversed in words: %s %s %s\n", digitWord(u), digitWord(t), digitWord(h));
return 0;
}
DAY 5 — Arrays and Functions

Q1. Write a program using a function to reverse an array.


#include <stdio.h>
#define N 5

void reverseArray(int arr[], int n) {


int temp, i = 0, j = n - 1;
while (i < j) { temp=arr[i]; arr[i]=arr[j]; arr[j]=temp; i++; j--; }
}

int main() {
int arr[N];
printf("Enter %d elements: ", N);
for (int i = 0; i < N; i++) scanf("%d", &arr[i]);
reverseArray(arr, N);
printf("Reversed: ");
for (int i = 0; i < N; i++) printf("%d ", arr[i]);
printf("\n");
return 0;
}

Q2. Find the max and min element of an array.


#include <stdio.h>
#define N 5

int main() {
int arr[N], max, min;
printf("Enter %d elements: ", N);
for (int i = 0; i < N; i++) scanf("%d", &arr[i]);
max = min = arr[0];
for (int i = 1; i < N; i++) {
if (arr[i] > max) max = arr[i];
if (arr[i] < min) min = arr[i];
}
printf("Max = %d, Min = %d\n", max, min);
return 0;
}

Q3. WAP that takes an array as input and returns sum of all elements.
#include <stdio.h>
#define N 5

int sumArray(int arr[], int n) {


int sum = 0;
for (int i = 0; i < n; i++) sum += arr[i];
return sum;
}

int main() {
int arr[N];
printf("Enter %d elements: ", N);
for (int i = 0; i < N; i++) scanf("%d", &arr[i]);
printf("Sum = %d\n", sumArray(arr, N));
return 0;
}

Q4. Delete from an array where position of the element is given.


#include <stdio.h>
#define N 6

int main() {
int arr[N], pos, n = N;
printf("Enter %d elements: ", N);
for (int i = 0; i < N; i++) scanf("%d", &arr[i]);
printf("Enter position to delete (0-indexed): "); scanf("%d", &pos);
for (int i = pos; i < n - 1; i++) arr[i] = arr[i + 1];
n--;
printf("After deletion: ");
for (int i = 0; i < n; i++) printf("%d ", arr[i]);
printf("\n");
return 0;
}

Q5. Insert an element in an array at the position given by the user.


#include <stdio.h>
#define MAX 10

int main() {
int arr[MAX], n, pos, val;
printf("Enter number of elements: "); scanf("%d", &n);
printf("Enter elements: ");
for (int i = 0; i < n; i++) scanf("%d", &arr[i]);
printf("Enter position to insert (0-indexed): "); scanf("%d", &pos);
printf("Enter value to insert: "); scanf("%d", &val);
for (int i = n; i > pos; i--) arr[i] = arr[i - 1];
arr[pos] = val;
n++;
printf("After insertion: ");
for (int i = 0; i < n; i++) printf("%d ", arr[i]);
printf("\n");
return 0;
}
DAY 6 — 2D Arrays / Matrix Operations & Pointers

Q1. WAP to take two m×n matrices and display their multiplication.
#include <stdio.h>
#define R 2
#define C 2

int main() {
int a[R][C], b[C][R], c[R][R];
printf("Enter matrix A (%dx%d):\n", R, C);
for (int i=0;i<R;i++) for (int j=0;j<C;j++) scanf("%d",&a[i][j]);
printf("Enter matrix B (%dx%d):\n", C, R);
for (int i=0;i<C;i++) for (int j=0;j<R;j++) scanf("%d",&b[i][j]);
for (int i=0;i<R;i++) for (int j=0;j<R;j++) {
c[i][j] = 0;
for (int k=0;k<C;k++) c[i][j] += a[i][k]*b[k][j];
}
printf("Product:\n");
for (int i=0;i<R;i++){for(int j=0;j<R;j++) printf("%d ",c[i][j]);printf("\n");}
return 0;
}

Q2. WAP to display the transpose of a matrix.


#include <stdio.h>
#define N 3

int main() {
int a[N][N];
printf("Enter %dx%d matrix:\n", N, N);
for (int i=0;i<N;i++) for (int j=0;j<N;j++) scanf("%d",&a[i][j]);
printf("Transpose:\n");
for (int i=0;i<N;i++){for(int j=0;j<N;j++) printf("%d ",a[j][i]);printf("\n");}
return 0;
}

Q3. Declare integer variable num=42, create pointer ptr pointing to num, print value using
pointer.
#include <stdio.h>

int main() {
int num = 42;
int *ptr = &num;
printf("Value using pointer: %d\n", *ptr);
return 0;
}

Q4. Declare array arr={10,20,30} and print memory address using both &arr[0] and arr.
#include <stdio.h>

int main() {
int arr[] = {10, 20, 30};
printf("Address using &arr[0]: %p\n", (void*)&arr[0]);
printf("Address using arr: %p\n", (void*)arr);
return 0;
}

Q5. Array data={1,3,5,7,9}. Print third element using data[2], *(data+2), and pointer offset.
#include <stdio.h>

int main() {
int data[] = {1, 3, 5, 7, 9};
int *p = data;
printf("data[2] = %d\n", data[2]);
printf("*(data + 2) = %d\n", *(data + 2));
printf("*(p + 2) = %d\n", *(p + 2));
return 0;
}

Q6. x=5, y=10. Create swap function using two pointers. Print before/after values.
#include <stdio.h>

void swap(int *a, int *b) { int t = *a; *a = *b; *b = t; }


int main() {
int x = 5, y = 10;
printf("Before: x=%d, y=%d\n", x, y);
swap(&x, &y);
printf("After: x=%d, y=%d\n", x, y);
return 0;
}

Q7. Array arr={1,2,3,4}. Use pointer p=arr and for loop to sum elements by incrementing p++.
#include <stdio.h>

int main() {
int arr[] = {1, 2, 3, 4};
int *p = arr, sum = 0;
for (int i = 0; i < 4; i++, p++) sum += *p;
printf("Sum = %d\n", sum);
return 0;
}
DAY 7 — PPS Lab on Structures (Date: 25 Mar 2026)

Q1. Create structure "Student" with members name, age, total marks. Input data for two
students, display and find average marks.
#include <stdio.h>
#include <string.h>

struct Student {
char name[50];
int age;
float totalMarks;
};

int main() {
struct Student s[2];
for (int i = 0; i < 2; i++) {
printf("Enter name, age, total marks for student %d: ", i+1);
scanf("%s %d %f", s[i].name, &s[i].age, &s[i].totalMarks);
}
printf("\nStudent Details:\n");
for (int i = 0; i < 2; i++)
printf("%s | Age: %d | Marks: %.2f\n", s[i].name, s[i].age, s[i].totalMarks);
printf("Average Marks = %.2f\n", (s[0].totalMarks + s[1].totalMarks) / 2.0);
return 0;
}

Q2. Define structure "Date" with members day, month, year. Input two dates and find the
difference in days.
#include <stdio.h>

struct Date { int day, month, year; };

int toDays(struct Date d) {


return [Link] * 365 + [Link] * 30 + [Link];
}

int main() {
struct Date d1, d2;
printf("Enter date 1 (dd mm yyyy): "); scanf("%d %d %d",&[Link],&[Link],&[Link]);
printf("Enter date 2 (dd mm yyyy): "); scanf("%d %d %d",&[Link],&[Link],&[Link]);
int diff = toDays(d2) - toDays(d1);
if (diff < 0) diff = -diff;
printf("Difference = %d days (approx)\n", diff);
return 0;
}

Q3. Create structure Complex with real and imaginary parts. WAP to add and multiply two
complex numbers.
#include <stdio.h>

struct Complex { float real, imag; };

int main() {
struct Complex c1, c2, sum, prod;
printf("Enter c1 (real imag): "); scanf("%f %f",&[Link],&[Link]);
printf("Enter c2 (real imag): "); scanf("%f %f",&[Link],&[Link]);
[Link] = [Link] + [Link]; [Link] = [Link] + [Link];
[Link] = [Link]*[Link] - [Link]*[Link];
[Link] = [Link]*[Link] + [Link]*[Link];
printf("Sum = %.2f + %.2fi\n", [Link], [Link]);
printf("Prod = %.2f + %.2fi\n", [Link], [Link]);
return 0;
}

Q4. Define structure Time with hours, minutes, seconds. Input two times, add them, display in
proper format.
#include <stdio.h>

struct Time { int h, m, s; };

int main() {
struct Time t1, t2, res;
printf("Enter time1 (hh mm ss): "); scanf("%d %d %d",&t1.h,&t1.m,&t1.s);
printf("Enter time2 (hh mm ss): "); scanf("%d %d %d",&t2.h,&t2.m,&t2.s);
res.s = (t1.s + t2.s) % 60;
res.m = (t1.m + t2.m + (t1.s + t2.s) / 60) % 60;
res.h = t1.h + t2.h + (t1.m + t2.m + (t1.s + t2.s) / 60) / 60;
printf("Result = %02d:%02d:%02d\n", res.h, res.m, res.s);
return 0;
}

Q5. WAP in C to implement a student database using structures and perform operations like
add, delete, and search.
#include <stdio.h>
#include <string.h>

#define MAX 50
struct Student { int id; char name[50]; float marks; };
struct Student db[MAX];
int count = 0;

void addStudent() {
printf("Enter id, name, marks: ");
scanf("%d %s %f", &db[count].id, db[count].name, &db[count].marks);
count++; printf("Added.\n");
}
void deleteStudent() {
int id; printf("Enter id to delete: "); scanf("%d", &id);
for (int i = 0; i < count; i++) if (db[i].id == id) {
for (int j = i; j < count-1; j++) db[j] = db[j+1];
count--; printf("Deleted.\n"); return;
}
printf("Not found.\n");
}
void searchStudent() {
int id; printf("Enter id to search: "); scanf("%d", &id);
for (int i = 0; i < count; i++) if (db[i].id == id) {
printf("Found: %d %s %.2f\n", db[i].id, db[i].name, db[i].marks);
return;
}
printf("Not found.\n");
}
int main() {
int ch;
do {
printf("[Link] [Link] [Link] [Link]: "); scanf("%d",&ch);
if (ch==1) addStudent();
else if (ch==2) deleteStudent();
else if (ch==3) searchStudent();
} while (ch != 0);
return 0;
}

You might also like