0% found this document useful (0 votes)
14 views44 pages

C Programming Lab Report

This document is a lab report on C programming submitted to the Department of BICTE. It includes objectives, problem analysis, algorithms, source code, and outputs for various programming exercises, such as calculating the volume of a cube, checking for leap years, and determining student grades. The report demonstrates the practical application of C programming concepts and conditional statements.
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)
14 views44 pages

C Programming Lab Report

This document is a lab report on C programming submitted to the Department of BICTE. It includes objectives, problem analysis, algorithms, source code, and outputs for various programming exercises, such as calculating the volume of a cube, checking for leap years, and determining student grades. The report demonstrates the practical application of C programming concepts and conditional statements.
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
You are on page 1/ 44

JANAJYOTI MULTIPLE CAMPUS

Department of BICTE
Lalbandi, Sarlahi

A Lab Report on
C programming (ICT Ed. 416)

Submitted By:

Name: ……………………………………….

Roll No: ……………………………………….

Reg. No. ………………………………………

Submitted To:

Department of Bachelor of Information and Communication Technology Education


(BICTE)

Internal Examiner External Examiner


Department: BICTE Department:
Name: Shivahari Nepal Name:
Designation: HOD Designation:
Signature: Signature:
Submission Date:
Lab Question no. 1
A. Objective:
to calculate and display the volume of a CUBE having its height (h=10cm), width
(w=12cm) and depth (8cm).

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)

Input Processing Output Necessary header


Variables variables/calculations variables files/functions/macros
h(int) vol = h*w*d vol stdio.h
w(int) (int)
d(int)

C. Algorithm:
Step 1: Start

Step 2: Define variables: h(int), w(int), d(int), vol(int)

Step 3: Assign value to variables: h = 10, w=12, d=8

Step 4: Calculate the volume as: vol = h*w*d

Step 5: Display the volume (vol)

Step 6: Stop

D. Flowchart:

E. Source Code:

//Following code is written and compiled in


Code::Blocks IDE
#include<stdio.h>

int main(void)

//start the program

Int h,w,d,vol;

//variables declaration

h=10; w=12; d=8;

//assign value to variables

vol=h*w*d;

//calculation using mathematical formula

printf("The Volume of the cube is: %d",vol);

//display the volume

return 0;
//end the main program

F. Output (Compilation, Debugging & Testing)

The Volume of the cube is: 960


G. Discussion and Conclusion
The program is focused on the calculation of volume of a cube for the given height,
width and depth. From this lab, I understood the basic structure of C programming
including the meaning of header files & steps of problem solving. Hence, volume of a
cube is calculated and displayed.
Lab Assignment
Lab exercises One :
1. Write a program to display “hello world” in C.
2. Write a program to add two numbers (5&7) and display its sum.
3. Write a program to find the area, volume and perimeter of a rectangle.
4. Write a program to calculate area of a circle having its radius.
5. Write a program to calculate simple interest and compound interest.

Lab exercises Tow:


1. Write a program to declare two integers and one float variables then initialize
them to 10, 15, and 12.6. Also print the variable values in the screen.
2. Write a C program to prompt the user to input 3 integer values and print these
values in forward and reversed order.
3. Write a program to swap two variables values with and without using third variables.
4. Write a program to print the size of char, float, double and long double data types in C

Lab Exercises Three (If else):


1. Write a program to find the largest and smallest among three entered
numbers and also display whether the identified largest/smallest number is
even or odd.
Objective:
The objective of this lab experiment is to develop a C program that takes three input
numbers from the user and determines the largest and smallest among them.
Additionally, the program will determine whether the identified largest and
smallest numbers are even or odd.

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;

// Step 2: Prompt the user to enter three numbers


printf("Enter three numbers: ");
scanf("%d %d %d", &num1, &num2, &num3);

// Step 5: Identify the largest number


largest = (num1 > num2) ? (num1 > num3 ? num1 : num3) : (num2 > num3 ? num2 :
num3);

// Step 6: Identify the smallest number


smallest = (num1 < num2) ? (num1 < num3 ? num1 : num3) : (num2 < num3 ? num2
: num3);

// Step 7: Check if the largest number is even or odd


if (largest % 2 == 0)
printf("Largest number %d is even.\n", largest);
else
printf("Largest number %d is odd.\n", largest);

// Step 8: Check if the smallest number is even or odd


if (smallest % 2 == 0)
printf("Smallest number %d is even.\n", smallest);
else
printf("Smallest number %d is odd.\n", smallest);

return 0;
}
Output:

Enter three numbers: 7 12 5


Largest number 12 is even.
Smallest number 5 is odd.

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;

// Step 2: Prompt user for input


printf("Enter a single alphabet: ");
scanf(" %c", &alphabet);

// Step 4: Check for uppercase vowel


if (alphabet == 'A' || alphabet == 'E' || alphabet == 'I' || alphabet == 'O' || alphabet ==
'U') {
// Step 4 (cont.): Print result
printf("The input alphabet is a vowel.\n");
} else {
// Step 5: Check for lowercase vowel
switch(alphabet) {
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
// Step 5 (cont.): Print result
printf("The input alphabet is a vowel.\n");
break;
default:
// Step 5 (cont.): Print result
printf("The input alphabet is not a vowel.\n");
}
}

return 0;
}
Output:
Enter a single alphabet: A
The input alphabet is a vowel.

Enter a single alphabet: b


The input alphabet is not a vowel.
Conclusion:
The C program developed successfully determines whether the input alphabet
provided by the user is a vowel or not. It effectively utilizes both if-else and
switch statements to handle uppercase and lowercase letters, providing the desired
output. This experiment demonstrates the practical implementation of conditional
statements in C programming for character-based operations.
12. Write a program that asks a number and test the number whether it is
divisible by 7 but not by eleven.
Objective:
The objective of this lab experiment is to design and implement a C program that
prompts the user to input a number and then tests whether the number is divisible
by 7 but not by 11. The program should output a message indicating whether the
number satisfies the given conditions.

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;

// Prompt user to enter a number


printf("Enter a number: ");
scanf("%d", &num);

// Check if the number is divisible by 7 but not by 11


if (num % 7 == 0 && num % 11 != 0)
printf("The number is divisible by 7 but not by 11.\n");
else
printf("The number is either not divisible by 7 or divisible by 11.\n");

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;

// Step 2: User input for the year


printf("Enter a year: ");
scanf("%d", &year);

// Step 3: Check if divisible by 4


if (year % 4 == 0) {
// Step 4: Check if divisible by 100
if (year % 100 == 0) {
// Step 5: Check if divisible by 400
if (year % 400 == 0) {
printf("%d is a leap year.\n", year); // Leap year
} else {
printf("%d is not a leap year.\n", year); // Not a leap year
}
} else {
printf("%d is a leap year.\n", year); // Leap year
}
} else {
printf("%d is not a leap year.\n", year); // Not a leap year
}

return 0;
}
Output:

Enter a year: 2000


2000 is a leap year.
Conclusion:
The C program successfully checks whether the entered year is a leap year or not
according to the conditions specified (divisible by 4 and either divisible by 100
or divisible by 400). The algorithm employed efficiently handles various cases,
providing accurate results.

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;

// Prompt the user to enter a number


printf("Enter a number: ");

// Read the input number


scanf("%d", &num);

// Use switch-case to determine the sign of the number


switch(num > 0) {
case 1:
printf("Positive.\n");
break;
case 0:
switch(num < 0) {
case 1:
printf("Negative.\n");
break;
case 0:
printf("Zero.\n");
break;
}
break;
}
return 0;
}
Output:

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.

Lab Exercises Four(Loop):


1. Write a program to print 1 to 10 number.
Objective:
The objective of this lab experiment is to develop a C program to print numbers from 1 to 10
sequentially on the screen.

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;

// Loop to print numbers from 1 to 10


for (int i = 1; i <= 10; i++) {
// Print the current number
printf("%d\n", num);

// Increment the number


num++;
}

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++;
}

// Step 5: Display the sum


printf("The sum of numbers from 1 to 50 is: %d\n", sum);

return 0;
}
Output:

The sum of numbers from 1 to 50 is: 1275


Conclusion:
The C program successfully calculates and displays the sum of numbers from 1
to 50, which is 1275. This is achieved by utilizing a loop to iterate through each
number from 1 to 50 and accumulating the sum. The program demonstrates the
use of variables, looping constructs, and basic arithmetic operations in C
programming.

3. Write a Program to Check Whether a Number is Prime or not.


Objective:
The objective of this lab experiment is to design a C program that checks whether a
given input number is prime or not. The program will employ efficient
algorithms to determine the primality of the input number.

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:

1.Accept the input number from the user.


2.If the number is less than 2, it cannot be prime. Output "Not Prime" and exit.
3.Loop from 2 to the square root of the input number.
4.Check if the input number is divisible by any number in the loop.
5.If it is divisible, output "Not Prime" and exit.
6.If the loop completes without finding any divisors, output "Prime".
C Program:

#include <stdio.h>
#include <math.h>
int main() {
int num, i, isPrime = 1;

// Accepting input number


printf("Enter a number: ");
scanf("%d", &num);

// Checking if the number is less than 2


if (num < 2) {
printf("Not Prime\n");
return 0;
}

// Looping from 2 to the square root of the number


for (i = 2; i <= sqrt(num); i++) {
// Checking if the number is divisible by 'i'
if (num % i == 0) {
isPrime = 0; // Number is not prime
break;
}
}

// Outputting the result


if (isPrime)
printf("Prime\n");
else
printf("Not Prime\n");

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.

4. Write a program to find the factorial of a number.


Objective:
The objective of this lab experiment is to develop a C program that calculates the
factorial of a given integer. Factorial of a non-negative integer n, denoted by n!,
is the product of all positive integers less than or equal to n.

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;

printf("Enter a number: ");


scanf("%d", &num);

if (num < 0) {
printf("Error: Factorial is not defined for negative numbers.\n");
return 1; // Exit the program with an error status
}

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


factorial *= i;
}

printf("Factorial of %d is %d\n", num, factorial);

return 0; // Exit the program successfully


}
Output:

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.

5. Write a program to display the series: 1 6 11 16 .......... 101.


Objective:
The objective of this lab report is to develop a C program that displays the series 1, 6,
11, 16, ..., 101, where each term increments by 5.

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.

6. Write a program to display multiplication table of 6.


Objective:
The objective of this lab is to create a C program that generates and displays the
multiplication table of 6. This program aims to demonstrate the use of loops and
basic arithmetic operations in C programming.

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");

// Loop to generate and display the table


for (i = 1; i <= 10; i++) {
printf("6 * %d = %d\n", i, 6 * i);
}

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;

printf("Three-Digit Armstrong Numbers:\n");


for(num = 100; num <= 999; num++) {
temp = num;
sum = 0;

while(temp > 0) {
digit = temp % 10;
sum += (digit * digit * digit);
temp /= 10;
}

if(sum == num) {
printf("%d\n", num);
}
}
return 0;
}
Output:

Three-Digit Armstrong Numbers:


153
370
371
407
Conclusion:
In this lab experiment, we successfully implemented a C program to identify and
print all three-digit Armstrong numbers. The program iterates through all three-
digit numbers and checks each number against the Armstrong condition. The
output demonstrates that there are four three-digit Armstrong numbers: 153, 370,
371, and 407. This experiment illustrates the application of loops and conditional
statements in C programming to solve mathematical problems efficiently.

8. Write a program to check the given number is palindrome or not.


Objective:
The objective of this lab experiment is to develop a C program to determine whether a
given number is a palindrome or not. A palindrome number is one that remains
the same when its digits are reversed.

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;

printf("Enter a number: ");


scanf("%d", &num);

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.

Enter a number: 12345


The number is not a palindrome.
Conclusion:
The C program developed successfully determines whether a given number is a
palindrome or not. By utilizing a while loop to reverse the digits of the number
and then comparing it with the original number, the program accurately identifies
palindrome numbers. This experiment demonstrates the practical application of
loops and conditional statements in solving real-world problems.
9. Write a program to display character from A to Z by using for loop.
Objective:
The objective of this lab experiment is to develop a C program that utilizes a for loop
to display characters from 'A' to 'Z' sequentially.

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

printf("Characters from A to Z:\n");

// Loop to iterate through characters from 'A' to 'Z'


for(ch = 'A'; ch <= 'Z'; ch++) {
printf("%c ", ch); // Print 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:

GCD Algorithm (Euclidean Algorithm):


1.Start
2.Take two integers as input: num1 and num2
3.Repeat until num2 becomes 0:
4.Calculate remainder as num1 % num2
5.Set num1 = num2 and num2 = remainder
6.GCD will be the value of num1
7.End
LCM Algorithm:
1.Start
2.Take two integers as input: num1 and num2
3.Calculate the product of num1 and num2 and store it in 'product'
4.Calculate GCD using the Euclidean Algorithm (as described above)
5.LCM = (num1 * num2) / GCD
End
C Program:
#include <stdio.h>

// Function to find GCD using Euclidean Algorithm


int findGCD(int num1, int num2) {
int remainder;
while (num2 != 0) {
remainder = num1 % num2;
num1 = num2;
num2 = remainder;
}
return num1;
}

// Function to find LCM


int findLCM(int num1, int num2) {
int gcd = findGCD(num1, num2);
int lcm = (num1 * num2) / gcd;
return lcm;
}

int main() {
int num1, num2;
printf("Enter two numbers: ");
scanf("%d %d", &num1, &num2);

printf("GCD of %d and %d is %d\n", num1, num2, findGCD(num1, num2));


printf("LCM of %d and %d is %d\n", num1, num2, findLCM(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.

11. Write a program to generate the following Fibonacci Series.


1,1,2,3,5,8,………………25 terms
Objective:
The objective of this lab experiment is to develop a C program to generate the Fibonacci
series containing 25 terms.

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;

printf("Fibonacci Series up to %d terms:\n", terms);


printf("%d, %d, ", first, second);

for (int i = 3; i <= terms; i++) {


next = first + second;
printf("%d%s", next, (i == terms) ? "" : ", ");
first = second;
second = next;
}

return 0;
}
Output:

Fibonacci Series up to 25 terms:


1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765, 10946,
17711, 28657, 46368, 75025
Conclusion:
The C program successfully generates the Fibonacci series up to 25 terms. The algorithm
efficiently calculates each term of the series by adding the previous two terms. This
experiment illustrates the application of loops and variables in generating sequences,
demonstrating the practical implementation of mathematical concepts in programming.
12. Write a program to display the following:
a) * b) 1
** 1 2
*** 123
**** 1234
* 12345
**
***
****
*
*
*
*

Lab Exercises Five (Array and String):


1. Write a program to Read 1 to 10 number in to an array and print in reveres order
Objective:
The objective of this lab experiment is to develop a C program that reads ten integers into an
array and then prints them in reverse order.

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;

// Step 3: Read 10 integers into the array


printf("Enter 10 integers:\n");
for (i = 0; i < 10; i++) {
printf("Enter integer %d: ", i + 1);
scanf("%d", &arr[i]);
}

// Step 4: Print the original order of the array


printf("\nOriginal Order:\n");
for (i = 0; i < 10; i++) {
printf("%d ", arr[i]);
}
// Step 5: Print the elements of the array in reverse order
printf("\n\nReversed Order:\n");
for (i = 9; i >= 0; i--) {
printf("%d ", arr[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:

Prompt the user to input the number of persons (N).


Allocate memory dynamically for an array to store the ages.
Prompt the user to input the ages of N persons.
Calculate the minimum, maximum, and average age.
Display the results.
Algorithm:

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;

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


scanf("%d", &N);

ages = (int *)malloc(N * sizeof(int));

printf("Enter ages of %d persons:\n", N);


for (int i = 0; i < N; i++) {
scanf("%d", &ages[i]);
sum_age += ages[i];

if (i == 0 || ages[i] < min_age)


min_age = ages[i];

if (i == 0 || ages[i] > max_age)


max_age = ages[i];
}

avg_age = (float)sum_age / N;

printf("Minimum age: %d\n", min_age);


printf("Maximum age: %d\n", max_age);
printf("Average age: %.2f\n", avg_age);

free(ages);
return 0;
}
Output:

Enter the number of persons: 5


Enter ages of 5 persons:
25
35
40
20
30
Minimum age: 20
Maximum age: 40
Average age: 30.00
Conclusion:
The C program successfully reads the ages of N persons, calculates the minimum, maximum, and
average ages, and displays the results accurately. This program efficiently utilizes dynamic
memory allocation to accommodate varying numbers of persons.

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:

Input the ages of 20 students into an array.


Count the number of students whose age falls between 19 to 26.
Algorithm:

1. Declare an integer array ages of size 20 to store the ages of students.


2. Declare variables count and i for counting and loop iteration respectively, and initialize
count to zero.
3. Input the ages of 20 students using a loop.
4. Traverse the array and for each age, check if it falls between 19 to 26.
5. If the age falls within the specified range, increment the count variable.
6. Output the value of count as the result.
C Program:
#include <stdio.h>

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]);
}

// Count the number of students with age between 19 to 26


for (i = 0; i < 20; i++) {
if (ages[i] >= 19 && ages[i] <= 26) {
count++;
}
}

// Output the count


printf("Number of students with age between 19 to 26: %d\n", count);

return 0;
}
Output:

Enter the ages of 20 students:


Enter age of student 1: 21
Enter age of student 2: 18
Enter age of student 3: 25
Enter age of student 4: 20
Enter age of student 5: 27
Enter age of student 6: 22
Enter age of student 7: 19
Enter age of student 8: 23
Enter age of student 9: 24
Enter age of student 10: 26
Enter age of student 11: 20
Enter age of student 12: 18
Enter age of student 13: 25
Enter age of student 14: 20
Enter age of student 15: 21
Enter age of student 16: 22
Enter age of student 17: 19
Enter age of student 18: 18
Enter age of student 19: 27
Enter age of student 20: 28
Number of students with age between 19 to 26: 11
Conclusion:
The C program successfully inputs the ages of 20 students, counts the number of students whose
age falls between 19 to 26, and outputs the count. In the given example, the count is 11,
indicating that 11 students have ages between 19 to 26.
4. Write a program to accept an array of numbers and sort the ascending order.
Objective:

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>

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

int i, j, temp;

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

for (j = 0; j < n-i-1; j++) {

if (arr[j] > arr[j+1]) {

// Swap arr[j] and arr[j+1]


temp = arr[j];

arr[j] = arr[j+1];

arr[j+1] = temp;

int main() {

int n, i;

printf("Enter the size of the array: ");

scanf("%d", &n);

int arr[n];

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

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

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

bubbleSort(arr, n);

printf("Sorted array in ascending order:\n");

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

printf("%d ", arr[i]);

}
return 0;

Output:

Enter the size of the array: 5

Enter the elements of the array:

53719

Sorted array in ascending order:

13579

Conclusion:

In this lab experiment, we successfully implemented a C program to sort an array of numbers in


ascending order using the Bubble Sort algorithm. The program accepts the size and elements of
the array from the user, sorts the array, and displays the sorted array. Bubble Sort is a simple
sorting algorithm with a time complexity of O(n^2), which makes it suitable for small-sized
arrays.

5. Write a program to take names and marks of 50 students and print


them out in descending order of marks.
Objective:
The objective of this lab experiment is to develop a C program that takes the
names and marks of 50 students as input and prints them out in descending
order of marks. This exercise aims to demonstrate the implementation of
sorting algorithms and data manipulation techniques in the C programming
language.

Problem Analysis:
To achieve the objective, the following tasks need to be accomplished:

Input the names and marks of 50 students.


Implement a sorting algorithm to arrange the students' records in descending
order of marks.
Print the sorted list of students along with their corresponding marks.
Algorithm:

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

// Define a structure to hold student information


struct Student {
char name[MAX_NAME_LENGTH];
int marks;
};

// Function to perform bubble sort on array of students based on marks


void bubbleSort(struct Student arr[], int n) {
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (arr[j].marks < arr[j + 1].marks) {
// Swap elements if they are in the wrong order
struct Student temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}

int main() {
struct Student students[MAX_STUDENTS];
int i, n;

printf("Enter the number of students (max 50): ");


scanf("%d", &n);

// Input student names and marks


for (i = 0; i < n; i++) {
printf("Enter name of student %d: ", i + 1);
scanf("%s", students[i].name);
printf("Enter marks of student %d: ", i + 1);
scanf("%d", &students[i].marks);
}

// Sort the array of students in descending order of marks


bubbleSort(students, n);

// Print the sorted list of students along with their marks


printf("\nSorted List of Students (Descending Order of Marks):\n");
for (i = 0; i < n; i++) {
printf("%d. %s - %d\n", i + 1, students[i].name, students[i].marks);
}

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

Sorted List of Students (Descending Order of Marks):


1. Charlie - 90
2. Alice - 80
3. Bob - 70
Conclusion:
The C program successfully fulfills the objective of sorting the names and
marks of 50 students in descending order of marks. This was achieved by
implementing the bubble sort algorithm to arrange the student records based
on their marks. The program demonstrates the use of arrays, structures,
input/output operations, and sorting algorithms in C programming.
6. WAP to print the following matrices:

[ ]
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.

Lab Exercises Six (Structure):


1. Write a program to enter the students name roll no and marks using structure.
Objective:
The objective of this lab experiment is to create a program in C that utilizes structures to manage
student information, including their name, roll number, and marks. Using structures will enable
efficient organization and manipulation of data related to each student.

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>

// Define structure for student


struct student {
char name[50];
int rollNo;
float marks;
};

int main() {
int numStudents, i;

// Prompt user to enter number of students


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

// Declare array of structures to store student details


struct student students[numStudents];

// Input student details


for (i = 0; i < numStudents; i++) {
printf("\nEnter details for student %d:\n", i + 1);
printf("Name: ");
scanf("%s", students[i].name);
printf("Roll Number: ");
scanf("%d", &students[i].rollNo);
printf("Marks: ");
scanf("%f", &students[i].marks);
}

// Output student details


printf("\nStudent Details:\n");
for (i = 0; i < numStudents; i++) {
printf("Student %d\n", i + 1);
printf("Name: %s\n", students[i].name);
printf("Roll Number: %d\n", students[i].rollNo);
printf("Marks: %.2f\n", students[i].marks);
}

return 0;
}
Output:

Enter the number of students: 2

Enter details for student 1:


Name: John
Roll Number: 101
Marks: 85.5

Enter details for student 2:


Name: Alice
Roll Number: 102
Marks: 92.0
Student Details:
Student 1
Name: John
Roll Number: 101
Marks: 85.50
Student 2
Name: Alice
Roll Number: 102
Marks: 92.00
Conclusion:
Through this lab experiment, we successfully implemented a program in C to manage student
information using structures. This program allows users to input details for multiple students,
store them efficiently, and display them accurately. Structures prove to be an effective way to
organize related data elements within a program, enhancing code readability and maintainability.

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>

// Define a structure named 'company'


struct company {
char name[50];
char address[100];
char phone[15];
int numEmployees;
};

int main() {
// Declare a variable of type 'company'
struct company comp;

// Read input values for name, address, phone, and numEmployees


printf("Enter company name: ");
scanf("%s", comp.name);
printf("Enter company address: ");
scanf("%s", comp.address);

printf("Enter company phone number: ");


scanf("%s", comp.phone);

printf("Enter number of employees: ");


scanf("%d", &comp.numEmployees);

// Display the values of name, address, phone, and numEmployees


printf("\nCompany Details:\n");
printf("Name: %s\n", comp.name);
printf("Address: %s\n", comp.address);
printf("Phone: %s\n", comp.phone);
printf("Number of Employees: %d\n", comp.numEmployees);

return 0;
}
Output:

Enter company name: ABC Corporation


Enter company address: 123 Main Street, City
Enter company phone number: 123-456-7890
Enter number of employees: 50

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;

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


scanf("%d", &n);

struct Student students[n];

// Input data for each student


for(i = 0; i < n; i++) {
printf("\nEnter details for Student %d:\n", i + 1);
printf("Roll number: ");
scanf("%d", &students[i].rollNumber);
printf("Name: ");
scanf("%s", students[i].name);
printf("Address: ");
scanf("%s", students[i].address);
printf("Age: ");
scanf("%d", &students[i].age);
printf("Average marks: ");
scanf("%f", &students[i].averageMarks);
}

// Displaying collected information


printf("\nStudent Information:\n");
printf("Roll No\tName\tAddress\tAge\tAverage Marks\n");
for(i = 0; i < n; i++) {
printf("%d\t%s\t%s\t%d\t%.2f\n", students[i].rollNumber, students[i].name, students[i].address,
students[i].age, students[i].averageMarks);
}

return 0;
}
Output:

Enter the number of students: 2

Enter details for Student 1:


Roll number: 101
Name: John
Address: 123 Main St
Age: 20
Average marks: 85.5

Enter details for Student 2:


Roll number: 102
Name: Alice
Address: 456 Elm St
Age: 21
Average marks: 78.9

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.

Lab Exercises Seven (Pointer):


1. Write a program to add two values by using pointer.
Objective:
The objective of this lab experiment is to understand the concept of pointers in C programming and
implement a program to add two values using pointers.

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:

1. Begin by declaring three variables: num1, num2, and sum.


2. Prompt the user to enter the first value and store it in num1.
3. Prompt the user to enter the second value and store it in num2.
4. Declare two pointer variables: ptr1 and ptr2.
5. Assign the addresses of num1 and num2 to ptr1 and ptr2, respectively.
6. Dereference the pointers and add the values pointed to by ptr1 and ptr2, storing the result in
sum.
7. Print the sum.
C Code:
#include <stdio.h>

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;

// Addition using pointers


sum = *ptr1 + *ptr2;

// 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

2. Write a program to swap two values using pointer.


Objective:
The objective of this lab experiment is to understand the concept of pointers in C programming and
demonstrate the swapping of two integer values using pointers.

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:

1. Declare three integer variables a, b, and temp.


2. Initialize variables a and b with the values to be swapped.
3. Declare two integer pointers ptr_a and ptr_b and assign the addresses of a and b respectively.
4. Use pointer arithmetic to perform the swap:
5. Assign the value pointed by ptr_a to temp.
6. Assign the value pointed by ptr_b to ptr_a.
7. Assign the value of temp to the value pointed by ptr_b.
8. Display the swapped values of a and b.
9. End of the algorithm.
C Code:

#include <stdio.h>

void swap(int *ptr_a, int *ptr_b) {


int temp = *ptr_a;
*ptr_a = *ptr_b;
*ptr_b = temp;
}

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.

3. Write a program to show pointer arithmetic.


4. Write a program to show array and structure with pointer.
5. Write a program to Dynamic Memory Allocation (malloc, calloc, realloc and free).

Lab Exercises Eight (Function):


1. Write a program to add, subtract, multiply and divide two integers using user
defined type function with return type.
2. Write a program to find the square of any number using the function.
3. Write a program to call by reference and call by value in function.
4. Write a program to use arguments of main function to pass arguments from
command line interface

Lab Exercises Nine (File Handling):


1. Write a Program to create a file named test.txt and write some text “I study BICTE” to the file.
2. Write program to write and read roll, name and percentage of 5 students to / from data file using
fprintf() and fscanf().
3. Write characters into a file “filec.txt”. The set of characters are read form the keyboard until an
enterkey is pressed (use putc() and getc() function).
4. Read characters form file “filec.txt” created in question 8. Also count the number of characters in
the file (use fputs() and fgets() function).
5. Write name, age and height of a person into a data file “person.txt” and read it (use fprintf() and
fscanf() function)
6. Write a program to write and read a record to /from a data file using fwrite() and fread() function.
7. Write a program to rename and remove user create data file.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy