Student Manual 24es203 C Lab
Student Manual 24es203 C Lab
DATE:
AIM:
To find average of 4 integers
ALGORITHM:
Step1.Start
Step2.Declare variables
Step4.Calculate avg=sum/n
Step6.Stop
PROGRAM:
#include<stdio.h>
void main()
{
int i,n=4,sum=0,nu[100];
float avg;
printf("\nEnter numbers\n");
for(i=1;i<5;i++)
{
scanf("%d",&nu[i]);
sum=sum + nu[i];
}
avg=sum/n;
printf("\nAverage is : %2f\n",avg);
}
OUTPUT:
2 4 2 4
RESULT
Thus the Cprogram to find the average of 4numbers has been executed and verified.
Program:
#include <stdio.h>
int main()
{
int a,b;
printf("Enter value of a: \n");
scanf("%d",&a);
b=a%2==0?1:0;
if (b==0)
printf("The number is Odd");
else
printf("The number is Even");
return 0;
}
Output:
Enter value of a: 32
The number is even
AIM:
TowriteaCProgramtofindwhetherthegiven yearisleapyearornot.
ALGORITHM:
Step1:Start
Step2:Takeintegervariableyear
Step3:Checkifyearisdivisibleby400thenDISPLAY"isaleapyear"
Step4:Checkifyearis notdivisibleby100ANDdivisibleby4thenDISPLAY"isaleapyear"
Step5:Otherwise,DISPLAY"isnotaleapyear"Step
6:Stop
PROGRAM:
#include<stdio.h>
void main()
{
int year;
printf("Enter a year :\n");
scanf("%d",&year);
if((year% 400)==0)
{
printf("%disaleapyear\n",year);
}
else if((year%100)!=0&&(year%4)==0)
{
printf("%dis a leap year\n",year);
}
else
{
printf("%dis not a leapyear \n",year);
}
}
OUTPUT:
Enter a year:
2000
2000 is a leap year
Enter a year:
1900
1900 is not a leap year
RESULT:
Thus the C Program to find whether the given year is leap year or not has been
executed successfully and the output was verified.
1(d). Biggest among given three integers using nested if-else
Program:
#include<stdio.h>
void main()
{
int a, b, c;
printf("Enter three numbers\n");
scanf("%d %d %d", &a, &b, &c);
if(a > b)
{
if(a > c)
{
printf("a: %d is largest\n", a);
}
}
else if(b > c)
{
printf("b: %d is largest\n", b);
}
else
printf("c: %d is largest\n", c);
}
Output:
Enter three numbers
45
89
63
b: 89 is largest
AIM:
ALGORITHM:
Step5:Printtheresults
PROGRAM:
#include<stdio.h>
// functions declaration
int add(int n1,int n2);
int subtract(int n1, int n2);
int multiply(int n1,int n2);
int divide(int n1,int n2);
int square(int n1);
/ / main function
int main()
{
int num1,num2;
printf("Enter two numbers:");
scanf("%d%d",&num1,&num2);
printf("%d + %d = %d\n", num1, num2, add(num1,num2));
printf("%d - %d = %d\n", num1, num2, subtract(num1, num2));
printf("%d * %d = %d\n", num1, num2, multiply(num1, num2));
printf("%d / %d = %d\n", num1, num2, divide(num1, num2));
printf(“%d^%d=%d\n”,num1,num1,square(num1));
return 0;
}
//function to add two integer numbers
int add(int n1,int n2)
{
int result;
result=n1+n2;
return result;
}
int result;
result = n1 / n2;
return result;
}
OUTPUT:
Enter two numbers: 20 5
20+5=25
20–5=15
20*5=100
20/5=4
20^20=400
RESULT
Program:
#include <stdio.h>
int main()
{
int i, n;
int a = 0, b = 1,c;
printf("Enter the number of terms: ");
scanf("%d", &n);
printf("Fibonacci series: %d, %d, ", a, b);
for (i = 0; i <= n-3; ++i)
{
c = a + b;
a = b;
b = c;
printf("%d, ", c);
}
return 0;
}
Output:
Program:
#include <stdio.h>
int main()
{
int n, rev = 0, rem;
printf("Enter an integer: ");
scanf("%d", &n);
while (n != 0)
{
rem = n % 10;
rev = rev * 10 + rem;
n /= 10;
}
printf("Reversed number = %d", rev);
return 0;
}
Output:
Program:
#include <stdio.h>
int main()
{
int n, rev = 0, rem, a;
printf("Enter an integer: ");
scanf("%d", &n);
a = n;
while (n != 0)
{
rem = n % 10;
rev = rev * 10 + rem;
n /= 10;
}
if (a == rev)
printf("%d is a Palindrome.", a);
else
printf("%d is not a Palindrome.", a);
return 0;
}
Output:
Program:
#include <stdio.h>
int main()
{
int a,b,temp,sum=0;
printf(“Enter a three digit number: “);
scanf(“%d”,&a);
temp=a;
while(temp>0)
{
b=temp%10;
sum=sum+b*b*b;
temp/=10;
}
if(sum==a)
{
printf(“It’s an Armstrong number”);}
else
{
printf(“It’s not an Armstrong number “);}
return 0;
}
Output:
#include <stdio.h>
// Traverse the array and insert the element after every i-th position
int j = *size - 1; // Pointer for the last position of the new array
for (int k = originalSize - 1; k >= 0; k--) {
// If the current position is a multiple of i, insert the element
if ((k + 1) % i == 0) {
arr[j--] = elem; // Insert the element
}
arr[j--] = arr[k]; // Move the original element
}
}
int main() {
int n, i, elem;
return 0;
}
O/P:
Modified array: 1 2 9 3 4 9 5
int arr[n];
return 0;
}
O/P:
Enter the size of the array: 5
Enter 5 elements:
12 11 13 5 6
Sorted array: 5 6 11 12 13
Program:
#include<stdio.h>
int main()
{
inti=0,j=0;
int g ,h;
printf(“enter row size”);
scanf(“%d”,&g);
printf(“enter column size”);
scanf(“%d”,&h);
int a[g][h];
int b[g][h];
printf(“Enter the First Matrix elements :”);
for (i=0;i<g;i++)
{
for(j=0;j<h;j++)
{
printf(“a[%d] [%d]=”,i,j);
scanf(“%d”,&a[i][j]);
}
}
printf(“First Matrix:”);
for(i=0;i<g;i++)
{
for(j=0;j<h;j++)
{
printf(“%d”,a[i][j]);
}
printf(“ ”);
printf(“Enter Second Matrix elements:” );
for(i=0;i<g;i++)
{
for(j=0;j<h;j++)
{
printf(“b[%d][%d]= ”,i,j);
scanf(“%d”,&b[i][j]);
}
}
printf( “Second Matrix: ”);
for(i=0;i<g;i++)
{
for(j=0;j<h;j++)
{
printf(“%d ”,b[i][j]);
}
printf(“ ”);
}
printf(“Addition of Two Matrix “);
for(i=0;i<g;i++)
{
for(j=0;j<h;j++)
{
printf(“%d ”,a[i][j]+b[i][j]);
}
printf(“ ” );
}
return 0;
}
Output:
Program:
#include<stdio.h>
int main()
{
int i=0,j=0;
int g ,h;
printf(“enter row size”);
scanf(“%d”,&g);
printf(“enter column size”);
scanf(“%d”,&h);
int a[g][h];
int b[g][h];
printf(“Enter the First Matrix elements :”);
for (i=0;i<g;i++)
{
for(j=0;j<h;j++)
{
printf(“a[%d] [%d]=i,j);
scanf(“%d”,&a[i][j]);
}
}
printf(“First Matrix:”);
for(i=0;i<g;i++)
{
for(j=0;j<h;j++)
{
printf(“%d”,a[i][j]);
}
printf(“ ”);
printf(“Enter Second Matrix elements:” );
for(i=0;i<g;i++)
{
for(j=0;j<h;j++)
{
printf(“b[%d][%d]= ”,i,j);
scanf(“%d”,&b[i][j]);
}
}
printf( “Second Matrix: ”);
for(i=0;i<g;i++)
{
for(j=0;j<h;j++)
{
printf(“%d ”,b[i][j]);
}
printf(“ ”);
}
printf(“Multiplication of Two Matrix:”);
for(i=0;i<g;i++)
{
for(j=0;j<h;j++)
{
int c=0;
for(int k=0;k<h;k++)
{
c=c+(a[i][j]*b[i][j]);
}
printf(“%d”,c);
printf(“ “);
}
printf(“ “);
}
return 0;
}
Output:
Program:
#include<stdio.h>
int main()
{
int i=0,j=0;
int g ,h;
printf(“enter row size”);
scanf(“%d”,&g);
printf(“enter column size”);
scanf(“%d”,&h);
int a[g][h];
int c[g][h];
printf(“Enter the First Matrix elements :”);
for (i=0;i<g;i++)
{
for(j=0;j<h;j++)
{
printf(“a[%d] [%d]=”,i,j);
scanf(“%d”,&a[i][j]);
}
}
printf(“First Matrix:”);
for(i=0;i<g;i++)
{
for(j=0;j<h;j++)
{
printf(“%d”,a[i][j]);
}
printf(“ “);
}
for(i=0;i<g;i++)
{
for(j=0;j<h;j++)
{
c[i][j]=a[i][j];
}
}
printf(“Transpose of Matrix:”);
for(i=0;i<g;i++)
{
for(j=0;j<h;j++)
{
printf( “%d”,c[i][j]);
}
printf(“ ”);
}
return 0;
}
Output:
First Matrix:
1 2 3
4 5 6
7 8 9
Transpose of Matrix:
1 4 7
2 5 8
3 6 9
O/P:
**** Performing string concatenation ****
Enter two strings
Sara nathan
The concatenated string is Saranathan
// Loop to copy the required part of the source string to the result string
for (i = 0; i < length && source[start + i] != '\0'; i++) {
result[i] = source[start + i];
}
int main() {
char source[100], result[100];
int start, length;
return 0;
}
O/P:
Enter the source string: Hello, World!
Enter the start index: 7
Enter the length of the substring: 5
int main() {
char str[100];
// Input string
printf("Enter a string: ");
fgets(str, sizeof(str), stdin);
return 0;
}
O/P:
Enter a string: A man a plan a canal Panama
// Input strings
printf("Enter the strings (one per line):\n");
for (int i = 0; i < n; i++) {
scanf("%s", arr[i]);
}
return 0;
}
O/P:
Enter the number of strings: 5
Enter the strings (one per line):
apple
banana
grape
orange
kiwi
Enter the string to search: grape
Program:
#include<stdio.h>
void swap(int,int);
void main( )
{
int n1,n2;
printf("Enter the two numbers to be swapped\n");
scanf("%d%d",&n1,&n2);
printf("\nThe values of n1 and n2 in the main function before calling the swap
function are n1=%d n2=%d",n1,n2);
swap(n1,n2);
printf("\nThe values of n1 and n2 in the main function after calling the swap
function are n1=%d n2=%d",n1,n2);
}
void swap(int n1,int n2)
{
int temp;
temp=n1;
n1=n2;
n2=temp;
printf("\nThe values of n1 and n2 in the swap function after swapping are n1=%d
n2=%d",n1,n2);
}
Output:
Program:
#include<stdio.h>
void swap(int *,int *);
void main( )
{
int n1,n2;
printf("Enter the two numbers to be swapped\n");
scanf("%d%d",&n1,&n2);
printf("\nThe values of n1 and n2 in the main function before calling the swap
function are n1=%d n2=%d",n1,n2);
swap(&n1,&n2);
printf("\nThe values of n1 and n2 in the main function after calling the swap
function are n1=%d n2=%d",n1,n2);
}
void swap(int *n1,int *n2)
{
int temp;
temp=*n1;
*n1=*n2;
*n2=temp;
printf("\nThe values of n1 and n2 in the swap function after swapping are n1=%d
n2=%d",*n1,*n2);
}
Output:
#include <stdio.h>
int main() {
int n, target;
int arr[n];
if (resultByValue != -1) {
printf("Using Pass by Value: Element %d found at index %d.\n", target, resultByValue);
} else {
printf("Using Pass by Value: Element %d not found.\n", target);
}
if (resultByReference != -1) {
printf("Using Pass by Reference: Element %d found at index %d.\n", target, resultByReference);
} else {
printf("Using Pass by Reference: Element %d not found.\n", target);
}
return 0;
}
O/P:
Enter the number of elements in the array: 5
Enter 5 sorted elements:
10 20 30 40 50
Enter the target value to search: 30
ALGORITHM:
Step 1: START
Step 2: Read student details like name,
mark1,2,3 Step 3: Calculate total, and
average
Step 4: Display the
grade Step 5: STOP
PROGRAM:
#include <stdio.h>
int main() {
struct Student student;
O/P:
Enter the student's name: John Doe
Enter the student's roll number: 101
Enter marks for 5 subjects (out of 10):
Subject 1: 8
Subject 2: 9
Subject 3: 7
Subject 4: 6
Subject 5: 8
Program:
#include<stdio.h>
#define MAX_FILE_NAME 100
int main()
{
FILE* fp;
int count = 0;
char filename[MAX_FILE_NAME];
char c;
printf("Enter file name: ");
scanf("%s", filename);
fp = fopen(filename, "r");
if (fp == NULL) {
printf("Could not open file %s",filename);
return 0;
}
for (c = getc(fp); c != EOF; c = getc(fp))
count = count + 1;
fclose(fp);
printf("The file %s has %d characters\n ",filename, count);
return 0;
}
Output:
8.a Count the number of character, words and lines in the file
#include <stdio.h>
#include <ctype.h>
// If the file ends while inside a word, count the last word
if (inWord) {
(*wordCount)++;
}
}
int main() {
FILE *file;
char filename[100];
int charCount = 0, wordCount = 0, lineCount = 0;
return 0;
}
O/P:
Hello World! This is a test.
This is the second line.
And here is the third line.
8b. Replace a specific word with the given word in the same file
#include <stdio.h>
#include <string.h>
int main() {
FILE *file;
char filename[100];
char oldWord[100], newWord[100];
return 0;
}
O/P:
Hello world!
This is a test file. Hello again!
Hi world!
This is a test file. Hi again!