0% found this document useful (0 votes)
21 views15 pages

c program - lab - sami

The document contains a series of programming experiments in C, each demonstrating different algorithms and functionalities such as checking for Armstrong numbers, summing digits, generating Fibonacci sequences, finding the largest and smallest numbers in a list, swapping values, matrix operations, calculating factorials, string operations, and implementing linear search. Each experiment includes the program code, aim, and sample output. The experiments are designed to enhance understanding of C programming concepts and functions.

Uploaded by

Sathish Askanur
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)
21 views15 pages

c program - lab - sami

The document contains a series of programming experiments in C, each demonstrating different algorithms and functionalities such as checking for Armstrong numbers, summing digits, generating Fibonacci sequences, finding the largest and smallest numbers in a list, swapping values, matrix operations, calculating factorials, string operations, and implementing linear search. Each experiment includes the program code, aim, and sample output. The experiments are designed to enhance understanding of C programming concepts and functions.

Uploaded by

Sathish Askanur
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/ 15

PROGRAMMING IN C LAB

Experiment Number: 1
Experiment Name: Write a C program to check whether the given number is Armstrong or
not
Aim: T o Write a C program to check whether the given number is Armstrong or not.
Program:
#include<stdio.h>
#include<conio.h>
int main(){
int num,r,sum=0,temp;
clrscr();
printf("Enter a number: ");
scanf("%d",&num);
temp=num;
while(num!=0)
{
r=num%10;
num=num/10;
sum=sum+(r*r*r);
}
if(sum==temp)
printf("%d is an Armstrong number",temp);
else
printf("%d is not an Armstrong number",temp);
getch(); return
0;
}

Output:
Enter a number:121
121 is an Armstrong number

Experiment Number: 2
Experiment Name: Write a C program to find the sum of individual digits of a positive
integer.
Aim: To Write a C program to find the sum of individual digits of a positive integer.

Program:
#include<stdio.h>
#include<conio.h>

g.sathish 1
void main()
{
int n,r,s=0;
clrscr();
printf(“Enter any positive integer:”);
scanf(“%d”,&n);
while(n>0)
{ r=n
%10;
s=s+r;
n=n/10;
}
printf(“the sum of individual digits of positive integer=%d”,s); getch();
}

Output:
Enter any positive integer:123
The sum of individual digits of positive integer=6

Experiment Number: 3
Experiment Name: Write a program to generate the first n terms of the Fibonacci
sequence
Aim: T o Write a program to generate the first n terms of the Fibonacci
sequence.
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int i,n,a=0,b=1,c;
clrscr();
printf(“Enter any number :”);
scanf(“%d”,&n);
printf(“ The fibonacci sequence:\n”);
printf(“\t%d\t%d\t”,a,b);
for(i=3; i<=n;i++)
{
c=a+b; printf(“%d\
t”,c); a=b;
b=c;
}
getch();
g.sathish 2
}

Output:
Enter any number:5
The fibonacci sequence:
0
1
1
2
3

Experiment Number: 4
Experiment Name: largest and smallest number in a list of integers.
Aim: Write a C program to find both the largest and smallest number in a list of
integers.

Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int i,n,a[10],largest,smallest; clrscr();
printf("How many numbers you want to enter:");
scanf("%d",&n);
printf("Enter %d elements : \n",n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
largest=a[0];
smallest=a[0];
for(i=1;i<n;i++)
{
if(largest<a[i])
largest=a[i];
if(smallest>a[i])
smallest=a[i];
}
printf("\n largest number =%d\n",largest);

g.sathish 3
printf("smallest number=%d",smallest);
getch();
}
Output:
How many numbers you want to enter:5
Enter 5 elements:
45
65
76
23
10
largest number =76
smallest number=10 2

Experiment Number: 5
Experiment Name: Write a program for swapping of two integer values using Call by
Value & Call by Address
Aim: To Write a program for swapping of two integer values using Call by Value &
Call by Address
Program: Call by Value
#include<stdio.h>
#include <conio.h>
void swap(int x, int y);
main()
{
int a,b;
clrscr();
printf("Enter two numbers: ");
scanf("%d%d",&a,&b);
printf("Before swapping\n");
printf("a = %d\n",a);
printf("b = %d\n",b);
swap(a,b); // function call by Value
printf("Completed swapping..\n");
getch();
}
void swap(int x, int y)
{
int temp;
temp =x; x
= y;
g.sathish 4
y = temp;
printf("After Swapping Values \n");
printf("a = %d\n",x);
printf("b = %d\n",y);
}
Output:
Enter two numbers: 5 6
Before swapping
a=5
b=6
complete swapping
After swapping values
a=6
b=5
Program: Call by Address
#include<stdio.h>
#include<conio.h>
void swap(int *x, int *y);
main()
{
int a,b;
clrscr();
printf("Enter two numbers: ");
scanf("%d%d",&a,&b);
printf("Before swapping\n");
printf("a = %d\n",a);
printf("b = %d\n",b);
swap(&a,&b); // function call by Value
printf("Completed swapping….\n");
getch();
}
void swap(int *x, int *y) // function definition
{
int temp;
temp =*x;
*x = *y;
*y = temp;
printf("After Swapping Values \n");
printf("a = %d\n",*x);
printf("b = %d\n",*y);
}
g.sathish 5
Output:
Enter two numbers: 5 6
Before swapping
a=5
b=6
complete swapping…
After swapping values
a=6
b=5
Experiment Number: 6
Experiment Name: Write a program to add two matrices using
functions.
Aim: To Write a program to add two matrices using functions.
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int a[2][2],b[2][2],c[2][2],r1,c1,r2,c2,i,j;
clrscr();
printf("enter no of rows & columns of matrix-A\n ");
scanf("%d%d",&r1,&c1);
printf("enter no of rows & columns of matrix-B\n ");
scanf("%d%d",&r2,&c2);
if(r1==r2 && c1==c2)
{
printf("enter elements of matrix-A\n ");
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("enter elements of matrix-B\n ");
for(i=0;i<r2;i++)
{
for(j=0;j<c2;j++)
{
scanf("%d",&b[i][j]);
}

g.sathish 6
}
printf("matrix addition:\n");
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
c[i][j]=a[i][j]+b[i][j];
printf("%2d",c[i][j]);
}
printf("\n");
}
}
else
printf("matrix addition is not possible");
getch();
}

Output:
enter no of rows & columns of matrix-A:
2
2
enter no of rows & columns of matrix-B:
2
2
enter elements of matrix-A
1
2
3
4
enter elements of matrix-B
1
2
3
4
matrix addition:
2
4
6
8
Experiment Number: 7
Experiment Name: Write a program to calculate factorial of given integer value using
recursive function.

g.sathish 7
Aim: To Write a program to calculate factorial of given integer value using recursive
function
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int n,f;
int fact(int);
clrscr();
printf(“Enter any integer: ”);
scanf(“%d”,&n);
f=fact(n);
printf(“the factorial of a given number=%d”,f);
getch();
}
int fact(int n)
{
int f;
if(n==1)
return 1;
else
f=n*fact(n-1);
return (f);
}

Output:
Enter any integer:5
the factorial of a given number=120

Experiment Number: 8
Experiment Name: Write a program for multiplication of two N X N matrices using
functions.
Aim: To Write a program for multiplication of two N X N matrices using functions.
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int a[2][2],b[2][2],c[2][2],i,j,m,n,p,q,k;
clrscr();
printf("\n Enter the order of matrix-A\n ");

g.sathish 8
scanf("%d%d",&m,&n);
printf("\n Enter order of matrix-B\n ");
scanf("%d%d",&p,&q);
if(n==p)
{
printf("Enter elements of matrix-A\n ");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
}
printf("Enter elements of matrix-B\n ");
for(i=0;i<p;i++)
{
for(j=0;j<q;j++)
scanf("%d",&b[i][j]);
}
for(i=0;i<m;i++)
{
for(j=0;j<q;j++)
{ c[i][j]=0;
for(k=0;k<p;k++)
{
c[i][j]=c[i][j]+(a[i][k]*b[k][j]);
}
}
}
printf("\n Multiplication of matrix is\n");
for(i=0;i<m;i++)
{
for(j=0;j<q;j++)
{
printf("%2d",c[i][j]);
}
printf("\n");
}
}
else
printf("\n Matrix multiplication is not possible");
getch();
}

Output:

g.sathish 9
enter no of rows & columns of matrix-A:
2
2
enter no of rows & columns of matrix-B:
2
2
enter elements of matrix-A
1
2
3
4
enter elements of matrix-B
1
2
3
4
Multiplication of matrix is
4
10
15
22
Experiment Number: 9
Experiment Name: Write a c program to perform various string operations.
Aim: Write a c program to perform various string operations
Program:
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
int find_length(char string[])
{
int len = 0, i;
for (i = 0; string[i] != '\0'; i++)
{
len++;
}
return len;
}
void join_strings(char string1[], char string2[])
{
int i, len1, len2;

1
g.sathish
0
len1 = find_length(string1);
len2 = find_length(string2);
for (i = len1; i < len1 + len2; i++)
{ string1[i] = string2[i - len1];
}
string1[i] = '\0';
}
/*returns 0 if thery are same otherwise returns 1*/
int compare_strings(char string1[], char string2[])
{
int len1, len2, i, count = 0;
len1 = find_length(string1);
len2 = find_length(string2);
if (len1 != len2)
return 1;
for (i = 0; i < len1; i++)
{
if (string1[i] == string2[i]) count++;
}
if (count == len1)
return 0;
return 1;
}
void copy_string(char destination[], char source[])
{
int len, i;
len = find_length(source);
for (i = 0; i < len; i++)
{
destination[i] = source[i];
}
destination[i] = '\0';
}
int main()
{
char string1[20], string2[20]; int
choice;
clrscr();
while (1)
{
printf("\n1. Find Length \n2. Concatenate \n3. Compare \n4. Copy
\n5. Exit\n");

1
g.sathish
1
printf("Enter your choice: ");
scanf("%d", & choice);
switch (choice)
{
case 1:
printf("Enter the string: ");
scanf("%s", string1);
printf("The length of string is %d", find_length(string1));
break;
case 2:
printf("Enter two strings: "); scanf("%s
%s", string1, string2);
join_strings(string1, string2);
printf("The concatenated string is %s", string1);
break;
case 3:
printf("Enter two strings: "); scanf("%s
%s", string1, string2);
if (compare_strings(string1, string2) == 0)
{
printf("They are equal");
}
else
{
printf("They are not equal");
}
break;
case 4:
printf("Enter a string: ");
scanf("%s", string1);
printf("String1 = %s\n",string1);
printf("After copying string1 to string 2\n");
copy_string(string2, string1);
printf("String2 = %s", string2);
break;
case 5:
exit(0);
}
}
getch();
return 0;
}

1
g.sathish
2
Output:
1. Find Length
2. Concatenate
3. Compare
4. Copy
5. Exit
Enter your choice:1
Enter the string: india
The length of string is 5

Experiment Number: 10
Experiment Name: Write C program that implements searching of given item in a given
list using linear search.
Aim: Write C program that implements searching of given item in a given list using
linear search.
Program:
#include<stdio.h>
#include<conio.h>
#include<process.h>
void main()
{
int i,n,element,a[10];
clrscr();
printf("Enter how many elements u want to enter:");
scanf("%d",&n);
printf("Enter %d elements :\n",n);
for(i=0;i<n;i++) scanf("%d",&a[i]);
printf("Enter any element to search : ");
scanf("%d",&element);
for(i=0;i<n;i++)
{
if(a[i]==element)
{
printf("Element is found at position %d",i);
getch();
exit(0);
}
}
printf("Element is not found");
getch();
}

1
g.sathish
3
Output:
Enter how many elements u want to enter:5
Enter 5 elements :
12
56
78
43
9
Enter any element to search :9
Element is found at position 5

1
g.sathish
4
9

1
g.sathish
5

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