EX NO: 1 Write a program using I/O statements and expressions.
Date:
a. C Program to display the values using Data types
Aim:
Algorithm:
4
Program:
#include <stdio.h> int main() { int
ivar; float fvar; char svar[100];
// Input integer value
printf("\nEnter the integer value: ");
scanf("%d", &ivar); // Input float
value printf("\nEnter the float
value: "); scanf("%f", &fvar);
// Input string value printf("\nEnter
the string value: "); scanf("%s",
svar); // Output the entered values
printf("\n"); printf("\nInteger value
is: %d", ivar); printf("\nFloat
value is: %f", fvar);
printf("\nEntered string is: %s\n",
svar); return 0;
5
Output:
Result:
6
b. C program to evaluate area of triangle with 3 sides given.
Date:
Aim:
Algorithm:
7
Program:
/*To evaluate area of triangle (sqrt(s(s-a)(s-b)(s-c)*/
#include<stdio.h>
#include<math.h> int
main()
{
int a, b, c;
float s, area;
// Input values of the sides of the triangle
printf("Enter the values of a, b, c: ");
scanf("%d%d%d", &a, &b, &c); //
Calculate the semi-perimeter s = (a + b +
c) / 2.0;
// Calculate the area using Heron's
formula area = sqrt(s * (s - a) * (s - b) * (s
- c)); // Output the area of the triangle
printf("The area of the triangle is = %f\n", area);
return 0;
}
Output:
Result:
8
EX NO: 2. Programs using decision-making constructs.
Date:
a) C program to check positive negative or zero using simple if statement
Aim:
Algorithm:
9
Program:
#include <stdio.h>
int main()
{ int
num;
// Input number from user
printf("Enter any number: ");
scanf("%d", &num);
// Check if the number is positive, negative, or zero
if(num > 0)
printf("Number is POSITIVE\n");
else if(num < 0)
printf("Number is NEGATIVE\n");
else
printf("Number is ZERO\n");
return 0;
10
Output:
Result:
11
b). C program to check greatest among two numbers using if else statement.
Date:
Aim:
Algorithm:
12
Program:
#include <stdio.h>
int main()
{ int x,
y;
// Input the value of x
printf("Enter the value of x:\n");
scanf("%d", &x);
// Input the value of y
printf("Enter the value of y:\n");
scanf("%d", &y); // Compare
the values of x and y
if (x > y) { printf("x
is Greater\n");
} else if (x < y) {
printf("y is Greater\n");
else
{ printf("x and y are
Equal\n");
return 0;
13
Output:
Result:
14
c). C program to check greatest among three numbers using Nested if... else statement.
Date:
Aim:
Algorithm:
15
Program:
#include<stdio.h>
int main() { int a, b, c;
// Input 3 numbers from the user
printf("Enter 3 numbers: ");
scanf("%d%d%d", &a, &b, &c);
// Compare the numbers and determine the greatest
if(a > b) { if(a > c)
{ printf("a is the
greatest\n");
} else {
printf("c is the greatest\n");
} } else { if(b
> c) { printf("b is the
greatest\n");
} else {
printf("c is the greatest\n");
} return
0; }
16
Output:
Result:
17
d). C program to check a given number is divisible by both 5 and 8 using elseif ladder
statement.
Date:
Aim:
Algorithm:
18
Program:
#include <stdio.h>
int main()
int a;
// Input a number from the user
printf("Enter a number: ");
scanf("%d", &a);
// Check divisibility by both 5 and 8, or by 5 or 8 individually
if(a % 5 == 0 && a % 8 == 0)
printf("Divisible by both 5 and 8\n");
else if(a % 8 == 0)
{ printf("Divisible by
8\n");
else if(a % 5 == 0)
{ printf("Divisible by
5\n");
else
19
{ printf("Divisible by
none\n");
return 0;
Output:
Result:
20
EX NO 3. Write a program to find whether the given year is a leap year or Not?
Date: (Hint: not every centurion year is a leap year. For example, 1700, 1800
and 1900 is not a leap year)
Aim:
Algorithm:
21
Program:
#include <stdio.h> int
main()
{ int
year;
// Input the year from the user
printf("Enter the Year (YYYY): ");
scanf("%d", &year);
// Check if the year is a leap year
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
printf("\nThe Given year %d is a Leap Year\n", year);
else
printf("\nThe Given year %d is Not a Leap Year\n", year);
return 0;
22
Output:
Result:
23
EX NO: 4 Write a program to perform the Calculator operations, namely,
Date: addition, subtraction, multiplication, division and square of a number
Aim:
Algorithm:
24
Program
#include <stdio.h> int main() { int
choice; float num1, num2, result;
printf("Calculator Operations:\n");
printf("1. Addition\n"); printf("2.
Subtraction\n"); printf("3.
Multiplication\n"); printf("4.
Division\n"); printf("5. Square of a
Number\n"); printf("Enter your
choice: "); scanf("%d", &choice);
switch(choice) { case 1: //
Addition printf("Enter two
numbers: "); scanf("%f %f",
&num1, &num2); result =
num1 + num2; printf("Result:
%.2f\n", result); break;
case 2: // Subtraction
printf("Enter two numbers: ");
scanf("%f %f", &num1, &num2);
result = num1 - num2;
printf("Result: %.2f\n", result);
break; case 3: // Multiplication
printf("Enter two numbers: ");
scanf("%f %f", &num1, &num2);
result = num1 * num2;
printf("Result: %.2f\n", result);
break; case 4: // Division
25
printf("Enter two numbers: ");
scanf("%f %f", &num1, &num2);
if (num2 != 0) { result =
num1 / num2;
printf("Result: %.2f\n", result);
} else { printf("Error: Division by zero is
not allowed.\n");
}
break;
case 5: //
Square of a
Number
printf("Enter a
number: ");
scanf("%f",
&num1);
result = num1
* num1;
printf("Square:
%.2f\n",
result);
break;
default:
printf("Invalid choice.\n");
}
return 0;
}
26
Output:
Result:
27
EX NO 5. Check whether a given number is Armstrong number or not
Date:
Aim:
Algorithm:
28
Program:
#include<stdio.h>
int main() {
int num, originalNum, remainder, result = 0;
// Input a three-digit integer
printf("Enter a three-digit integer:
"); scanf("%d", &num);
// Check if the input is a three-digit
number if (num < 100 || num > 999) {
printf("Please enter a valid three-digit
integer.\n"); return 1; // Exit with an error code
}
originalNum = num;
// Calculate the Armstrong number
while (originalNum != 0)
{
// remainder contains the last digit remainder = originalNum % 10; result += remainder *
remainder * remainder;
// Removing last digit from the original number
originalNum /= 10;
}
// Check if the original number is equal to the calculated
result if (result == num)
printf("%d is an Armstrong number.\n", num);
else
printf("%d is not an Armstrong number.\n", num);
return 0;
}
29
Output:
Result:
30
EX NO 6. Check whether a given number is odd or even
Date:
Aim:
Algorithm:
31
Program:
#include <stdio.h>
int main()
{
int num;
// Input prompt for the user printf("\nOdd
or Even Number\n"); printf("\nEnter an
integer you want to check: "); scanf("%d",
&num);
// Check if the number is even or
odd if ((num % 2) == 0)
{
printf("%d is an Even number\n", num);
}
else
{
printf("
%d is an
Odd
number\
n",
num);
return 0;
}
32
Output:
Result:
33
EX NO 7. Write a program to find the factorial of a give number.
Date:
Aim:
Algorithm:
34
Program:
#include <stdio.h>
int main() {
int i, num;
long fact = 1; // Corrected type from longint to long
// Input prompt for the user printf("ENTER A NUMBER: ");
scanf("%d", &num);
// Calculate factorial using a for
loop for (i = 1; i <= num; i++)
{
fact *= i;
}
// Output the result
printf("THE FACTORIAL OF %d IS %ld\n", num, fact);
return 0;
35
Output:
Result:
36
EX NO 8. Write a C program to find out the average of 4 integers.
Date:
Aim:
Algorithm:
37
Program:
#include <stdio.h>
int main()
{
int avg = 0, sum = 0, x = 0, num[4];
// Input numbers from the
user for (x = 0; x < 4; x++)
{
printf("Enter number %d: ", (x + 1));
scanf("%d", &num[x]);
}
// Calculate the sum
for (x = 0; x < 4; x++)
{
sum += num[x];
}
// Calculate the average
avg = sum / 4;
// Output the average
printf("Average of entered numbers is: %d\n", avg);
return 0;
}
38
Output:
Result:
39
EX NO 9. Write a program to print half pyramid of *.
Date:
Aim:
Algorithm:
40
Program:
#include <stdio.h>
int main() {
int n, i, j;
// Read the number of rows for the pyramid
printf("Enter the number of rows: ");
scanf("%d", &n);
// Loop through each
row for (i = 1; i <= n;
i++) {
// Print i asterisks for the current row for (j = 1; j <= i; j++) { printf("* ");
}
// Move to the next line after printing a row
printf("\n");
}
return 0;
}
41
Output:
Result:
42
EX NO 10. Write a program to display array elements using two dimensional arrays.
Date:
Aim:
Algorithm:
43
Program:
#include <stdio.h>
int main() {
int disp[2][3]; // Declare a 2D array with 2 rows and 3
columns int i, j;
// Input values for the
array for (i = 0; i < 2; i++) {
for (j = 0; j < 3; j++) {
printf("Enter value for disp[%d][%d]: ", i, j);
scanf("%d", &disp[i][j]);
}
}
// Display the elements of the 2D array printf("Two Dimensional array elements:\n");
for (i = 0; i < 2; i++) { for (j = 0; j <
3; j++) {
printf("%d ", disp[i][j]); // Print element
}
printf("\n"); // New line after each row
}
return 0;
}
44
Output:
Result:
45
EX NO 11. Write a c program to perform swapping using a function.
Date:
Aim:
Algorithm:
46
Program:
#include <stdio.h>
#include <conio.h> // Note: conio.h is specific to some compilers; you may omit it if not
needed.
void swap(int *p, int *q);
void main()
{ int a, b;
// Prompt user for input
printf("Enter any two
numbers:\n");
scanf("%d %d", &a, &b); // Corrected the format
string printf("\nBefore swapping: a=%d, b=%d", a,
b);
// Call the swap function
swap(&a, &b);
printf("\nAfter swapping: a=%d, b=%d", a, b);
getch(); // Note: Only needed if you're using conio.h
}
void swap(int *p, int *q)
{ int tmp; tmp = *p;
*p = *q;
*q = tmp;
}
47
Output:
Result:
48
EX NO 12. Write a C program to display all prime numbers between two
Date: intervals using functions.
Aim:
Algorithm:
49
Program:
#include <stdio.h>
#include <conio.h> // Optional, remove if not needed
// Function prototype int
checkPrimeNumber(int
n);
int main() {
in
t n1, n2, i, flag;
// Clear the screen (optional)
clrscr(); // Note: clrscr() is not standard, may not work on all compilers
printf("Enter two positive integers:\n");
scanf("%d %d", &n1, &n2); // Corrected the format string
printf("Prime numbers between %d and %d are:\n", n1,
n2); for (i = n1 + 1; i < n2; ++i) {
// flag will be equal to 1 if i is
prime flag =
checkPrimeNumber(i); if (flag
== 1) { printf("%d\n", i);
}
}
return 0;
}
// User-defined function to check if a number is
prime int checkPrimeNumber(int n) {
if (n <= 1) return 0; // Numbers less than or equal to 1 are not prime
fo
50
r (int j = 2; j * j <= n; j++) { // Check for factors up to the square root
of n
if (n % j == 0) {
return 0; // Not a prime number
}
}
return 1; // Prime number
}
int checkPrimeNumber(int n)
{ int j,flag=1;
for(j=2;j<n/2;++j
if(n%j==0)
flag=0
break;
} } return
flag;
51
Output :
Result:
52
EX NO 13. Write a C program to solve towers of Hanoi using recursion.
Date:
Aim:
Algorithm:
53
Program:
#include <stdio.h>
// Function to perform the Towers of Hanoi
void TowersOfHanoi(int n, char source, char destination, char
auxiliary) { if (n == 1) { printf("Move disk 1 from %c to %c\n",
source, destination);
return;
TowersOfHanoi(n - 1, source, auxiliary, destination);
printf("Move disk %d from %c to %c\n", n, source,
destination);
TowersOfHanoi(n - 1, auxiliary, destination, source);
}
// Main function int main() { int n;
// Number of disks printf("Enter
the number of disks: ");
scanf("%d", &n);
// Call the function
TowersOfHanoi(n, 'A', 'C', 'B'); // A, B and C are names of
rods return 0;
}
Explanation:
If n is 1:
● Move disk from source to destination.
54
Else:
● Call TowersOfHanoi(n-1, source, auxiliary, destination) to move n-1 disks from
source to auxiliary.
● Move the nth disk from source to destination.
● Call TowersOfHanoi(n-1, auxiliary, destination, source) to move n-1 disks from
auxiliary to destination.
55
Output :
Result :
56
EX NO 14. Write a program in C to get the largest element of an array using the function.
Date:
Aim:
Algorithm:
57
Program:
#include<stdio.>
#define MAX
100
int findMaxElem(int arr[], int n); // Function prototype
int main() { int
arr[MAX], mxelem, n, i;
printf("\nFunction: Get the largest element of an array:\n");
printf("\nInput the number of elements to be stored in the array:
"); scanf("%d", &n);
if (n <= 0 || n > MAX) { printf("Please enter a valid number
of elements (1 to %d).\n", MAX); return 1; // Exit if input is
invalid
}
printf("Input %d elements in the
array:\n", n); for (i = 0; i < n; i++) {
printf("Element - %d: ", i);
scanf("%d
", &arr[i]);
}
mxelem = findMaxElem(arr, n); printf("The
largest element in the array is: %d\n\n", mxelem);
return 0;
}
int findMaxElem(int arr[], int n) { int i = 1, mxelem = arr[0];
// Initialize mxelem with the first element
58
while (i < n) { if (mxelem < arr[i]) { mxelem = arr[i];
// Update mxelem if current element is larger
}
i++;
}
return mxelem;
}
Output:
Result:
59
EX NO 15. Write a C program to concatenate two strings
Date:
Aim:
Algorithm:
60
Program:
#include <stdio.h>
int main() {
char S1[100] = "Programming "; // Use double quotes for strings
char S2[] = "is awesome"; // Use double quotes for strings
int length = 0, j;
// Calculate the length of
S1 while (S1[length] != '\0')
{
++length;
}
// Concatenate S2 to S1
for (j = 0; S2[j] != '\0'; ++j, ++length) {
S1[length] = S2[j];
}
// Null-terminate the concatenated string
S1[length] = '\0';
printf("After concatenation: ");
puts(S1);
return 0;
}
Output:
Result:
61
EX NO 16. Write a C program to find the length of String
Date:
Aim:
Algorithm:
62
Program:
#include <stdio.h>
int main() { // Fixed the function
declaration char s[1000]; int c =0;
printf("Input a string: "); fgets(s, sizeof(s), stdin); //
Use fgets instead of gets for safety
// Remove the newline character added by fgets if present
while (s[c] != '\0' && s[c] != '\n') {
c++;
}
printf("Length of the string: %d\n",
c); return 0;
Output:
Result:
63
EX NO 17. write a program to find the frequency of a character in a string
Date:
Aim:
Algorithm:
64
Program:
#include <stdio.h>
int main() { // Fixed the function
declaration char str[1000], ch; int
i, count = 0;
printf("Enter a string: ");
fgets(str, sizeof(str), stdin);
printf("Enter a character to find its frequency: ");
scanf(" %c", &ch); // Added a space before %c to consume any leftover newline
// Count the frequency of the
character for (i = 0; str[i] != '\0';
++i) {
if (ch == str[i]) {
++count;
}
}
printf("Frequency of '%c' = %d\n", ch, count);
return 0;
}
Output:
Result:
65
EX NO 18. Write a C program to Store Student Information in Structure and Display it
Date:
Aim:
Algorithm:
66
Program:
#include <stdio.h>
struct student { char firstName[50]; // Added space
between 'char' and 'firstName'
int roll; int marks;
} s[10];
int main() {
int i;
printf("Enter information of students:\n");
for (i = 0; i < 5; ++i) { s[i].roll = i +
1; printf("\nFor roll number %d,\n",
s[i].roll);
printf("Enter first name: "); scanf("%s",
s[i].firstName); printf("Enter marks:
"); scanf("%d", &s[i].marks);
}
printf("Displaying
Information:\n\n"); for (i = 0; i < 5; ++i)
{ printf("\nRoll number:
%d\n", s[i].roll);
printf("First name: ");
puts(s[i].firstName);
printf("Marks: %d\n", s[i].marks);
}
return 0;
67
Output:
Enter information of students:
For roll number 1,
Enter first name:
John Enter marks:
85
For roll number 2,
Enter first name:
Alice Enter marks:
90
For roll number 3,
Enter first name:
Bob Enter marks:
78
For roll number 4,
Enter first name:
Sarah Enter marks:
88
For roll number 5,
Enter first name:
David Enter marks:
95
68
Displaying Information:
Roll number: 1
First name:
John Marks: 85
Roll number: 2
First name:
Alice Marks:
90
Roll number: 3
First name: Bob
Marks: 78
Roll number: 4
First name: Sarah
Marks: 88
Roll number: 5
First name: David
Marks: 95
Result:
69
EX NO 19. The annual examination is conducted for 10 students for five subjects.
Date: Write a program to read the data and determine the following:
(a) Total marks obtained by each student.
(b) The highest marks in each subject and the mark of the student who secured it.
(c) The student who obtained the highest total marks.
Aim:
Algorithm:
70
Program:
#include<stdio.h>
#define SIZE 50
struct student
{ char
name[30]; int
rollno;
int sub[3];
};
int main() { int i,
j, max, ni, n; struct
student st[SIZE];
printf("Enter how many students (max %d): ",
SIZE); scanf("%d", &n);
// Ensure n does not exceed SIZE if (n > SIZE) { printf("Number
of students exceeds the maximum allowed (%d).\n", SIZE);
return 1;
}
// Read the names and roll numbers for (i = 0; i < n; i++)
{ printf("\nEnter name and roll number for
student%d:",i+1);
scanf("%s", st[i].name);
scanf("%d", &st[i].rollno);
}
// Read marks for each subject for (i = 0; i < n; i++) {
for (j = 0; j < 3; j++) {
71
printf("\nEnter marks of student %s for subject %d:",st[i].name,j+1)
scanf("%d", &st[i].sub[j]);
}
}
// Calculate total marks for each
student for (i = 0; i < n; i++) {
int total = 0;
for (j = 0; j < 3; j++) { total
+= st[i].sub[j];
}
printf("\nTotal marks obtained by student %s are %d", st[i].name, total);
}
// Find and display the student with the highest marks in each
subject for (j = 0; j < 3; j++) { max = 0; for (i = 0; i <
n; i++) { if (max < st[i].sub[j]) { max =
st[i].sub[j]; ni = i;
}
}
printf("\nStudent %s got maximum marks = %d in Subject %d", st[ni].name, max, j + 1);
}
// Find the student with the highest total marks
int highestTotal = 0;
for (i = 0; i < n; i++) {
int total = 0;
for (j = 0; j < 3; j++) {
total += st[i].sub[j];
}
if (highestTotal <total) {
highestTotal =total;
72
ni = i;
}
}
printf("\n%s obtained the total highest marks.", st[ni].name);
return 0;
}
Output:
Enter how many students (max 50): 2
Enter name and roll number for student
1:John1
Enter marks of student John for subject 1: 80
Enter marks of student John for subject 2:90
Enter marks of student John for subject 3:85
Enter name and roll number for student
2:Alice 2
Enter marks of student Alice for subject 1: 88
Enter marks of student Alice for subject 2:79
Enter marks of student Alice for subject 3: 92
Total marks obtained by student John are 255
Total marks obtained by student Alice are 259
Student Alice got maximum marks = 88 in
Subject 1
Student John got maximum marks = 90 in
Subject 2
73
Student Alice got maximum marks = 92 in
Subject 3
Alice obtained the total highest marks.
Result:
74
EX NO 20. Write a program to demonstrate file operations (e.g. count the
Date: number of characters, words and lines in a file, replace a specific
word with the given word in the same file).
Aim:
Algorithm
75
Program:
#include<stdio.>
#include<stdlib.>
#include<string.>
#include<ctype.>
#define MAX_BUFFER 1024
void countFileContent(const char *filename, int *charCount, int *wordCount, int
*lineCount) { FILE *file = fopen(filename, "r");
if (!file) {
perror("Unabl
e to open file"); return;
}
int c, prevChar = ' ';
*charCount = *wordCount = *lineCount = 0;
while ((c = fgetc(file)) != EOF) {
(*charCount)++;
if (c =='\n') {
(*lineCount)++;
}
if (isspace(c)) { if
(!isspace(prevChar)) {
(*wordCount)++;
}
}
prevChar = c;
}
76
// Count the last word if the file does not end with a
newline if (!isspace(prevChar)) {
(*wordCount)++;
fclose(file);
void replaceWordInFile(const char *filename, const char *oldWord, const char
*newWord) { FILE *file = fopen(filename, "r");
if (!file)
perror("Unabl
e to open file"); return;
}
Char
buffer[MAX_BUFFER];
char*result = NULL;
size_t resultSize = 0;
while (fgets(buffer, MAX_BUFFER, file)) {
char *pos; while ((pos = strstr(buffer, oldWord)) != NULL) {
size_t lenBefore = pos - buffer;
size_t lenAfter = strlen(pos + strlen(oldWord)); result =
77
realloc(result, resultSize + lenBefore + strlen(newWord) + lenAfter + 1);
if (!result) {
perror("Unabl
e to allocate memory"); fclose(file);
return;
memcpy(result + resultSize, buffer, lenBefore); strcpy(result + resultSize
+ lenBefore, newWord); strcpy(result + resultSize + lenBefore +
strlen(newWord), pos + strlen(oldWord)); resultSize += lenBefore
+ strlen(newWord); strcpy(buffer, result + resultSize - lenAfter);
}
result = realloc(result, resultSize + strlen(buffer) +
1); strcpy(result + resultSize, buffer); resultSize
+= strlen(buffer);
}
fclose(file);
file = fopen(filename, "w");
if (!file) { perror("Unable to open
file for writing");
free(result); return;
fwrite(result, sizeof(char),
78
resultSize, file); free(result);
fclose(file);
int main() { const char *filename
= "[Link]"; int charCount,
wordCount, lineCount;
countFileContent(filename, &charCount, &wordCount, &lineCount);
printf("Characters: %d\nWords: %d\nLines: %d\n", charCount, wordCount,
lineCount);
const char *oldWord = "old"; const char *newWord =
"new"; replaceWordInFile(filename, oldWord, newWord);
printf("Replaced '%s' with '%s' in the file.\n", oldWord,
newWord);
return 0;
Output:
Assuming [Link] contains:
Hello world
This is a test file.
old value is here.
79
Running the program would produce:
Characters: 39
Words: 10
Lines: 3
Replaced 'old' with 'new' in the file.
After running the program, the contents of [Link] would be:
Hello world
This is a test file.
new value is here.
Result:
80