0% found this document useful (0 votes)
10 views25 pages

C Lab Manual

The document contains a list of programming experiments with corresponding C code examples and outputs. Each experiment focuses on a specific programming task, such as printing messages, performing arithmetic operations, and manipulating data structures like matrices. The document serves as a practical guide for learning basic programming concepts and techniques.

Uploaded by

saksham.sahu2807
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)
10 views25 pages

C Lab Manual

The document contains a list of programming experiments with corresponding C code examples and outputs. Each experiment focuses on a specific programming task, such as printing messages, performing arithmetic operations, and manipulating data structures like matrices. The document serves as a practical guide for learning basic programming concepts and techniques.

Uploaded by

saksham.sahu2807
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

EXPERIMENT LIST

[Link]. Name of Experiment Date

1 Write a program to print ― HELLO FRIENDS

Write a program that reads two nos. from key board and gives their addition,
2
subtraction, multiplication, division, modulo, average
3 Write a program to convert days into months and days

4 Write a program to find whether the number is odd or even

5 Write a program to solve Quadratic Equation

Write a program to select & print the largest of the three nos. using Nested-
6
If-Else statement

7 Write a program to display multiplication table


1 1 1
8 Write a program to print 1 + 2 + 3 + 4 … … … + 1/𝑁 series

The distance between two cities (In KM) is input through key board. Write a
9 program to convert and print this distance in meters, feet, inches &
centimeters

10 Program to print * patterns

11 Write a program to print Fibonacci series

12 Write a program to calculate factorial of a number

13 Write a program to reverse the digit

14 Add, subtract and multiply two nos. using switch statement

15 Write a program to add two matrices

16 Write a program to multiply two matrices

17 Write a program to count total words in text

1
Problem Statement:

1. Write a program to print ―HELLO FRIENDS

C Program:

#include <stdio.h>

int main() {
printf("HELLO FRIENDS");
return 0;
}

Output:

HELLO FRIENDS

2
Problem Statement:

2. Write a program that reads two nos. from key board and gives their addition, subtraction,
multiplication, division, modulo, average

C Program:

#include <stdio.h>

int main() {
int a, b;
float average;

printf("Enter two numbers: ");


scanf("%d %d", &a, &b);

printf("Addition = %d\n", a + b);


printf("Subtraction = %d\n", a - b);
printf("Multiplication = %d\n", a * b);

if (b != 0) {
printf("Division = %.2f\n", (float)a / b);
printf("Modulo = %d\n", a % b);
} else {
printf("Division and Modulo by zero is not allowed.\n");
}

average = (a + b) / 2.0;
printf("Average = %.2f\n", average);

return 0;
}

3
Output:

Enter two numbers: 10 5


Addition = 15
Subtraction = 5
Multiplication = 50
Division = 2.00
Modulo = 0
Average = 7.50

4
Problem Statement:

3. Write a program to convert days into months and days

C Program:

#include <stdio.h>

int main() {
int totalDays, months, days;

printf("Enter total number of days: ");


scanf("%d", &totalDays);

months = totalDays / 30;


days = totalDays % 30;

printf("Months = %d\n", months);


printf("Remaining Days = %d\n", days);

return 0;
}

Output:

Enter total number of days: 95


Months = 3
Remaining Days = 5

5
Problem Statement:

4. Write a program to find whether the number is odd or even

C Program:

#include <stdio.h>

int main() {
int num;

printf("Enter a number: ");


scanf("%d", &num);

if (num % 2 == 0)
printf("%d is an Even number.\n", num);
else
printf("%d is an Odd number.\n", num);

return 0;
}

Output

Enter a number: 7
7 is an Odd number.

6
Problem Statement:

5. Write a program to solve Quadratic Equation

C Program:

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

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

printf("Enter coefficients a, b and c: ");


scanf("%f %f %f", &a, &b, &c);

d = b * b - 4 * a * c;

if (d > 0) {
root1 = (-b + sqrt(d)) / (2 * a);
root2 = (-b - sqrt(d)) / (2 * a);
printf("Roots are real and distinct.\n");
printf("Root1 = %.2f\nRoot2 = %.2f\n", root1, root2);
}
else if (d == 0) {
root1 = -b / (2 * a);
printf("Roots are real and equal.\n");
printf("Root = %.2f\n", root1);
}
else {
printf("Roots are imaginary.\n");
root1 = -b / (2 * a);
root2 = sqrt(-d) / (2 * a);

7
printf("Root1 = %.2f + %.2fi\n", root1, root2);
printf("Root2 = %.2f - %.2fi\n", root1, root2);
}

return 0;
}

Output

Enter coefficients a, b and c: 1 -3 2


Roots are real and distinct.
Root1 = 2.00
Root2 = 1.00

8
Problem Statement:

6. Write a program to select & print the largest of the three nos. using Nested-If-Else
statement

C Program:
#include <stdio.h>
int main() {
int a, b, c;
printf("Enter three numbers: ");
scanf("%d %d %d", &a, &b, &c);
if (a > b) {
if (a > c)
printf("%d is the largest number.\n", a);
else
printf("%d is the largest number.\n", c);
} else {
if (b > c)
printf("%d is the largest number.\n", b);
else
printf("%d is the largest number.\n", c);
}
return 0;
}

Output

Enter three numbers: 10 25 15


25 is the largest number

9
Problem Statement:

7. Write a program to display multiplication table

C Program:
#include <stdio.h>
int main() {
int num, i;
printf("Enter a number: ");
scanf("%d", &num);
printf("Multiplication Table of %d:\n", num);
for (i = 1; i <= 10; i++) {
printf("%d x %d = %d\n", num, i, num * i);
}
return 0;
}

Output
Enter a number: 5
Multiplication Table of 5:
5x1=5
5 x 2 = 10
5 x 3 = 15
5 x 4 = 20
5 x 5 = 25
5 x 6 = 30
5 x 7 = 35
5 x 8 = 40
5 x 9 = 45
5 x 10 = 50

10
Problem Statement:

1 1 1
8. Write a program to print 1 + 2 + 3 + 4 … … … + 1/𝑁 series

C Program:

#include <stdio.h>
int main() {
int i, n;
float sum = 0.0;

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


scanf("%d", &n);

printf("Series: ");
for (i = 1; i <= n; i++) {
printf("1/%d", i);
if (i < n)
printf(" + ");
sum = sum + (1.0 / i);
}

printf("\nSum of the series = %.2f\n", sum);

return 0;
}

Output

Enter the value of N: 5


Series: 1/1 + 1/2 + 1/3 + 1/4 + 1/5
Sum of the series = 2.28

11
Problem Statement:

9. The distance between two cities (In KM) is input through key board. Write a program to
convert and print this distance in meters, feet, inches & centimeters

C Program:

#include <stdio.h>

int main() {
float km;
float meters, feet, inches, centimeters;

printf("Enter distance between two cities (in KM): ");


scanf("%f", &km);

meters = km * 1000;
centimeters = km * 100000;
inches = km * 39370.1;
feet = km * 3280.84;

printf("Distance in meters = %.2f m\n", meters);


printf("Distance in feet = %.2f ft\n", feet);
printf("Distance in inches = %.2f in\n", inches);
printf("Distance in centimeters = %.2f cm\n", centimeters);

return 0;
}

12
Output:

Enter distance between two cities (in KM): 5


Distance in meters = 5000.00 m
Distance in feet = 16404.20 ft
Distance in inches = 196850.50 in
Distance in centimeters = 500000.00 cm

13
Problem Statement:

10. Program to print Patterns


*

**

***

****

C Program:

#include <stdio.h>

int main() {
int i, j;
int rows = 4; // Number of rows in the pattern

for (i = 1; i <= rows; i++) {


for (j = 1; j <= i; j++) {
printf("* ");
}
printf("\n"); // Move to next row
}

return 0;
}

Output

*
**
***
****

14
Problem Statement:

11. Write a program to print Fibonacci series

C Program:

#include <stdio.h>

int main() {
int n, i;
int t1 = 0, t2 = 1, nextTerm;

printf("Enter the number of terms: ");


scanf("%d", &n);

printf("Fibonacci Series: ");

for (i = 1; i <= n; i++) {


printf("%d ", t1);
nextTerm = t1 + t2;
t1 = t2;
t2 = nextTerm;
}

printf("\n");
return 0;
}

Output

Enter the number of terms: 10


Fibonacci Series: 0 1 1 2 3 5 8 13 21 34

15
Problem Statement:

12. Write a program to calculate factorial of a number

#include <stdio.h>

int main() {

int n, i;

unsigned long long factorial = 1; // Use unsigned long long for large factorials

printf("Enter a positive integer: ");

scanf("%d", &n);

if (n < 0)

printf("Factorial of a negative number is not defined.\n");

else {

for (i = 1; i <= n; i++) {

factorial *= i; // Multiply factorial by i

printf("Factorial of %d = %llu\n", n, factorial);

return 0;

Output:

Enter a positive integer: 5

Factorial of 5 = 120

16
Problem Statement:

13. Write a program to reverse the digit

C Program:

#include <stdio.h>

int main() {
int num, reversed = 0, remainder;

printf("Enter an integer: ");


scanf("%d", &num);

int original = num; // Store the original number

while (num != 0) {
remainder = num % 10; // Get the last digit
reversed = reversed * 10 + remainder; // Append it to reversed
num = num / 10; // Remove the last digit
}

printf("Reversed number of %d is %d\n", original, reversed);

return 0;
}

Output:

Enter an integer: 12345


Reversed number of 12345 is 54321

17
Problem statement:

14. Add, subtract and multiply two nos. using switch statement

C Program:

#include <stdio.h>

int main() {
int num1, num2, choice;
int result;

printf("Enter two numbers: ");


scanf("%d %d", &num1, &num2);

printf("Choose an operation:\n");
printf("1. Addition\n");
printf("2. Subtraction\n");
printf("3. Multiplication\n");
printf("Enter your choice (1-3): ");
scanf("%d", &choice);

switch(choice) {
case 1:
result = num1 + num2;
printf("Addition = %d\n", result);
break;
case 2:
result = num1 - num2;
printf("Subtraction = %d\n", result);
break;
case 3:
result = num1 * num2;

18
printf("Multiplication = %d\n", result);
break;
default:
printf("Invalid choice! Please choose 1, 2, or 3.\n");
}

return 0;
}

Output:

Enter two numbers: 10 5


Choose an operation:
1. Addition
2. Subtraction
3. Multiplication
Enter your choice (1-3): 2
Subtraction = 5

19
Problem statement:

15. Write a program to add two matrices

#include <stdio.h>

int main()
{
int rows, cols;
int i, j;

printf("Enter number of rows and columns: ");


scanf("%d %d", &rows, &cols);

int matrix1[rows][cols], matrix2[rows][cols], sum[rows][cols];

// Input first matrix


printf("Enter elements of first matrix:\n");
for (i = 0; i < rows; i++) {
for (j = 0; j < cols; j++) {
scanf("%d", &matrix1[i][j]);
}
}

// Input second matrix


printf("Enter elements of second matrix:\n");
for (i = 0; i < rows; i++) {
for (j = 0; j < cols; j++) {
scanf("%d", &matrix2[i][j]);
}
}

// Add the matrices

20
for (i = 0; i < rows; i++) {
for (j = 0; j < cols; j++) {
sum[i][j] = matrix1[i][j] + matrix2[i][j];
}
}

// Print the sum matrix


printf("Sum of the two matrices:\n");
for (i = 0; i < rows; i++) {
for (j = 0; j < cols; j++) {
printf("%d\t", sum[i][j]);
}
printf("\n");
}

return 0;
}

Output:

Enter number of rows and columns: 2 2


Enter elements of first matrix:
12
34
Enter elements of second matrix:
56
78
Sum of the two matrices:
6 8
10 12

21
Problem statement:

16. Write a program to multiply two matrices

C Program:

#include <stdio.h>

int main() {
int r1, c1, r2, c2;
int i, j, k;

printf("Enter rows and columns of first matrix: ");


scanf("%d %d", &r1, &c1);

printf("Enter rows and columns of second matrix: ");


scanf("%d %d", &r2, &c2);

// Check if multiplication is possible


if (c1 != r2) {
printf("Matrix multiplication not possible! Columns of first matrix must equal rows of
second matrix.\n");
return 0;
}

int matrix1[r1][c1], matrix2[r2][c2], product[r1][c2];

// Initialize product matrix to 0


for (i = 0; i < r1; i++) {
for (j = 0; j < c2; j++) {
product[i][j] = 0;
}
}

22
// Input first matrix
printf("Enter elements of first matrix:\n");
for (i = 0; i < r1; i++) {
for (j = 0; j < c1; j++) {
scanf("%d", &matrix1[i][j]);
}
}

// Input second matrix


printf("Enter elements of second matrix:\n");
for (i = 0; i < r2; i++) {
for (j = 0; j < c2; j++) {
scanf("%d", &matrix2[i][j]);
}
}

// Multiply matrices
for (i = 0; i < r1; i++) {
for (j = 0; j < c2; j++) {
for (k = 0; k < c1; k++) {
product[i][j] += matrix1[i][k] * matrix2[k][j];
}
}
}

// Print the product matrix


printf("Product of the two matrices:\n");
for (i = 0; i < r1; i++) {
for (j = 0; j < c2; j++) {
printf("%d\t", product[i][j]);
}
printf("\n");

23
}

return 0;
}

Output:

Enter rows and columns of first matrix 2 x 3:


Enter rows and columns of second matrix 3 x 2:
Enter elements of first matrix:
123
456
Enter elements of second matrix:
78
9 10
11 12
Product of the two matrices:
58 64
139 154

24
Problem statement:

17. Write a program to count total words in text

C Program:
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int main() {
char text[1000];
int i, wordCount = 0;
int inWord = 0; // Flag to track if inside a word
printf("Enter a text: ");
fgets(text, sizeof(text), stdin); // Read a line of text including spaces
for (i = 0; text[i] != '\0'; i++) {
if (isspace(text[i]) || text[i] == '\n') {
if (inWord) {
wordCount++;
inWord = 0;
}
} else {
inWord = 1; // Currently inside a word
}
}
// Count the last word if the text doesn't end with space
if (inWord)
wordCount++;
printf("Total number of words: %d\n", wordCount);
return 0;
}

Output:
Enter a text: Hello friends, welcome to C programming.
Total number of words: 6

25

You might also like