C Programming Lab Report
C Programming Lab Report
Department of BICTE
Lalbandi, Sarlahi
A Lab Report on
C programming (ICT Ed. 416)
Submitted By:
Name: ……………………………………….
Submitted To:
B. Problem Analysis:
The problem is to calculate the volume of a CUBE having its inputs parameters
identified as:Height (integer type), width (integer type) and depth (integer type) . The
output of the program is to display the volume; hence the output parameter is identified
as vol (integer type). During the processing or calculation phase, we don’t need any extra
parameters (variables) for this problem.
The volume of the cube is the multiplication of its height, width and depth, hence the
mathematical formula to calculate volume is:
vol = height* width* depth. (vol = h*w*d)
C. Algorithm:
Step 1: Start
Step 6: Stop
D. Flowchart:
E. Source Code:
int main(void)
Int h,w,d,vol;
//variables declaration
vol=h*w*d;
return 0;
//end the main program
Problem Analysis:
To achieve the objective, we need to devise a plan to compare the three input numbers
and identify the largest and smallest among them. We will utilize conditional
statements to make these comparisons. We will also include logic to determine
whether the identified numbers are even or odd.
Algorithm:
2. Start
3. Prompt the user to enter three numbers (num1, num2, num3).
4. Read the three numbers from the user.
5. Initialize variables largest and smallest to store the largest and smallest
numbers.
6. Compare num1, num2, and num3 to identify the largest number and store it in
largest.
7. Compare num1, num2, and num3 to identify the smallest number and store it in
smallest.
8. Check if largest is even or odd and print the result.
9. Check if smallest is even or odd and print the result.
10. End
C Program:
#include <stdio.h>
int main() {
int num1, num2, num3;
int largest, smallest;
return 0;
}
Output:
This program takes three numbers as input from the user, identifies the largest
and smallest among them, and determines whether they are even or odd.
11. Write a program to check whether input alphabet is vowel or not using if else
and switch statement.
Objective:
The objective of this lab experiment is to create a C program that can determine
whether a given input alphabet is a vowel or not using both if-else and switch
statements.
Problem Analysis:
In this experiment, we are tasked with creating a program that accepts a single alphabet
input from the user and checks whether it is a vowel or not. A vowel is one of the
letters A, E, I, O, or U, regardless of case. The program should handle both
uppercase and lowercase letters.
Algorithm:
1.Start
2.Prompt the user to enter a single alphabet.
3.Read the input character.
4.Check if the input character is an uppercase vowel using if-else statement:
5.If it is 'A', 'E', 'I', 'O', or 'U', print "The input alphabet is a vowel."
6.Else, proceed to the next step.
7.Check if the input character is a lowercase vowel using switch statement:
8.If it is 'a', 'e', 'i', 'o', or 'u', print "The input alphabet is a vowel."
9.Else, print "The input alphabet is not a vowel."
10. End
C Program:
#include <stdio.h>
int main() {
char alphabet;
return 0;
}
Output:
Enter a single alphabet: A
The input alphabet is a vowel.
Problem Analysis:
To accomplish the objective, we need to devise an algorithm that efficiently tests the
divisibility of a number by 7 and 11. The algorithm should check if the number is
divisible by 7 and not by 11 simultaneously. We'll use the modulo operator (%) to
check for divisibility.
Algorithm:
1.Start
2.Prompt the user to enter a number and store it in a variable (num).
3.Check if num is divisible by 7 and not divisible by 11 using the following
conditions:
4.if (num % 7 == 0 && num % 11 != 0), print "The number is divisible by 7 but not
by 11."
5.else, print "The number is either not divisible by 7 or divisible by 11."
6.End
C Program:
#include <stdio.h>
int main() {
int num;
return 0;
}
Output:
Enter a number: 77
The number is either not divisible by 7 or divisible by 11.
Enter a number: 77
The number is divisible by 7 but not by 11.
Conclusion:
In this lab experiment, we successfully designed and implemented a C program to
test whether a user-entered number is divisible by 7 but not by 11. The program
utilizes the modulo operator to perform the divisibility test and provides
appropriate output messages based on the test results. This program can be useful
in various applications where such divisibility tests are required.
13. Write a program to check whether the entered year is leap year or not (a
year is leap if it is divisible by 4 and divisible by 100 or 400.)
Objective:
The objective of this lab experiment is to design a C program that checks whether a
given year is a leap year or not. A leap year is defined as a year divisible by 4
and either divisible by 100 or divisible by 400.
Problem Analysis:
To solve this problem, we need to take a user-input year and determine whether it
satisfies the conditions of a leap year. The main challenge lies in devising an
algorithm that efficiently checks these conditions and outputs whether the year is
a leap year or not.
Algorithm:
1.Start.
2.Take user input for the year.
3.Check if the entered year is divisible by 4.
4.If not, it's not a leap year. Exit.
5.If divisible by 4, check if it's divisible by 100.
6.If not, it's a leap year. Output accordingly. Exit.
7.If divisible by 100, check if it's divisible by 400.
8.If yes, it's a leap year. Output accordingly. Exit.
9.If no, it's not a leap year. Output accordingly. Exit.
10. End.
C Program:
#include <stdio.h>
int main() {
int year;
return 0;
}
Output:
14. Write a program to compute grade of students using if else adder. The grades are
assigned as followed:
Marks Grade
Marks < 50 F
50<= marks<60 B-
60<= marks<70 B
70<= marks<80 B+
80<= marks<90 A-
90<= marks<100 A
Objective:
The objective of this lab is to develop a C program that calculates the grade of a
student based on their marks using an if-else ladder.
Problem Analysis:
To compute the grade of a student, we need to take input of marks obtained and
then determine the corresponding grade based on the grading criteria provided. We'll use
an if-else ladder to efficiently evaluate the marks and assign the appropriate grade.
Algorithm:
1. Start
2. Input the marks obtained by the student.
3. Check the marks using an if-else ladder:
4. If marks < 50, grade = F
5. Else if marks < 60, grade = B-
6. Else if marks < 70, grade = B
7. Else if marks < 80, grade = B+
8. Else if marks < 90, grade = A-
9. Else if marks < 100, grade = A
10. Else, invalid marks
11. Output the grade.
12. End
C Program:
#include <stdio.h>
int main() {
float marks;
char grade;
// Input marks
printf("Enter marks obtained: ");
scanf("%f", &marks);
// Grade calculation
if (marks < 50)
grade = 'F';
else if (marks < 60)
grade = 'B';
else if (marks < 70)
grade = 'B';
else if (marks < 80)
grade = 'B';
else if (marks < 90)
grade = 'A';
else if (marks < 100)
grade = 'A';
else
grade = 'X'; // Invalid marks
// Output grade
if (grade == 'X')
printf("Invalid marks entered.\n");
else
printf("Grade: %c\n", grade);
return 0;
}
Output:
Enter marks obtained: 85
Grade: A
Conclusion:
The C program successfully computes the grade of a student based on the marks
obtained. It utilizes an if-else ladder to efficiently determine the grade according to the
specified grading criteria.
15. Write a program to check whether a number is positive, negative or zero
using switch case
Objective:
The objective of this lab experiment is to develop a C program that determines whether
a given number is positive, negative, or zero using the switch-case construct.
Problem Analysis:
In this lab, we aim to create a C program that takes an input number from the user and
checks whether it is positive, negative, or zero. We will use the switch-case
statement to achieve this. The program should prompt the user to enter a number,
read the input, and then perform the necessary comparisons to determine its sign.
Algorithm:
1.Start
2.Display a message prompting the user to enter a number.
3.Read the input number.
4.Use a switch-case statement to:
5.Case 1: If the number is greater than 0, print "Positive."
6.Case 2: If the number is less than 0, print "Negative."
7.Case 3: If the number is 0, print "Zero."
8.Default: If none of the above conditions are met, print an error message.
9.End.
C Program:
#include <stdio.h>
int main() {
// Declare variable to store the input number
int num;
Enter a number: 5
Positive.
Enter a number: -8
Negative.
Enter a number: 0
Zero.
Conclusion:
The C program successfully accomplishes the task of determining whether a
given number is positive, negative, or zero using the switch-case construct. It
prompts the user to input a number, reads the input, and then uses nested switch-
case statements to determine the sign of the number and print the corresponding
message. The program is efficient and provides accurate results for various input
values.
Problem Analysis:
In this experiment, we need to write a C program that will sequentially print numbers from 1
to 10. The program should be designed to loop through the numbers and print each number on
a new line.
Algorithm:
1.
Start
2.
Initialize a variable num to store the current number, set it to 1.
3.
Start a loop that will iterate from 1 to 10.
4.
Inside the loop:
a. Print the current value of num.
b. Increment the value of num by 1.
5. End loop
6. End
C Program:
#include <stdio.h>
int main() {
// Initialize the variable to store the current number
int num = 1;
return 0;
}
Output:
1
2
3
4
5
6
7
8
9
10
Conclusion:
In this lab experiment, we successfully developed a C program to print numbers from 1 to 10.
The program utilizes a loop to iterate through the numbers and prints each number on a new
line. This experiment demonstrates the basic usage of loops and printing in C programming.
2. Write a program to calculate and display sum of the numbers from 1 to 50.
Objective:
The objective of this lab experiment is to develop a C program that calculates and
displays the sum of all numbers from 1 to 50.
Problem Analysis:
To achieve the objective, we need to iterate from 1 to 50 and accumulate the sum of
these numbers. This requires understanding looping constructs in C
programming and the concept of variable initialization and updating.
Algorithm:
1.Start
2.Initialize variables: sum to 0, num to 1.
3.Start a loop to iterate from num = 1 to num = 50.
a. Add the current value of num to sum.
b. Increment num by 1.
4.End loop
5.Display the value of sum.
6.End
C Program:
#include <stdio.h>
int main() {
// Step 2: Initialize variables
int sum = 0;
int num = 1;
// Step 3: Loop to iterate from 1 to 50
while (num <= 50) {
// Step 3a: Add current value of num to sum
sum += num;
// Step 3b: Increment num
num++;
}
return 0;
}
Output:
Problem Analysis:
A prime number is a natural number greater than 1 that has no positive divisors other
than 1 and itself. Therefore, to determine whether a number is prime or not, we
need to check if it is divisible by any integer other than 1 and itself. We can
optimize this process by only checking divisibility up to the square root of the
number, as any factor beyond the square root would already have been covered
by its counterpart less than the square root.
Algorithm:
#include <stdio.h>
#include <math.h>
int main() {
int num, i, isPrime = 1;
return 0;
}
Output:
Enter a number: 17
Prime
Conclusion:
The C program developed successfully determines whether a given number is
prime or not. It effectively utilizes the optimized algorithm that checks
divisibility up to the square root of the input number, thereby enhancing
efficiency. This experiment demonstrates the application of fundamental
mathematical concepts in programming to solve real-world problems efficiently.
Problem Analysis:
To solve this problem, we need to understand the concept of factorial. Factorial of a
number is calculated recursively by multiplying the number with the factorial of
its preceding integer until the number becomes 1.
Algorithm:
1.Start
2.Declare variables num (to store the input number), factorial (to store the
factorial), and i (loop variable).
3.Prompt the user to enter a number and store it in num.
4.Initialize factorial to 1.
5.If num is less than 0, print an error message and terminate.
6.Otherwise, iterate from 1 to num using a loop:
7.Multiply factorial by the loop variable i.
8.Print the value of factorial as the result.
9.End
C Program:
#include <stdio.h>
int main() {
int num, factorial = 1, i;
if (num < 0) {
printf("Error: Factorial is not defined for negative numbers.\n");
return 1; // Exit the program with an error status
}
Enter a number: 5
Factorial of 5 is 120
Conclusion:
The C program successfully calculates the factorial of a given number. It
prompts the user to input a number, checks if it's non-negative, and then
calculates the factorial using a loop. This program demonstrates the use of loops
and basic arithmetic operations in C programming to solve mathematical
problems.
Problem Analysis:
We need to generate and display the series mentioned in the objective. The series starts
from 1 and each subsequent term is obtained by adding 5 to the previous term
until we reach 101. We can solve this problem using a loop construct.
Algorithm:
1.Start
2.Initialize a variable term to 1.
3.Begin a loop that iterates until term is less than or equal to 101.
4.Inside the loop, print the value of term.
5.Increment term by 5.
6.End loop
7.End
C Program:
#include <stdio.h>
int main() {
int term = 1;
printf("Series: ");
while (term <= 101) {
printf("%d ", term);
term += 5;
}
return 0;
}
Output:
Series: 1 6 11 16 21 26 31 36 41 46 51 56 61 66 71 76 81 86 91 96 101
Conclusion:
In this lab, we have successfully developed a C program to display the series 1,
6, 11, 16, ..., 101. We achieved this by using a loop construct to generate the
terms of the series, incrementing by 5 until reaching 101. The program executed
as expected, producing the desired output.
Problem Analysis:
To generate the multiplication table of 6, we need to iterate through numbers from 1 to
10 and multiply each number by 6 to get the corresponding multiplication result.
The program should display these results in a formatted table.
Algorithm:
1.Start
2.Initialize a variable i to 1.
3.Loop while i is less than or equal to 10.
a. Calculate the product of 6 and i.
b. Display the multiplication expression and the result.
c. Increment i by 1.
4.End
C Program:
#include <stdio.h>
int main() {
// Loop variable
int i;
// Display header
printf("Multiplication Table of 6\n");
printf("-------------------------\n");
return 0;
}
Output:
Multiplication Table of 6
-------------------------
6*1=6
6 * 2 = 12
6 * 3 = 18
6 * 4 = 24
6 * 5 = 30
6 * 6 = 36
6 * 7 = 42
6 * 8 = 48
6 * 9 = 54
6 * 10 = 60
Conclusion:
The C program successfully generates and displays the multiplication table of 6.
It demonstrates the use of loops to iterate through numbers and basic arithmetic
operations to calculate the multiplication results. This program can be extended
to generate multiplication tables of other numbers by modifying the multiplier
value and loop conditions accordingly.
7. Write a program to all three digit Armstrong number.
Objective:
The objective of this lab experiment is to develop a C program that identifies and
prints all three-digit Armstrong numbers. Armstrong numbers, also known as
narcissistic numbers, are numbers that are equal to the sum of their own digits
raised to the power of the number of digits.
Problem Analysis:
To identify three-digit Armstrong numbers, we need to iterate through all three-digit
numbers (from 100 to 999) and check if each number satisfies the Armstrong
condition. The Armstrong condition involves summing the cubes of each digit
for a three-digit number and checking if the sum equals the original number.
Algorithm:
1.Start
2.Initialize variables num, digit, sum, temp.
3.Iterate num from 100 to 999.
4.Within the loop:
a. Set temp equal to num.
b. Initialize sum to 0.
c. While temp is greater than 0:
5.Extract the last digit of temp and store it in digit.
6.ii. Add digit cubed to sum.
7.iii. Divide temp by 10 to remove the last digit.
8.d. If sum equals num, print num.
9.End
C Program:
#include<stdio.h>
int main() {
int num, digit, sum, temp;
while(temp > 0) {
digit = temp % 10;
sum += (digit * digit * digit);
temp /= 10;
}
if(sum == num) {
printf("%d\n", num);
}
}
return 0;
}
Output:
Problem Analysis:
To solve this problem, we need to devise an algorithm that can reverse the digits of the
given number and then compare it with the original number to check if they are
equal. If they are equal, then the number is a palindrome; otherwise, it is not.
Algorithm:
1.Start
2.Accept the number input from the user.
3.Initialize variables num, reverse, and temp.
4.Set reverse and temp to 0.
5.Store the original number in temp.
6.Using a while loop, reverse the digits of the number:
7.Extract the last digit of the number using the modulus operator and add it to
reverse.
8.Multiply reverse by 10.
9.Divide the number by 10 to remove the last digit.
10. Repeat until the number becomes 0.
11. Check if temp is equal to reverse:
12. If yes, print "The number is a palindrome."
13. If no, print "The number is not a palindrome."
14. End
C Program:
#include <stdio.h>
int main() {
int num, reverse = 0, temp;
temp = num;
while (temp != 0) {
reverse = reverse * 10 + temp % 10;
temp = temp / 10;
}
if (num == reverse)
printf("The number is a palindrome.\n");
else
printf("The number is not a palindrome.\n");
return 0;
}
Output:
Enter a number: 12321
The number is a palindrome.
Problem Analysis:
The problem requires iterating through the characters from 'A' to 'Z' and printing them
on the screen. This can be achieved using a for loop that starts from 'A' and
iterates until 'Z'.
Algorithm:
1.Start
2.Initialize a loop counter variable 'char' with the value of 'A'.
3.Start a for loop with 'char' initialized to 'A' and terminating when 'char' is less
than or equal to 'Z'.
4.Inside the loop:
a. Print the value of 'char'.
b. Increment the value of 'char' by 1.
5.End the loop.
6.End
C Program:
#include <stdio.h>
int main() {
char ch; // Variable to hold the character
return 0;
}
Output:
Characters from A to Z:
ABCDEFGHIJKLMNOPQRSTUVWXYZ
Conclusion:
In this lab experiment, we successfully developed a C program to display
characters from 'A' to 'Z' using a for loop. The program iterates through the
characters sequentially and prints them on the screen. This exercise demonstrates
the usage of loops and character manipulation in C programming.
10. Write a program to find GCD (greatest common divisor or HCF) and LCM
(least common multiple) of two numbers.
Objective:
The objective of this lab experiment is to develop a C program to find the Greatest
Common Divisor (GCD) and Least Common Multiple (LCM) of two given
numbers using appropriate algorithms.
Problem Analysis:
The problem requires the implementation of algorithms to calculate the GCD and
LCM of two integers. The GCD is the largest positive integer that divides both
numbers without leaving a remainder, while the LCM is the smallest positive
integer that is divisible by both numbers without leaving a remainder.
Algorithm:
int main() {
int num1, num2;
printf("Enter two numbers: ");
scanf("%d %d", &num1, &num2);
return 0;
}
Output:
Enter two numbers: 24 36
GCD of 24 and 36 is 12
LCM of 24 and 36 is 72
Conclusion:
The C program successfully finds the Greatest Common Divisor (GCD) and
Least Common Multiple (LCM) of two given numbers using the Euclidean
Algorithm for GCD calculation. The program employs a straightforward
approach and provides accurate results for various input values, thus meeting the
objectives of the experiment.
Problem Analysis:
The Fibonacci series is a sequence of numbers in which each number is the sum of the two
preceding ones, usually starting with 0 and 1. In this case, we are starting with 1,1 and
generating the series up to the 25th term. The challenge is to devise an efficient algorithm to
calculate and display these terms.
Algorithm:
1. Start
2. Initialize three variables: first, second, and next, all set to 1 initially.
3. Print the first two terms of the Fibonacci series: 1, 1.
4. Loop from 3 to 25:
a. Calculate the next term as the sum of the previous two terms.
b. Print the next term.
c. Update first with the value of second.
d. Update second with the value of next.
5. End
C Program
#include <stdio.h>
int main() {
int first = 1, second = 1, next, terms = 25;
return 0;
}
Output:
Problem Analysis:
The problem requires us to read ten integers into an array and then print them in reverse order.
This necessitates understanding of basic array handling and looping constructs in C
programming. We need to design an algorithm that can efficiently reverse the elements of the
array without using any built-in functions for reversing.
Algorithm:
1. Start
2. Declare an array of size 10 to hold the integers.
3. Iterate from 0 to 9 and prompt the user to enter each integer, storing them in the array.
4. Print a message indicating the original order of the array.
5. Iterate from 9 to 0 and print each element of the array in reverse order.
6. End
C Program:
#include <stdio.h>
int main() {
// Step 2: Declare an array of size 10
int arr[10];
int i;
return 0;
}
Output:
Enter 10 integers:
Enter integer 1: 5
Enter integer 2: 3
Enter integer 3: 8
Enter integer 4: 2
Enter integer 5: 7
Enter integer 6: 1
Enter integer 7: 9
Enter integer 8: 4
Enter integer 9: 6
Enter integer 10: 0
Original Order:
5382719460
Reversed Order:
0649172835
Conclusion:
The C program developed successfully reads ten integers into an array and prints them in reverse
order. This lab experiment has provided practical experience in array handling and looping
constructs in C programming, reinforcing the concepts of array traversal and manipulation.
2. Write a program to read N number of person age in an array and print minimum,
maximum and average age.
Objective:
The objective of this lab experiment is to design a C program that reads the ages of N persons
into an array and then calculates and displays the minimum age, maximum age, and average age
of the group.
Problem Analysis:
To achieve the objective, we need to:
1. Start
2. Declare variables: N (number of persons), *ages (pointer to dynamically allocated
memory for ages array), min_age, max_age, sum_age, avg_age.
3. Prompt the user to enter N.
4. Allocate memory for ages array of size N.
5. Prompt the user to input ages of N persons and store them in the ages array.
6. Initialize min_age and max_age to the first age in the array.
7. Iterate through the ages array to find the minimum and maximum ages.
8. Calculate the sum of all ages.
9. Calculate the average age.
10. Display the minimum, maximum, and average ages.
11. Free the dynamically allocated memory.
12. End.
C Program:
#include <stdio.h>
#include <stdlib.h>
int main() {
int N, *ages, min_age, max_age, sum_age = 0;
float avg_age;
avg_age = (float)sum_age / N;
free(ages);
return 0;
}
Output:
3. Write a program to input the age of 20 students and count the number of students having
age in between 19 to 26.
Objective:
The objective of this lab report is to create a C program that inputs the ages of 20 students into an
array and then counts the number of students whose age falls between 19 to 26.
Problem Analysis:
We are required to develop a C program that performs the following tasks:
int main() {
int ages[20];
int count = 0;
int i;
// Input ages of 20 students
printf("Enter the ages of 20 students:\n");
for (i = 0; i < 20; i++) {
printf("Enter age of student %d: ", i + 1);
scanf("%d", &ages[i]);
}
return 0;
}
Output:
The objective of this lab experiment is to implement a C program that accepts an array of
numbers from the user and sorts them in ascending order using a sorting algorithm.
Problem Analysis:
Given an array of numbers, we need to arrange them in ascending order. To achieve this, we will
employ a sorting algorithm. One of the most commonly used sorting algorithms is the Bubble
Sort algorithm, which compares adjacent elements and swaps them if they are in the wrong order.
This process is repeated until the entire array is sorted.
Algorithm:
1. Start
2. Declare an array to store the numbers.
3. Accept the size of the array from the user.
4. Accept the elements of the array from the user.
5. Implement the Bubble Sort Algorithm:
6. Iterate through the array from the first element to the second last element.
7. For each iteration, iterate through the array from the first element to the element before
the last sorted element.
8. Compare adjacent elements, if they are in the wrong order, swap them.
9. Repeat this process until the entire array is sorted.
10. Display the sorted array.
11. End
C Program:
#include <stdio.h>
int i, j, temp;
arr[j] = arr[j+1];
arr[j+1] = temp;
int main() {
int n, i;
scanf("%d", &n);
int arr[n];
scanf("%d", &arr[i]);
bubbleSort(arr, n);
}
return 0;
Output:
53719
13579
Conclusion:
Problem Analysis:
To achieve the objective, the following tasks need to be accomplished:
1. Start
2. Declare an array of structures to store the names and marks of 50
students.
3. Input the names and marks of the students.
4. Implement a sorting algorithm (e.g., bubble sort) to sort the array of
structures based on the marks in descending order.
5. Print the sorted list of students along with their corresponding marks.
6. End
C Program:
#include <stdio.h>
#include <string.h>
#define MAX_STUDENTS 50
#define MAX_NAME_LENGTH 50
int main() {
struct Student students[MAX_STUDENTS];
int i, n;
return 0;
}
Output:
Enter the number of students (max 50): 3
Enter name of student 1: Alice
Enter marks of student 1: 80
Enter name of student 2: Bob
Enter marks of student 2: 70
Enter name of student 3: Charlie
Enter marks of student 3: 90
[ ]
1 0 1
1 1 1
1 0 1
7. Write a program to input data in two dimensional array for example 3 x
3 matrix and display in Matrix in matrix form.
Objective:
The objective of this lab experiment is to develop a C program that allows
users to input data into a two-dimensional array, specifically a 3x3 matrix,
and display it in matrix form.
Problem Analysis:
In this lab experiment, we are dealing with a two-dimensional array, which
represents a matrix. The challenge lies in accepting user input for each
element of the matrix and then displaying the matrix in a readable format.
We need to ensure that the program handles the input properly, displays the
matrix accurately, and maintains the correct formatting.
Algorithm:
1. Start
2. Define a 3x3 matrix array.
3. Iterate through each row and column of the matrix:
a. Prompt the user to enter the value for the current element.
b. Read the user input and store it in the respective position of
the matrix.
4. Display the matrix:
a. Iterate through each row and column of the matrix.
b. Print the element at the current position.
c. If it's the last element of the row, print a new line to move to
the next row.
5. End
C Program:
c
Copy code
#include <stdio.h>
#define ROWS 3
#define COLS 3
int main() {
int matrix[ROWS][COLS];
int i, j;
// Input phase
printf("Enter the elements of the matrix:\n");
for (i = 0; i < ROWS; i++) {
for (j = 0; j < COLS; j++) {
printf("Enter element at position (%d,%d): ", i + 1, j + 1);
scanf("%d", &matrix[i][j]);
}
}
// Output phase
printf("\nMatrix:\n");
for (i = 0; i < ROWS; i++) {
for (j = 0; j < COLS; j++) {
printf("%d\t", matrix[i][j]);
}
printf("\n");
}
return 0;
}
Output:
mathematica
Copy code
Enter the elements of the matrix:
Enter element at position (1,1): 1
Enter element at position (1,2): 2
Enter element at position (1,3): 3
Enter element at position (2,1): 4
Enter element at position (2,2): 5
Enter element at position (2,3): 6
Enter element at position (3,1): 7
Enter element at position (3,2): 8
Enter element at position (3,3): 9
Matrix:
123
456
789
Conclusion:
The C program developed in this lab experiment successfully allows users to
input data into a 3x3 matrix and displays the matrix in a readable format.
This program demonstrates the fundamental concepts of handling two-
dimensional arrays in C programming.
8. Write a program to add two 2 x 2 matrices.
9. Write a program to multiply any two 3 x 3 matrices.
10. Write a program to read and print string using puts() and gets().
11. Write a program to by using all string handling functions.
12. Write a program to input given set of string and sort them in alphabetical order.
Problem Analysis:
In this lab experiment, we need to develop a program that can handle the following tasks:
Accepting input for student details such as name, roll number, and marks.
Storing this information for each student.
Displaying the entered information for verification.
To accomplish this, we will use a structure to represent each student, containing members for name,
roll number, and marks. Then, we will prompt the user to enter the details for each student, store
them in an array of structures, and finally display the entered information.
Algorithm:
1. Define a structure named student with members for name, roll number, and marks.
2. Declare an array of student structures to hold multiple student records.
3. Prompt the user to enter the number of students.
4. Use a loop to iterate over each student:
a. Prompt the user to enter the name, roll number, and marks for each student.
b. Store the entered information in the respective members of the current student structure.
5. Display the details of all students entered.
C Program:
#include <stdio.h>
int main() {
int numStudents, i;
return 0;
}
Output:
2. Create a structure named company which has name, address, phone and no of Employee as
member variables. Read name of company, its address, phone and no of Employee. Finally
display these members’ value.
Objective:
The objective of this lab experiment is to understand the concept of structures in C programming and
implement a structure named 'company' that contains various attributes such as name, address,
phone number, and number of employees. The program aims to demonstrate the usage of
structures for organizing related data and accessing its members.
Problem Analysis:
In this lab, we are required to create a structure named 'company' with four member variables: name,
address, phone, and number of employees. We need to read input values for these members from
the user and then display them on the output screen.
Algorithm:
1. Define a structure named 'company' with member variables: name, address, phone, and
numEmployees.
2. Declare a variable of type 'company' to store the information of a company.
3. Read input values for name, address, phone, and numEmployees from the user.
4. Display the values of name, address, phone, and numEmployees on the output screen.
C Program:
#include <stdio.h>
int main() {
// Declare a variable of type 'company'
struct company comp;
return 0;
}
Output:
Company Details:
Name: ABC Corporation
Address: 123 Main Street, City
Phone: 123-456-7890
Number of Employees: 50
Conclusion:
In this lab experiment, we successfully implemented a structure named 'company' in C
programming language. We learned how to define a structure, declare variables of that structure
type, read input values for structure members, and display the structure members' values on the
output screen. Structures provide a convenient way to organize related data into a single unit,
making the code more readable and maintainable.
3. Write a program to read Roll no, Name, Address, Age & average-marks of n students in
the BICTE first semester class and display them.
Objective:
The objective of this lab experiment is to develop a C program that reads the Roll number, Name,
Address, Age, and Average marks of 'n' students in the BICTE first semester class and displays
this information.
Problem Analysis:
In this lab experiment, we are required to create a C program that can handle the following tasks:
Accept input data including Roll number, Name, Address, Age, and Average marks for 'n' students.
Display the collected information for all the students.
To accomplish this, we need to design an algorithm that reads input data from the user and then
displays it in a formatted manner.
Algorithm:
1. Start
2. Declare necessary variables such as Roll number, Name, Address, Age, Average marks, and
loop counters.
3. Prompt the user to enter the number of students (n).
4. Using a loop, ask the user to input Roll number, Name, Address, Age, and Average marks for
each student and store them in respective variables.
5. Display the collected information in a formatted manner for each student.
6. End
C Program:
#include <stdio.h>
struct Student {
int rollNumber;
char name[50];
char address[100];
int age;
float averageMarks;
};
int main() {
int n, i;
return 0;
}
Output:
Student Information:
Roll No Name Address Age Average Marks
101 John 123 Main St 20 85.50
102 Alice 456 Elm St 21 78.90
Conclusion:
In this lab experiment, we have successfully developed a C program that reads and displays the
Roll number, Name, Address, Age, and Average marks of 'n' students in the BICTE first semester
class. The program efficiently collects the data and presents it in a formatted manner, facilitating
easy understanding and analysis of student information.
4. WAP to enter the students name, date of birth and sort the student basis of name.
5. Write a program to enter the students name roll no and marks using union.
Problem Analysis:
In this experiment, we are tasked with adding two values using pointers in C programming. Pointers
are variables that store memory addresses as their values. By utilizing pointers, we can directly
manipulate the memory locations where variables are stored, allowing for efficient memory
management and access.
Algorithm:
int main() {
int num1, num2, sum;
int *ptr1, *ptr2;
// Input
printf("Enter first number: ");
scanf("%d", &num1);
printf("Enter second number: ");
scanf("%d", &num2);
// Pointers initialization
ptr1 = &num1;
ptr2 = &num2;
// Output
printf("Sum of %d and %d is %d\n", *ptr1, *ptr2, sum);
return 0;
}
Output:
Enter first number: 5
Enter second number: 7
Sum of 5 and 7 is 12
Conclusion:
In this lab experiment, we successfully implemented a C program to add two values using
pointers. By utilizing pointers, we accessed the memory addresses of the variables directly,
which allowed us to efficiently manipulate their values. This experiment enhanced our
understanding of pointers and their role in C programming, emphasizing their significance in
memory management and manipulation tasks
Problem Analysis:
In this experiment, we are tasked with swapping the values of two integer variables using pointers.
Traditional swapping involves the use of a temporary variable. However, in this experiment, we
will utilize pointers to directly swap the values stored in the memory addresses of the variables.
Algorithm:
#include <stdio.h>
int main() {
int a = 5, b = 10;
int *ptr_a = &a, *ptr_b = &b;
printf("Before swapping:\n");
printf("a = %d, b = %d\n", a, b);
swap(ptr_a, ptr_b);
printf("After swapping:\n");
printf("a = %d, b = %d\n", a, b);
return 0;
}
Output:
Before swapping:
a = 5, b = 10
After swapping:
a = 10, b = 5
Conclusion:
The experiment successfully demonstrated the swapping of two integer values using pointers in
C programming. By manipulating memory addresses directly, the need for a temporary variable
was eliminated, resulting in an efficient swapping algorithm. Pointers provide a powerful
mechanism for accessing and manipulating memory, offering flexibility and optimization in
program implementation.