RU-CSE-C Programming Lab Manual
RU-CSE-C Programming Lab Manual
No Program Page No
1 Write a C program using switch statement to design a basic 2
calculator that performs the basic operations such as addition,
subtraction, multiplication, and division.
2 Write a C program to find the coefficients of a quadratic equation 5
and compute its roots.
3 Write a C program to find GCD and LCM of 2 integers. 8
4 The Fibonacci sequence is a sequence where the next term is the 10
sum of the previous two terms. The first two terms of the
Fibonacci sequence are 0 followed by 1. Read an integer Value: N
from the user and print the Fibonacci series of ‘N’ terms
5 Write a C program to check if a given number is a Palindrome. 12
6 Write a C program to print pascal’s triangle based on number of 14
rows.
7 Consider student’s marks in Computer Test. Write a C Program to 16
display the grade obtained by a student in Computer Test based
on range of marks.
8 Write a C program to read the size of a one-dimensional array 18
from the user and read 10 elements into the array (positive
integers) and
1. print alternate numbers starting from index value 1.
2. Reverse the array elements and print the same.
9 In computer based applications, matrices play a vital role in the 21
projection of three dimensional image into a two dimensional
screen, creating the realistic seeming motions. Write a C program
using 2-dimensional array to check for compatibility of two
matrices and perform matrix Multiplication.
10 Write a C program to implement bubble sort with appropriate 26
input and output.
11 Write a C program to implement binary search with appropriate 30
input and output.
12 Write a C program to read 2 strings from the user and perform 34
the following operations
a. Count upper case, lower case and special characters in a string.
b. Compare 2 strings
c. Concatenation
d. Print all VOWEL and CONSONANT characters separately
13 Write a C program to define a structure named Student with 39
name and DOB, where, DOB in turn is a structure with day, month
and year. Using the concept of nested structures display your
name and date of birth.
1
1. Write a C program using switch statement to design a basic calculator
that performs the basic operations such as addition, subtraction,
multiplication, and division.
Algorithm
Read input:
- Prompt the user to enter the first number (num1).
- Prompt the user to enter the operation code (operator).
Program:
#include <stdio.h>
int main()
{
2
{
case '+':
result = num1 + num2;
break;
case '*':
result = num1 * num2;
printf("Product: %.2f\n", result);
break;
case '/':
// Check for division by zero
if (num2 != 0)
{
result = num1 / num2;
printf("Result: %.2f\n", result);
}
else
break;
default:
printf("Invalid operator\n");
break;
3
}
return 0;
}
Output:
Enter two integer numbers:
12
2
0
Enter the operation (+, -, *, /): /
Division by zero is not allowed.
4
2. Write a C program to find the coefficients of a quadratic equation and
compute its roots.
Algorithm:
Read coefficients from the user.
- Prompt the user to enter coefficients `a`, ‘b’ and ‘c’
Calculate discriminant.
- Calculate the discriminant using the formula ‘discriminant = b2- 4ac`.
Check discriminant.
- If `discriminant` is greater than 0:
- Calculate real and different roots using the quadratic formula:
root1 = -b / (2 * a);
- Display the roots.
- If `discriminant` is less than 0:
- Calculate complex and different roots using the quadratic formula:
realPart = -b / (2 * a);
Program:
#include <stdio.h>
#include <math.h>
int main()
{
5
float a, b, c, discriminant, root1, root2;
printf("Enter coefficients a, b, and c: ");
scanf("%f%f%f", &a, &b, &c);
discriminant = b * b - 4 * a * c;
if (discriminant > 0)
{
root1 = -b / (2 * a);
6
printf("Root 2 = %.2f - %.2fi\n", realPart, imaginaryPart);
}
return 0;
Output:
Root 1 = -0.27
Root 2 = -3.73
7
3. Write a C program to find GCD and LCM of 2 integers.
Algorithm
Read input:
Prompt the user to enter the first integer (num1), Second integer (num2).
Calculate the GCD:
Set a to the value of num1 and b to the value of num2.
Program:
#include <stdio.h>
int main()
{
int num1, num2, lcm, gcd;
printf("Enter two integer numbers: \n");
b = a % b;
a = temp;
}
gcd = a;
// Find LCM using GCD
lcm = (num1 * num2) / gcd;
8
// Display GCD and LCM
printf("GCD of %d and %d is: %d\n", num1, num2, gcd);
printf("LCM of %d and %d is: %d\n", num1, num2, lcm);
return 0;
}
Output:
Enter two integer numbers:
12
4
GCD of 12 and 4 is: 4
LCM of 12 and 4 is: 12
5
20
GCD of 5 and 20 is: 5
LCM of 5 and 20 is: 20
9
4. The Fibonacci sequence is a sequence where the next term is the sum of
the previous two terms. The first two terms of the Fibonacci sequence
are 0 followed by 1. Read an integer Value: N from the user and print the
Fibonacci series of ‘N’ terms
Algorithm:
Read the value of N from the user.
Initialize variables first and second to 0 and 1, respectively.
Print "Fibonacci Series of first N numbers:" where N is the user-input value.
Repeat N times:
Program:
#include <stdio.h>
int main()
{
int n, first = 0, second = 1, next;
10
// Print Fibonacci series
printf("Fibonacci Series of first %d numbers: \n", n);
first = second;
second = next;
}
return 0;
}
Output:
Enter the number of terms for Fibonacci series:
7
Fibonacci Series of first 7 numbers:
0, 1, 1, 2, 3, 5, 8,
11
5. Write a C program to check if a given number is a Palindrome.
Algorithm:
Read the input number from the user and store it in a variable (num).
Initialize variables originalNum to store the original number and reversedNum to store the
reversed number to 0.
Save the original number by setting originalNum equal to num.
Repeat until num is not equal to 0:
a. Calculate the remainder (remainder) when num is divided by 10.
b. Multiply reversedNum by 10 and add the remainder to it.
Program:
#include <stdio.h>
int main()
12
num /= 10;
}
return 0;
}
Output:
Enter an integer:
1234
Enter an integer:
1221
1221 is a palindrome.
13
6. Write a C program to print pascal’s triangle based on number of rows.
Algorithm:
Read number of rows
- Update the coefficient for the next iteration using the formula `coef = coef * (line - i) / i`.
Print a newline after each line to separate them.
Program:
#include <stdio.h>
int main ()
{
int n;
printf ("Enter number of rows: \n");
scanf ("%d", &n);
14
printf("\n");
}
}
Output:
15
7. Consider student’s marks in Computer Test. Write a C Program to display
the grade obtained by a student in Computer Test based on range of
marks.
Algorithm:
Accept the user's input for the grade.
Check the value of grade using a series of if-else statements:
If grade is equal to 10, print "Grade: O, Performance: Outstanding."
Else, if grade is greater than or equal to 9, print "Grade: A+, Performance: Excellent."
Else, if grade is greater than or equal to 8, print "Grade: A, Performance: Very Good."
Else, if grade is greater than or equal to 7, print "Grade: B+, Performance: Good."
Else, if grade is greater than or equal to 6, print "Grade: B, Performance: Above Average."
Else, if grade is greater than or equal to 5.5, print "Grade: C+, Performance: Average."
Program:
# include <stdio.h>
int main ()
{
float grade;
printf ("Enter your grade (0 to 10)\n");
scanf ("%f", &grade);
if (grade == 10)
printf ("Grade:O, Performance: Outstanding");
else if (grade >= 9)
printf ("Grade:A+, Performance: Excellent");
else if (grade >= 8)
16
printf ("Grade:B+, Performance: Good");
else if (grade >= 6)
printf ("Grade:B, Performance: Above Average");
17
8. Write a C program to read the size of a one-dimensional array from the
user and read 10 elements into the array (positive integers) and
1. print alternate numbers starting from index value 1.
2. Reverse the array elements and print the same.
Algorithm:
Read the size of the array from the user:
Ensure the size is at least 10:
Check if the entered size is less than 10.
If it is less than 10, print an error message and exit the program with an error code.
Program:
#include <stdio.h>
int main()
{
int size;
// Read the size of the array from the user
printf("Enter the size of the array: \n");
scanf("%d", &size);
// Ensure that the size is at least 10
if (size < 10)
{
18
printf("Please enter a size of at least 10.\n");
return 1; // Exit with an error code
}
int array[size];
// Read 10 elements into the array
printf("Enter 10 positive integers:\n");
for (int i = 0; i < 10; ++i)
scanf("%d", &array[i]);
printf("\n");
printf("\n");
19
5
Please enter a size of at least 10.
20
9. In computer based applications, matrices play a vital role in the
projection of three dimensional image into a two dimensional screen,
creating the realistic seeming motions. Write a C program using 2-
dimensional array to check for compatibility of two matrices and
perform matrix Multiplication.
Algorithm:
Input dimensions of the first matrix (matrix A):
Read the number of rows and columns for matrix A from the user.
Input dimensions of the second matrix (matrix B):
Read the number of rows and columns for matrix B from the user.
Program:
#include <stdio.h>
int main() {
int rowsA, colsA, rowsB, colsB;
scanf("%d", &colsA);
21
printf("Enter the number of rows for matrix B: ");
scanf("%d", &rowsB);
printf("Enter the number of columns for matrix B: ");
scanf("%d", &colsB);
int matrixA[rowsA][colsA];
int matrixB[rowsB][colsB];
int result[rowsA][colsB];
22
}
}
printf("\nMatrix A:\n");
for (int i = 0; i < rowsA; ++i) {
for (int j = 0; j < colsA; ++j) {
printf("%d\t", matrixA[i][j]);
}
printf("\n");
}
printf("\nMatrix B:\n");
for (int i = 0; i < rowsB; ++i) {
for (int j = 0; j < colsB; ++j) {
printf("%d\t", matrixB[i][j]);
}
printf("\n");
}
printf("\nResultant Matrix (A * B):\n");
for (int i = 0; i < rowsA; ++i) {
23
for (int j = 0; j < colsB; ++j) {
printf("%d\t", result[i][j]);
}
printf("\n");
}
return 0; // Exit successfully
}
Output:
Matrix A:
1 2
3 4
Matrix B:
5 6
24
7 8
19 22
43 50
25
10.Write a C program to implement bubble sort with appropriate input and
output.
Algorithm:
Input the size of the array.
Input elements into the array.
Use nested loops to iterate over the array elements and perform the bubble sort
algorithm:
Iterate over the array elements from the beginning to the second-to-last element.
Within the above loop, iterate over the array elements from the beginning to size - i - 2.
Swap adjacent elements if they are in the wrong order.
Display the sorted array:
Program:
#include <stdio.h>
// Function to perform bubble sort on an array
void bubbleSort(int arr[], int size)
{
arr[j + 1] = temp;
}
}
26
}
}
scanf("%d", &arr[i]);
}
{
printf("Sorted Array:\n");
for (int i = 0; i < size; ++i)
printf("%d ", arr[i]);
printf("\n");
int main()
{
int size;
int arr[size];
27
// Input elements into the array
inputArray(arr, size);
displayArray(arr, size);
32
11
56
54
87
76
65
Sorted Array:
11 12 21 32 43 54 56 65 76 87
28
67
65
54
43
32
Sorted Array:
32 43 54 65 67
29
11. Write a C program to implement binary search with appropriate input
and output.
Algorithm:
Input the size of the array (size).
Input the sorted array elements into the array.
If no, check if the key is smaller than the element at index mid.
If yes, update high = mid - 1 (ignore the right half).
If no, update low = mid + 1 (ignore the left half).
If the while loop exits, return -1 (element not found).
Program:
#include <stdio.h>
// Function to perform binary search
int binarySearch(int arr[], int low, int high, int key) {
while (low <= high) {
30
else if (arr[mid] > key)
high = mid - 1;
int main() {
int arr[size];
31
scanf("%d", &key);
else
printf("Element %d not found in the array\n", key);
return 0;
}
Output:
Enter the size of the array: 6
Enter the sorted array elements:
12
23
34
45
56
67
Enter the element to be searched: 56
12
23
34
32
45
56
Enter the element to be searched: 78
33
12.Write a C program to read 2 strings from the user and perform the
following operations
a. Count upper case, lower case and special characters in a string.
b. Compare 2 strings
c. Concatenation
d. Print all VOWEL and CONSONANT characters separately
Algorithm:
Input the first string (str1).
Input the second string (str2).
Call countCharacters with str1 and str2.
Call compareStrings with str1 and str2.
Call concatenateStrings with str1, str2, and result.
Print the concatenated string.
Call printVowelsConsonants with str1 and str2.
If the result is positive, print "String 1 is lexicographically greater than String 2."
Function concatenateStrings (str1, str2, result)
Copy the content of str1 to result using strcpy.
Concatenate the content of str2 to result using strcat.
34
Function printVowelsConsonants (str)
Iterate through each character in the string str.
If the character is a vowel (lowercase or uppercase), print it.
Program:
#include <stdio.h>
#include <string.h>
// Function to count upper case, lower case, and special characters in a string
void countCharacters(char str[]) {
int upper = 0, lower = 0, special = 0;
special++;
}
35
if (result == 0)
printf("Strings are equal.\n");
}
printf("\n");
printf("Consonants: ");
for (int i = 0; i < strlen(str); i++) {
if ((str[i] >= 'a' && str[i] <= 'z') || (str[i] >= 'A' && str[i] <= 'Z')) {
36
if (!(str[i] == 'a' || str[i] == 'e' || str[i] == 'i' || str[i] == 'o' || str[i] == 'u' ||
str[i] == 'A' || str[i] == 'E' || str[i] == 'I' || str[i] == 'O' || str[i] == 'U')) {
printf("%c ", str[i]);
}
}
}
printf("\n");
}
int main() {
char str1[100], str2[100], result[200];
// Perform operations
countCharacters(str1);
countCharacters(str2);
printf("\n");
compareStrings(str1, str2);
printf("\n");
37
printVowelsConsonants(str1);
printVowelsConsonants(str2);
return 0;
}
Output:
Enter the first string: REVA
Enter the second string: University
Uppercase characters: 4
Lowercase characters: 0
Special characters: 0
Uppercase characters: 1
Lowercase characters: 9
Special characters: 0
Vowels: E A
Consonants: R V
Vowels: U i e i
Consonants: n v r s t y
38
13.Write a C program to define a structure named Student with name and
DOB, where, DOB in turn is a structure with day, month and year. Using
the concept of nested structures display your name and date of birth.
Algorithm:
Define a structure DOB to represent the Date of Birth with three members: day, month,
and year.
Define another structure Student with two members: name (an array of characters) and
birthDate (an instance of the DOB structure).
Use fgets to read the name input from the user and store it in the name member of the
student structure.
Prompt the user to enter their date of birth (DD MM YYYY) using printf.
Use scanf to read the day, month, and year inputs from the user and store them in the
corresponding members of the birthDate structure within the student structure.
Program:
#include <stdio.h>
int day;
int month;
int year;
};
};
int main() {
39
// Declare a variable of type Student
struct Student student;
scanf("%d%d%d",&student.birthDate.day,&student.birthDate.month,
&student.birthDate.year);
return 0;
}
Output:
Enter your name: Praveen
40