0% found this document useful (0 votes)
6 views27 pages

C Programming Experimentes 6 - 18

The document contains multiple C programming exercises, including calculations for the lateral surface area, total surface area, and volume of a cuboid, evaluating expressions, generating patterns, reversing a number, checking for prime numbers, performing matrix operations, checking for palindromes, and solving the Towers of Hanoi problem. Each exercise includes code snippets, expected outputs, and user inputs. The programs cover a variety of fundamental programming concepts such as loops, conditionals, recursion, and functions.

Uploaded by

23tq1a05c7
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views27 pages

C Programming Experimentes 6 - 18

The document contains multiple C programming exercises, including calculations for the lateral surface area, total surface area, and volume of a cuboid, evaluating expressions, generating patterns, reversing a number, checking for prime numbers, performing matrix operations, checking for palindromes, and solving the Towers of Hanoi problem. Each exercise includes code snippets, expected outputs, and user inputs. The programs cover a variety of fundamental programming concepts such as loops, conditionals, recursion, and functions.

Uploaded by

23tq1a05c7
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

exp5.

Write and execute a C program to compute Lateral surface area, Total surface area and volume
of a cuboid given length, breadth and height.

#include <stdio.h>

int main() {

float length, breadth, height;

float lateral_surface_area, total_surface_area, volume;

// Input dimensions

printf("Enter the length of the cuboid: ");

scanf("%f", &length);

printf("Enter the breadth of the cuboid: ");

scanf("%f", &breadth);

printf("Enter the height of the cuboid: ");

scanf("%f", &height);

// Calculating lateral surface area, total surface area, and volume

lateral_surface_area = 2 * height * (length + breadth);

total_surface_area = 2 * (length * breadth + breadth * height + height * length);

volume = length * breadth * height;

// Output results

printf("Lateral Surface Area: %.2f\n", lateral_surface_area);

printf("Total Surface Area: %.2f\n", total_surface_area);


printf("Volume: %.2f\n", volume);

return 0;

Output:

Enter the length of the cuboid: 3.4

Enter the breadth of the cuboid: 3.5

Enter the height of the cuboid: 55

Lateral Surface Area: 759.00

Total Surface Area: 782.80

Volume: 654.50

Exp 6. Write and execute a C program to evaluate an expression using operator precedence and
associativity rule. (hint: a+b/c*d –(e * (f-z)%k)

#include <stdio.h>

int main() {

int a, b, c, d, e, f, z, k, result;

// Input values

printf("Enter values for a, b, c, d, e, f, z, and k:\n");

printf("a = ");

scanf("%d", &a);

printf("b = ");

scanf("%d", &b);

printf("c = ");

scanf("%d", &c);
printf("d = ");

scanf("%d", &d);

printf("e = ");

scanf("%d", &e);

printf("f = ");

scanf("%d", &f);

printf("z = ");

scanf("%d", &z);

printf("k = ");

scanf("%d", &k);

// Evaluate the expression

result = a + b / c * d - (e * (f - z) % k);

// Output the result

printf("The result of the expression is: %d\n", result);

return 0;

Output:

Enter values for a, b, c, d, e, f, z, and k:

a=2

b=3

c=6

d=5
e=6

f=8

z=9

k=4

The result of the expression is: 4

Exp:7. Write and execute a C program to display the following pattern using for loop given n rows
(hint n=4). 1

12

123

1234

#include <stdio.h>

int main() {

int n, i, j;

// Input the number of rows

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

scanf("%d", &n);

// Generate the pattern

for (i = 1; i <= n; i++) { // Iterate over rows

for (j = 1; j <= i; j++) { // Iterate over columns

if (j == 1 || j == i) {

// Print numbers at the boundaries

printf("%d ", j);

} else {
// Print spaces between numbers

printf(" ");

// Move to the next line after each row

printf("\n");

return 0;

Output for :4

1 2

1 2 3

1 2 3 4

exp8. Write and execute a C program to reverse a 4 digit number.

#include <stdio.h>

int main() {

int number, reversedNumber = 0, remainder;

// Input the 4-digit number

printf("Enter a 4-digit number: ");

scanf("%d", &number);
// Check if the number is a valid 4-digit number

if (number >= 1000 && number <= 9999) {

while (number != 0) {

remainder = number % 10; // Get the last digit

reversedNumber = reversedNumber * 10 + remainder; // Build reversed number

number /= 10; // Remove the last digit

// Output the reversed number

printf("Reversed Number: %d\n", reversedNumber);

} else {

printf("Please enter a valid 4-digit number.\n");

return 0;

Output:
Enter a 4-digit number: 1234
Reversed Number: 4321
exp9. Write and execute a C program to print all the prime numbers
from 2 to n.
#include <stdio.h>

int main() {

int n, i, j, isPrime;

// Input the value of n


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

scanf("%d", &n);

printf("Prime numbers from 2 to %d are:\n", n);

// Loop through numbers from 2 to n

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

isPrime = 1; // Assume the number is prime

// Check divisors for the current number

for (j = 2; j <= i / 2; j++) {

if (i % j == 0) {

isPrime = 0; // Not a prime number

break;

// If the number is prime, print it

if (isPrime == 1) {

printf("%d ", i);

printf("\n");

return 0;
}

Output:

Enter the value of n: 5

Prime numbers from 2 to 5 are:

235

exp10. Write and execute a C program to read two matrices to perform matrix addition.

#include <stdio.h>

int main() {

int rows, columns, i, j;

// Input dimensions of the matrices

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

scanf("%d", &rows);

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

scanf("%d", &columns);

// Declare matrices

int matrix1[rows][columns], matrix2[rows][columns], result[rows][columns];

// Input first matrix

printf("Enter elements of the first matrix:\n");

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

for (j = 0; j < columns; j++) {

printf("Element [%d][%d]: ", i + 1, j + 1);


scanf("%d", &matrix1[i][j]);

// Input second matrix

printf("Enter elements of the second matrix:\n");

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

for (j = 0; j < columns; j++) {

printf("Element [%d][%d]: ", i + 1, j + 1);

scanf("%d", &matrix2[i][j]);

// Perform matrix addition

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

for (j = 0; j < columns; j++) {

result[i][j] = matrix1[i][j] + matrix2[i][j];

// Display the result matrix

printf("Resultant matrix after addition:\n");

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

for (j = 0; j < columns; j++) {

printf("%d ", result[i][j]);


}

printf("\n");

return 0;

Output: Enter the number of rows: 2

Enter the number of columns: 2

Enter elements of the first matrix:

Element [1][1]: 1

Element [1][2]: 2

Element [2][1]: 3

Element [2][2]: 4

Enter elements of the second matrix:

Element [1][1]: 5

Element [1][2]: 6

Element [2][1]: 78

Element [2][2]: 9

Resultant matrix after addition:

68

81 13

11. Write and execute a C program to perform matrix multiplication

#include <stdio.h>

int main() {
int rows1, cols1, rows2, cols2, i, j, k;

// Input dimensions of the first matrix

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

scanf("%d %d", &rows1, &cols1);

// Input dimensions of the second matrix

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

scanf("%d %d", &rows2, &cols2);

// Check if multiplication is possible

if (cols1 != rows2) {

printf("Matrix multiplication not possible. Number of columns of the first matrix must
equal the number of rows of the second matrix.\n");

return 0;

// Declare matrices

int matrix1[rows1][cols1], matrix2[rows2][cols2], result[rows1][cols2];

// Input elements of the first matrix

printf("Enter elements of the first matrix:\n");

for (i = 0; i < rows1; i++) {

for (j = 0; j < cols1; j++) {

printf("Element [%d][%d]: ", i + 1, j + 1);


scanf("%d", &matrix1[i][j]);

// Input elements of the second matrix

printf("Enter elements of the second matrix:\n");

for (i = 0; i < rows2; i++) {

for (j = 0; j < cols2; j++) {

printf("Element [%d][%d]: ", i + 1, j + 1);

scanf("%d", &matrix2[i][j]);

// Initialize the result matrix to 0

for (i = 0; i < rows1; i++) {

for (j = 0; j < cols2; j++) {

result[i][j] = 0;

// Perform matrix multiplication

for (i = 0; i < rows1; i++) {

for (j = 0; j < cols2; j++) {

for (k = 0; k < cols1; k++) {


result[i][j] += matrix1[i][k] * matrix2[k][j];

// Display the result matrix

printf("Resultant matrix after multiplication:\n");

for (i = 0; i < rows1; i++) {

for (j = 0; j < cols2; j++) {

printf("%d ", result[i][j]);

printf("\n");

return 0;

Output: Matrix 1 (2x3):

123

456

Matrix 2 (3x2):

78

9 10

11 12
Resultant Matrix (2x2):

58 64

139 154

exp12. a. Write and execute a C program to check whether the given string is
palindrome or not.

#include <stdio.h>

#include <string.h>

int main() {

char str[100], reversedStr[100];

int length, i, isPalindrome = 1;

// Input the string

printf("Enter a string: ");

scanf("%s", str);

// Get the length of the string

length = strlen(str);

// Compare characters from the beginning and end

for (i = 0; i < length / 2; i++) {

if (str[i] != str[length - i - 1]) {


isPalindrome = 0; // Not a palindrome

break;

// Output result

if (isPalindrome) {

printf("The string \"%s\" is a palindrome.\n", str);

} else {

printf("The string \"%s\" is not a palindrome.\n", str);

return 0;

Output:

Input: Enter a string: level

The string "level" is a palindrome.

b. Write and execute a C program to read a line of text and display the vowels
and consonants.

// C program to check if a character

// is a vowel or consonant

#include <stdio.h>
// Driver code

int main()

char ch = 'A';

// Checking if the character ch

// is a vowel or not.

if (ch == 'a' || ch == 'A' || ch == 'e' || ch == 'E'

|| ch == 'i' || ch == 'I' || ch == 'o' || ch == 'O'

|| ch == 'u' || ch == 'U') {

printf("The character %c is a vowel.\n", ch);

else {

printf("The character %c is a consonant.\n", ch);

return 0;

Output
The character A is a vowel.
Exp :13. Write and execute a C program to compute the following series using functions 1+ x+(x2/2!) +
(x3/3!) +………+ (xn/n!)

// C program to find sum of series

// 1 + x/1 + x^2/2 + x^3/3 + ....+ x^n/n

#include <math.h>

#include <stdio.h>

double sum(int x, int n)

double i, total = 1.0;

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

total = total +

(pow(x, i) / i);

return total;

// Driver code

int main()

int x = 2;
int n = 5;

printf("%.2f", sum(x, n));

return 0;

Output :

18.07
Exp:14. Write and execute a C program to compute the sum of first n natural numbers using recursion.

#include <stdio.h>

// Recursive function to calculate the sum

int sumOfNaturalNumbers(int n) {

if (n == 0) {

return 0; // Base case: sum of first 0 numbers is 0

return n + sumOfNaturalNumbers(n - 1); // Recursive case

int main() {

int n;

// Input the value of n

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

scanf("%d", &n);
if (n < 0) {

printf("Please enter a non-negative integer.\n");

} else {

// Call the recursive function and display the result

int sum = sumOfNaturalNumbers(n);

printf("The sum of the first %d natural numbers is: %d\n", n, sum);

return 0;

Output:

Enter the value of n: 5

The sum of the first 5 natural numbers is: 15

exp15. Write and execute a C program on” TOWERS OF HANOI PROBLEM“ using
recursion.

#include <stdio.h>

// Function to solve Towers of Hanoi

void towersOfHanoi(int n, char source, char target, char auxiliary) {

if (n == 1) {

printf("Move disk 1 from %c to %c\n", source, target);

return;

}
// Step 1: Move (n-1) disks from source to auxiliary

towersOfHanoi(n - 1, source, auxiliary, target);

// Step 2: Move the nth disk from source to target

printf("Move disk %d from %c to %c\n", n, source, target);

// Step 3: Move (n-1) disks from auxiliary to target

towersOfHanoi(n - 1, auxiliary, target, source);

int main() {

int n;

// Input the number of disks

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

scanf("%d", &n);

// Solve the Towers of Hanoi problem

printf("The sequence of moves to solve the Towers of Hanoi with %d disks is:\
n", n);

towersOfHanoi(n, 'A', 'C', 'B'); // A = Source, C = Target, B = Auxiliary


return 0;

Output:

Input:

Enter the number of disks: 3

Output:

The sequence of moves to solve the Towers of Hanoi with 3 disks is:

Move disk 1 from A to C

Move disk 2 from A to B

Move disk 1 from C to B

Move disk 3 from A to C

Move disk 1 from B to A

Move disk 2 from B to C

Move disk 1 from A to C


16. Write and execute a C program to read two integers and compute sum, difference,
product, quotient using “functions with arguments and with return values”.

#include <stdio.h>

// Function to compute the sum

int computeSum(int a, int b) {

return a + b;

}
// Function to compute the difference

int computeDifference(int a, int b) {

return a - b;

// Function to compute the product

int computeProduct(int a, int b) {

return a * b;

// Function to compute the quotient

float computeQuotient(int a, int b) {

if (b != 0) {

return (float)a / b; // Return quotient as a floating-point number

} else {

printf("Error: Division by zero.\n");

return 0.0; // Return 0 in case of division by zero

int main() {

int num1, num2;


// Input two integers

printf("Enter the first integer: ");

scanf("%d", &num1);

printf("Enter the second integer: ");

scanf("%d", &num2);

// Compute results using functions

int sum = computeSum(num1, num2);

int difference = computeDifference(num1, num2);

int product = computeProduct(num1, num2);

float quotient = computeQuotient(num1, num2);

// Display the results

printf("Sum: %d\n", sum);

printf("Difference: %d\n", difference);

printf("Product: %d\n", product);

if (num2 != 0) {

printf("Quotient: %.2f\n", quotient);

return 0;

Output:

Input:
Enter the first integer: 10

Enter the second integer: 5

Sum: 15

Difference: 5

Product: 50

Quotient: 2.00

Exp:17. Consider and execute a structure with the following structure members
a. Student name b. PIN c.subject1 marks d. subject2 marks e. subject3 marks Write and
execute a Program to compute individual subject total, individual student marks total
for n number of students.
#include <stdio.h>

struct Student {
char name[50];
int pin;
int subject1;
int subject2;
int subject3;
int totalMarks;
};

int main() {
int n, i;
int totalSubject1 = 0, totalSubject2 = 0, totalSubject3 = 0;

// Input the number of students


printf("Enter the number of students: ");
scanf("%d", &n);

struct Student students[n];

// Input student details


for (i = 0; i < n; i++) {
printf("\nEnter details for student %d:\n", i + 1);
printf("Name: ");
scanf("%s", students[i].name);
printf("PIN: ");
scanf("%d", &students[i].pin);
printf("Subject 1 marks: ");
scanf("%d", &students[i].subject1);
printf("Subject 2 marks: ");
scanf("%d", &students[i].subject2);
printf("Subject 3 marks: ");
scanf("%d", &students[i].subject3);

// Compute total marks for the student


students[i].totalMarks = students[i].subject1 + students[i].subject2 +
students[i].subject3;

// Add to subject totals


totalSubject1 += students[i].subject1;
totalSubject2 += students[i].subject2;
totalSubject3 += students[i].subject3;
}

// Display individual student totals


printf("\nStudent-wise Total Marks:\n");
for (i = 0; i < n; i++) {
printf("Student: %s, PIN: %d, Total Marks: %d\n", students[i].name,
students[i].pin, students[i].totalMarks);
}

// Display individual subject totals


printf("\nTotal Marks for Each Subject:\n");
printf("Subject 1: %d\n", totalSubject1);
printf("Subject 2: %d\n", totalSubject2);
printf("Subject 3: %d\n", totalSubject3);

return 0;
}
Output:
Input: Enter the number of students: 2
Enter details for student 1:
Name: Alice
PIN: 123
Subject 1 marks: 85
Subject 2 marks: 90
Subject 3 marks: 88

Enter details for student 2:


Name: Bob
PIN: 456
Subject 1 marks: 78
Subject 2 marks: 82
Subject 3 marks: 79
Output: Student-wise Total Marks:
Student: Alice, PIN: 123, Total Marks: 263
Student: Bob, PIN: 456, Total Marks: 239

Total Marks for Each Subject:


Subject 1: 163
Subject 2: 172
Subject 3: 167
exp18. Write and execute a c program to read, retrieve, and display the members of a
union
#include <stdio.h>

// Define a union
union Data {
int intValue;
float floatValue;
char charValue;
};

int main() {
union Data data; // Declare the union variable

// Read values into the union


printf("Enter an integer: ");
scanf("%d", &[Link]);
printf("Stored integer: %d\n", [Link]);

printf("Enter a float: ");


scanf("%f", &[Link]);
printf("Stored float: %.2f\n", [Link]);

printf("Enter a character: ");


scanf(" %c", &[Link]); // Space before %c to handle newline issues
printf("Stored character: %c\n", [Link]);

return 0;
}
Output:
Input: Enter an integer: 10
Enter a float: 20.5
Enter a character: A
Stored integer: 10
Stored float: 20.50
Stored character: A

You might also like