Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
29 views39 pages

C Programs

The document is a certificate template from Bagalkot University for students completing practical work in C programming during the academic year 2025-26. It includes a list of experiments and corresponding C programs that students are required to perform, covering various programming concepts such as arithmetic operations, prime number checks, and array manipulations. The document also contains sections for signatures from teachers and examiners.

Uploaded by

Mgm Mallikarjun
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views39 pages

C Programs

The document is a certificate template from Bagalkot University for students completing practical work in C programming during the academic year 2025-26. It includes a list of experiments and corresponding C programs that students are required to perform, covering various programming concepts such as arithmetic operations, prime number checks, and array manipulations. The document also contains sections for signatures from teachers and examiners.

Uploaded by

Mgm Mallikarjun
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 39

Bagalkot University, Jamkhandi-587301

Shri K.K. Tungal Memorial Trust (R.,)


TUNGAL SCHOOL OF BASIC AND APPLIED
SCIENCES, JAMKHANDI-587301

Department of Computer Science


Certificate

This is to certify that the work entered in the journal is the work of

Kumar/Kumari: _______________________________________________________________

of division Reg. No: ____________________ Subject: Cprogramming Lab has satisfactorily

completed the required number of practicals and worked as prescribed by the

BAGALKOT UNIVERSITY, JAMKHANDI for B.Sc I Semester in the college laboratory

during the academic year 2025-26.

____________ ______________ ____________


Subject Teacher Head of the Department Principal

____________ ____________
External Examiner Internal Examiner

Date: ____________
Index
Particulars of the experiments performed
PART A
Exp. Page Date of Date of
Name of the Experiment Signature
No No experiment Submission
Write a command to create a directory,
change directory and Type a C

1 Program to read radius of a circle and


to find area and circumference. And
save and run it.
Write a C Program to read three
2 numbers and find the biggest of three.
Write a C Program to demonstrate
3 library functions in math.h

4 Write a C Program to check for prime.


Write a C Program to generate n
5 primes.
Write a C Program to read a number,

6 find the sum of the digits, reverse the


number and check it for palindrome.
. Write a C Program to read numbers
from keyboard continuously till the
7 user presses 999 and to find the sum of
only positive numbers
Write a C Program to read two nos and

8 perform arithmetic operations using


switch case.
Write a C program to read marks
scored by n students and find the
9 average of marks (Demonstration of
single dimensional array.
Write a C Program to remove

10 Duplicate Element in a single


dimensional Array.
Program to perform addition and
11 subtraction of Matrices.

___________________
Staff In charge
PART B
Exp. Page Date of Date of
Name of the Experiment Signature
No No experiment Submission
Write a C Program to find the length of
1 a string without using built-in function.

Write a C Program to demonstrate


2
string functions.
Write a C Program to check a number

3 for prime by defining isprime()


function.

Write a C Program to read, display and


4 to find the trace of a square matrix.

Write a C Program to read, display and

5 multiply two mxn matrices using


functions.
Write a C Program to read a string and
to find the number of alphabets, digits,
6 vowels, consonants, spaces and special
characters.

7 Write a C Program to Reverse a String.

Write a C Program to Swap Two


8
Numbers.
Write a C Program to demonstrate

9 student structure to read & display


records of n students.
Write a C Program to demonstrate the
10 difference between structure & union.
Write a C Program to find the roots of

11 quadratic equation (demonstration of


else if ladder).

___________________
Staff In charge
C Programming Lab

PART A

DEPT OF CS TSBAS, JAMKHANDI Page 1


C Programming Lab
Prog 1: Write a command to create a directory, change
directory and Type a C Program to read radius of a circle and
to find area and circumference. And save and run it.

#include<stdio.h>
#include<conio.h>
void main()
{
float r,area,circum;
clrscr();
printf("Enter the Radius of a circle:");
scanf("%f",&r);
area=2*3.142*r;
circum=3.142*r*r;
printf("Area of a Circle=%f\n",area);
printf("Circumference of the circle=%f\n",circum);
getch();
}
************Output***************
Enter the Radius of a circle:5
Area of a Circle=31.420000
Circumference of the circle=78.550003

DEPT OF CS TSBAS, JAMKHANDI Page 2


C Programming Lab
Prog 2: Write a C Program to read three numbers and find the
biggest of three.
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
clrscr();
printf("Enter the three values\n");
scanf("%d%d%d",&a,&b,&c);
if(a>b&&a>c)
{
printf("a is largest: %d is grater then %d and
%d\n",a,b,c);
}
else if(b>a&&b>c)
{
printf("b is largest: %d is grater then %d and
%d\n",b,a,c);
}
else
{
printf("c is largest: %d is grater then %d and
%d\n",c,a,b);
}
getch();
}
*******************Output****************
Enter the three values
120 45 67
a is largest: 120 is grater then 45 and 67
Enter the three values
12 333 45
b is largest: 333 is grater then 12 and 45
Enter the three values
1 2 567
c is largest: 567 is grater then 1 and 2

DEPT OF CS TSBAS, JAMKHANDI Page 3


C Programming Lab
Prog 3. Write a C Program to demonstrate library functions in
math.h
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int s,val1,base,ex,res;
float val;
clrscr();
printf("Enter the value for finding squareroot:");
scanf("%d",&s);
printf("Squareroot of %d is %f\n",s,sqrt(s));
printf("Enter the floating point value to demonstrate math
functions:");
scanf("%f",&val);
printf("Ceil value of %f=%f\n",val,ceil(val));
printf("Floor value of %f=%f\n",val,floor(val));
printf("Enter the negative value to demonstrate math
functions:");
scanf("%d",&val1);
printf("Absolute value of %d=%d\n",val1,abs(val1));
printf("Enter the base and exponent to find exponential
value:\n");
scanf("%d%d",&base,&ex);
res=pow(base,ex);
printf("The value of %d raise to %d is %d\n",base,ex,res);
getch();
}
***************************OUTPUT************************
Enter the value for finding squareroot:9
Squareroot of 9 is 3.000000
Enter the floating point value to demonstrate math
functions:3.45
Ceil value of 3.450000=4.000000
Floor value of 3.450000=3.000000
Enter the negative value to demonstrate math functions:-678
Absolute value of -678=678
Enter the base and exponent to find exponential value:
2 5
The value of 2 raise to 5 is 32
DEPT OF CS TSBAS, JAMKHANDI Page 4
C Programming Lab
Prog 4. Write a C Program to check for prime.
#include<stdio.h>
#include<conio.h>
void main()
{
int i,x,c=0;
clrscr();
printf("Enter the value to check whether the no is prime
or not:");
scanf("%d",&x);
for(i=1;i<=x;i++)
{
if(x%i==0)
{
c++;
}
}
if(c<=2)
{
printf("%d is prime\m",x);
}
else
{
printf("%d is not prime\n",x);
}
getch();
}
***************************OUTPUT************************
OUTPUT 1:
Enter the value to check whether the no is prime or not:5
5 is prime

OUTPUT 2:
Enter the value to check whether the no is prime or not:9
9 is not prime

OUTPUT 3:
Enter the value to check whether the no is prime or not:1
1 is prime

DEPT OF CS TSBAS, JAMKHANDI Page 5


C Programming Lab
Prog 5. Write a C Program to generate n primes.
#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,j,c;
clrscr();
printf("Enter the value:");
scanf("%d",&n);
printf("Prime numbers from 1 to %d\n",n);
for(i=1;i<=n;i++)
{
c=0;
for(j=1;j<=n/2;j++)
{
if(i%j==0)
{
c++;
}
}
if(c<=2)
{
printf("%d\n",i);
}
}
getch();
}
***************************OUTPUT************************
Enter the value: 20
Prime numbers from 1 to 20
1
2
3
5
7
11
13
17
19

DEPT OF CS TSBAS, JAMKHANDI Page 6


C Programming Lab
Prog 6: Write a C Program to read a number, find the sum of
the digits, reverse the number and check it for palindrome.
#include<stdio.h>
#include<conio.h>
void main()
{
int num,temp,rev=0,rem=0,sum=0;
clrscr();
printf("Enter the number:");
scanf("%d",&num);
temp=num;
while(num!=0)
{
rem=num%10;
rev=rev*10+rem;
num=num/10;
sum=sum+rem;
}
if(rev==temp)
{
printf("%d is palindrome\n",temp);
}
else
{
printf("%d is not palindrome\n",temp);
}
printf("Sum=%d\n",sum);
getch();
}
***************************OUTPUT************************
Enter the number:345
345 is not palindrome
Sum=12
Enter the number:121
121 is palindrome
Sum=4

DEPT OF CS TSBAS, JAMKHANDI Page 7


C Programming Lab
Prog 7. Write a C Program to read numbers from keyboard
continuously till the user presses 999 and to find the sum of
only positive numbers.

#include<stdio.h>
#include<conio.h>
void main()
{
int num,sum=0;
clrscr();
x:printf("Enter the number:");
scanf("%d",&num);
if(num!=999)
{
if(num>0)
{
sum=sum+num;
goto x;
}
else
{
goto x;
}
}
else
{
printf("Sum =%d\n",sum);
}
getch();
}

***************************OUTPUT************************
Enter the number:-45
Enter the number:67
Enter the number:33
Enter the number:45
Enter the number:-989
Enter the number:999
Sum =145

DEPT OF CS TSBAS, JAMKHANDI Page 8


C Programming Lab
Prog 8. Write a C Program to read two nos and perform
arithmetic operations using switch case.

#include<stdio.h>
#include<conio.h>
void main()
{
int n1,n2,op;
float res;
clrscr();
printf("Enter the Values of n1 and n2\n");
scanf("%d%d",&n1,&n2);
x:printf("1.Addition\n2.Subtraction\n3.Multiplication\n4.D
ivision\n5.Exit\nEnter your Choice:");
scanf("%d",&op);
switch(op)
{
case 1: printf("%d + %d = %d\n",n1,n2,n1+n2);
goto x;
case 2: printf("%d - %d = %d\n",n1,n2,n1-n2);
goto x;
case 3: printf("%d x %d = %d\n",n1,n2,n1*n2);
goto x;
case 4: res=n1/n2;
printf("%d / %d = %f\n",n1,n2,res);
goto x;
case 5: break;
default:printf("Enter the correct choice\n");
goto x;
}
getch();
}

DEPT OF CS TSBAS, JAMKHANDI Page 9


C Programming Lab
***************************OUTPUT************************
Enter the Values of n1 and n2
9
3
1.Addition
2.Subtraction
3.Multiplication
4.Division
5.Exit
Enter your Choice:1
9 + 3 = 12
1.Addition
2.Subtraction
3.Multiplication
4.Division
5.Exit
Enter your Choice:2
9 - 3 = 6
1.Addition
2.Subtraction
3.Multiplication
4.Division
5.Exit
Enter your Choice:3
9 x 3 = 27
1.Addition
2.Subtraction
3.Multiplication
4.Division
5.Exit
Enter your Choice:4
9 / 3 = 3.000000
1.Addition
2.Subtraction
3.Multiplication
4.Division
5.Exit
Enter your Choice:5

DEPT OF CS TSBAS, JAMKHANDI Page 10


C Programming Lab
Prog 9. Write a C program to read marks scored by n students
and find the average of marks (Demonstration of single
dimensional array.

#include<stdio.h>
#include<conio.h>
void main()
{
int stud[100],n,i,sum=0;
float avg=0;
clrscr();
printf("Enter the no of students:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter the marks of %d Student:",i+1);
scanf("%d",&stud[i]);
}
for(i=0;i<n;i++)
{
printf("Marks of the %d student:%d\n",i+1,stud[i]);
sum=sum+stud[i];
}
printf("---------------------------------\n");
printf(" Total:%d\n",sum);
avg=sum/n;
printf(" Aerage:%f\n",avg);
getch();
}

DEPT OF CS TSBAS, JAMKHANDI Page 11


C Programming Lab
***************************OUTPUT************************
Enter the no of students:5
Enter the marks of 1 Student:88
Enter the marks of 2 Student:77
Enter the marks of 3 Student:66
Enter the marks of 4 Student:55
Enter the marks of 5 Student:44
Marks of the 1 student:88
Marks of the 2 student:77
Marks of the 3 student:66
Marks of the 4 student:55
Marks of the 5 student:44
---------------------------------
Total:330
Aerage:66.0000000

DEPT OF CS TSBAS, JAMKHANDI Page 12


C Programming Lab
Prog 10. Write a C Program to remove Duplicate Element in a
single dimensional Array.

#include<stdio.h>
#include<conio.h>
void main()
{
int arr[20],n,i,j,k;
clrscr();
printf("Enter the size of an arry:");
scanf("%d",&n);
printf("Enter the %d Elements of an array\n",n);
for(i=0;i<n;i++)
{
scanf("%d",&arr[i]);
}
printf("Array elements\n");
for(i=0;i<n;i++)
{
printf("arr[%d]=%d\n",i,arr[i]);
}
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(arr[i]==arr[j])
{
for(k=j;k<n;k++)
{
arr[k]=arr[k+1];
}
n--;
j--;
}
}
}
printf("Array after removing duplicate element\n");
for(i=0;i<n;i++)
{
printf("arr[%d]=%d\n",i,arr[i]);
}
DEPT OF CS TSBAS, JAMKHANDI Page 13
C Programming Lab
printf("\n");
getch();
}
***************************OUTPUT************************
Enter the size of an arry:6
Enter the 6 Elements of an array
11
33
44
11
22
11
Array elements
arr[0]=11
arr[1]=33
arr[2]=44
arr[3]=11
arr[4]=22
arr[5]=11
Array after removing duplicate element
arr[0]=11
arr[1]=33
arr[2]=44
arr[3]=22

DEPT OF CS TSBAS, JAMKHANDI Page 14


C Programming Lab
Prog 11. Program to perform addition and subtraction of
Matrices.
#include<stdio.h>
#include<conio.h>
void insert(int mat[5][5],int m,int n)
{
int i,j;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&mat[i][j]);
}
}
}
void display(int mat[5][5],int m,int n)
{
int i,j;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("%d\t",mat[i][j]);
}
printf("\n");
}
}
void add(int mat1[5][5],int mat2[5][5],int m,int n)
{
int res[5][5],i,j;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
res[i][j]=mat1[i][j]+mat2[i][j];
}
}
printf("Sum of two matrices\n");
display(res,m,n);
}
void sub(int mat1[5][5],int mat2[5][5],int m,int n)
DEPT OF CS TSBAS, JAMKHANDI Page 15
C Programming Lab
{
int res[5][5],i,j;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
res[i][j]=mat1[i][j]-mat2[i][j];
}
}
printf("Difference of two matrices\n");
display(res,m,n);
}
void main()
{
int mat1[5][5],mat2[5][5],sum[5][5],dif[5][5],m,n,i,j;
clrscr();
printf("Enter the no of rows and columns\n");
scanf("%d%d",&m,&n);
printf("Enter the elements of matrix 1:\n");
insert(mat1,m,n);
printf("Enter the elements of matrix 2:\n");
insert(mat2,m,n);
printf("Elements of matrix 1:\n");
display(mat1,m,n);
printf("Elements of matrix 2:\n");
display(mat2,m,n);
add(mat1,mat2,m,n);
sub(mat1,mat2,m,n);
getch();
}

DEPT OF CS TSBAS, JAMKHANDI Page 16


C Programming Lab
***************************OUTPUT************************
Enter the no of rows and columns
2
3
Enter the elements of matrix 1:
1
2
3
4
5
6
Enter the elements of matrix 2:
1
2
3
4
5
6
Elements of matrix 1:
1 2 3
4 5 6
Elements of matrix 2:
1 2 3
4 5 6
Sum of two matrices
2 4 6
8 10 12
Difference of two matrices
0 0 0
0 0 0

DEPT OF CS TSBAS, JAMKHANDI Page 17


C Programming Lab

PART B

DEPT OF CS TSBAS, JAMKHANDI Page 18


C Programming Lab
Prog 1: Write a C Program to find the length of a string
without using built-in function.
#include<stdio.h>
#include<conio.h>
void main()
{
char s[20];
int i;
clrscr();
printf("Enter the string:");
scanf("%s",&s);
for(i=0;s[i]!='\0';++i);
printf("Length of string %s = %d\n",s,i);
getch();
}
***************************OUTPUT************************
Enter the string: TUNGAL
Length of string TUNGAL = 6

DEPT OF CS TSBAS, JAMKHANDI Page 19


C Programming Lab
Prog 2: Write a C Program to demonstrate string functions.
int x;
clrscr();
printf("Enter String 1: ");
scanf("%s",&s1);
printf("Enter String 2: ");
scanf("%s",&s2);
printf("Length of %s =%d\n",s1,strlen(s1));
printf("Length of %s =%d\n",s2,strlen(s2));
x=strcmp(s1,s2);
if(x==0)
{
printf("%s is equal to %s\n",s1,s2);
}
else
{
printf("%s is not equal to %s\n",s1,s2);
}
printf("Copy string s1=%s to res=%s\n",s1,strcpy(res,s1));
printf("Concatinate s1=%s and s2=%s is %s\n",s1,s2,strcat
(s1,s2));
printf("Upper case of %s=%s\nUpper case of %s=%s\n",s1,
strupr(s1),s2,strupr(s2));
printf("Lower case of %s=%s\nLower case of
%s=%s\n",s1,strlwr(s1),s2,strlwr(s2));
getch();

}
***************************OUTPUT************************
Enter String 1: Tungal
Enter String 2: Schools
Length of Tungal =6
Length of Schools =7
Tungal is not equal to Schools
Copy string s1=Tungal to res=Tungal
Concatinate s1=TungalSchools and s2=Schools is TungalSchools
Upper case of TUNGALSCHOOLS=TUNGALSCHOOLS
Upper case of SCHOOLS=SCHOOLS
Lower case of tungalschools=tungalschools
Lower case of schools=schools

DEPT OF CS TSBAS, JAMKHANDI Page 20


C Programming Lab
Prog 3: Write a C Program to check a number for prime by
defining isprime() function.
#include<stdio.h>
#include<conio.h>
void isprime(int x)
{
int i,c=0;
for(i=1;i<=x;i++)
{
if(x%i==0)
{
c++;
}
}
if(c<=2)
{
printf("%d is prime\n",x);
}
else
{
printf("%d is not prime\n",x);
}
}
void main()
{
int num;
clrscr();
printf("Enter the value to check whether the given no is
prime or not:");
scanf("%d",&num);
isprime(num);
getch();
}
***************************OUTPUT************************
Enter the value to check whether the given no is prime or not:
4
4 is not prime

Enter the value to check whether the given no is prime or not:


23
23 is prime
DEPT OF CS TSBAS, JAMKHANDI Page 21
C Programming Lab
Prog 4: Write a C Program to read, display and to find the
trace of a square matrix.
#include<stdio.h>
#include<conio.h>
void main()
{
int mat[5][5],m,i,j,sum=0;;
clrscr();
printf("Size of square matrix m=n: ");
scanf("%d",&m);
printf("Enter the elements of square matrix\n");
for(i=0;i<m;i++)
{
for(j=0;j<m;j++)
{
scanf("%d",&mat[i][j]);
}
}
printf("Given square matrix is :\n");
for(i=0;i<m;i++)
{
for(j=0;j<m;j++)
{
printf("%d\t",mat[i][j]);
}
printf("\n");
}
for(i=0;i<m;i++)
{
sum=sum+mat[i][i];
}
printf("\nTrace of matrix is =%d\n",sum);
getch();
}

DEPT OF CS TSBAS, JAMKHANDI Page 22


C Programming Lab
***************************OUTPUT************************
Size of square matrix m=n: 3
Enter the elements of square matrix
1
2
3
4
5
6
7
8
9
Given square matrix is:
1 2 3
4 5 6
7 8 9

Trace of matrix is =15

DEPT OF CS TSBAS, JAMKHANDI Page 23


C Programming Lab
Prog 5: Write a C Program to read, display and multiply two
mxn matrices using functions.
#include<stdio.h>
#include<conio.h>
void read(int mat[5][5],int m,int n)
{
int i,j;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&mat[i][j]);
}
}
}
void display(int mat[5][5],int m,int n)
{
int i,j;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("%d\t",mat[i][j]);
}
printf("\n");
}
}
void mul(int mat1[5][5],int mat2[5][5],int r1,int r2,int c2)
{
int res[5][5],i,j,k;
for(i=0;i<r1;i++)
{
for(j=0;j<c2;j++)
{
res[i][j]=0;
for(k=0;k<r2;k++)
{
res[i][j]+=mat1[i][k]*mat2[k][j];
}
}
}
DEPT OF CS TSBAS, JAMKHANDI Page 24
C Programming Lab
display(res,r1,c2);
}
void main()
{
int mat1[5][5],mat2[5][5],c1,c2,r1,r2;
clrscr();
printf("Enter the no of rows and columns of matrix:\n");
scanf("%d%d",&r1,&c1);
r2=c1;
c2=r1;
printf("Enter the elements of matrix 1:\n");
read(mat1,r1,c1);
printf("Enter the elements of matrix 2:\n");
read(mat2,r2,c2);
printf("Elements of matrix 1:\n");
display(mat1,r1,c1);
printf("Elements of matrix 2:\n");
display(mat2,r2,c2);
printf("\nProduct of two matrices\n");
mul(mat1,mat2,r1,r2,c2);
getch();
}

DEPT OF CS TSBAS, JAMKHANDI Page 25


C Programming Lab
***************************OUTPUT************************
Enter the no of rows and columns of matrix:
3
2
Enter the elements of matrix 2:
1
2
3
4
5
6
Enter the elements of matrix 2:
1
2
3
4
5
6
Elements of matrix 1:
1 2 3
4 5 6
Elements of matrix 2:
1 2
3 4
5 6
Product of two matrices
22 28
49 64

DEPT OF CS TSBAS, JAMKHANDI Page 26


C Programming Lab
Prog 6: Write a C Program to read a string and to find the
number of alphabets, digits, vowels, consonants, spaces and
special characters.

#include<stdio.h>
#include<conio.h>
#include<ctype.h>
void main()
{
char str[100];
int v=0,c=0,d=0,s=0,i;
clrscr();
printf("Enter the full string:");
fgets(str,sizeof(str),stdin);
for(i=0;str[i]!='\0';i++)
{
str[i]=tolower(str[i]);
if(str[i]=='a'||str[i]=='e'||str[i]=='i'||str[i]=='o'
|| str[i]=='u')
{
v++;
}
else if(str[i]>='a' &&str[i]<='z')
{
c++;
}
else if(str[i]>='0'&&str[i]<='9')
{
d++;
}
else if(str[i]==' ')
{
s++;
}
}
printf("No of Vowels=%d\nNo of consonants=%d\nNo of
Digits=%d\nNo of space=%d\n",v,c,d,s);
getch();

}
DEPT OF CS TSBAS, JAMKHANDI Page 27
C Programming Lab
***************************OUTPUT************************
Enter the full string: Tungal School of Basic and Applied
Sciences Jamkhandi 5873
01
No of Vowels=17
No of consonants=29
No of Digits=6
No of space=8

DEPT OF CS TSBAS, JAMKHANDI Page 28


C Programming Lab
Prog 7: Write a C Program to Reverse a String.

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char str[100];
clrscr();
printf("Enter the String:");
scanf("%s",&str);
printf("Reversed string of %s = %s\n",str,strrev(str));
getch();
}
***************************OUTPUT************************
Enter the String: TUNGAL
Reversed string of LAGNUT = LAGNUT

DEPT OF CS TSBAS, JAMKHANDI Page 29


C Programming Lab
Prog 8: Write a C Program to Swap Two Numbers.
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
clrscr();
printf("Enter the values of a and b:\n");
scanf("%d%d",&a,&b);
printf("Before swapping\na=%d\nb=%d\n",a,b);
a=a+b;
b=a-b;
a=a-b;
printf("After swapping\na=%d\nb=%d\n",a,b);
getch();
}
***************************OUTPUT************************
Enter the values of a and b:
123
456
Before swapping
a=123
b=456
After swapping
a=456
b=123

DEPT OF CS TSBAS, JAMKHANDI Page 30


C Programming Lab
Prog 9: Write a C Program to demonstrate student structure to
read & display records of n students.
#include<stdio.h>
#include<conio.h>
struct student
{
char name[20],regno[20],course[20];
int perc;
}s[10];
void main()
{
int n,i;
clrscr();
printf("Enter the no of students:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter the Name of %d Student:",i+1);
scanf("%s",&s[i].name);
printf("Enter the Reg no of %d Student:",i+1);
scanf("%s",&s[i].regno);
printf("Enter the Course of %d Student:",i+1);
scanf("%s",&s[i].course);
printf("Enter the marks of %d Student:",i+1);
scanf("%d",&s[i].perc);
}
printf("\nSl.No\tName\t\tReg No\tCourse\tPercentage\n");
for(i=0;i<n;i++)
{

printf("%d\t%s\t\t%s\t%s\t%d\n",i+1,s[i].name,s[i].regno,s
[i].course,s[i].perc);
}
getch();
}

DEPT OF CS TSBAS, JAMKHANDI Page 31


C Programming Lab
***************************OUTPUT************************
Enter the Name of 1 Student:ABHI
Enter the Reg no of 1 Student:BCA01
Enter the Course of 1 Student:BCA
Enter the marks of 1 Student:98
Enter the Name of 2 Student:SINU
Enter the Reg no of 2 Student:MBBS01
Enter the Course of 2 Student:MBBS
Enter the marks of 2 Student:99
Enter the Name of 3 Student:VAISHU
Enter the Reg no of 3 Student:BECS01
Enter the Course of 3 Student:BECS
Enter the marks of 3 Student:87
Enter the Name of 4 Student:RACHU
Enter the Reg no of 4 Student:SCI01
Enter the Course of 4 Student:B.Sc
Enter the marks of 4 Student:89
Enter the Name of 5 Student:SONU
Enter the Reg no of 5 Student:MCA01
Enter the Course of 5 Student:MCA
Enter the marks of 5 Student:98

Sl.No Name Reg No Course Percentage


1 ABHI BCA01 BCA 98
2 SINU MBBS01 MBBS 99
3 VAISHU BECS01 BECS 87
4 RACHU SCI01 B.Sc 89
5 SONU MCA01 MCA 98

DEPT OF CS TSBAS, JAMKHANDI Page 32


C Programming Lab
Prog 10: Write a C Program to demonstrate the difference
between structure & union.
#include <stdio.h>
#include <string.h>
#include<conio.h>
struct ExampleStruct
{
int intVar;
float floatVar;
char str[20];
};

union ExampleUnion {
int intVar;
float floatVar;
char str[20];
};

void main()
{
struct ExampleStruct s;
union ExampleUnion u;
clrscr();
s.intVar = 42;
s.floatVar = 3.14;
strcpy(s.str, "Hello, World!");
u.intVar = 42;
u.floatVar = 3.14;
strcpy(u.str, "Hello, World!");
printf("Structure values:\n");
printf("intVar: %d\n", s.intVar);
printf("floatVar: %.2f\n", s.floatVar);
printf("str: %s\n", s.str);
printf("\nUnion values (after setting str):\n");
printf("intVar: %d\n", u.intVar);
printf("floatVar: %.2f\n", u.floatVar);
printf("str: %s\n", u.str);
getch();
}

DEPT OF CS TSBAS, JAMKHANDI Page 33


C Programming Lab
***************************OUTPUT************************
Structure values:
intVar: 42
floatVar: 3.14
str: Hello, World!

Union values (after setting str):


intVar: 25928
floatVar: 1143139122437582510000000000.00
str: Hello, World!

DEPT OF CS TSBAS, JAMKHANDI Page 34


C Programming Lab
Prog 11: Write a C Program to find the roots of quadratic
equation (demonstration of else if ladder)

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<math.h>
void main()
{
float A,B,C,root1,root2,real,img,disc;
clrscr();
printf("Enter the Values of A, B & C\n");
scanf("%f%f%f",&A,&B,&C);
if(A==0||B==0||C==0)
{
printf("Error!!! Roots cannot be determined\n");
}
else
{
disc=B*B-4*A*C;
if(disc<0)
{
printf("Imaginary Roots\n");
real=-B/(2*A);
img=sqrt(abs(disc))/(2*A);
printf("Root1:%f+i %f\n",real,img);
printf("Root2:%f-i%f\n",real,img);
}
else if(disc>0)
{
printf("Imaginary Roots\n");
root1=(-B+sqrt(disc))/(2*A);
root2=(-B-sqrt(disc))/(2*A);
printf("Root1:%f\n",root1);
printf("Root2:%f\n",root2);
}
else
{
printf("Imaginary Roots\n");
root1=-B/(2*A);
root2=root1;
DEPT OF CS TSBAS, JAMKHANDI Page 35
C Programming Lab
printf("Root1:%f\n",root1);
printf("Root2:%f\n",root2);
}
}
getch();
}
***************************OUTPUT************************
Output 1:
Enter the Values of A, B & C
3
2
1
Imaginary Roots
Root1:-0.333333+i 0.471405
Root2:-0.333333-i0.471405

Output 2:
Enter the Values of A, B & C
1
2
1
Imaginary Roots
Root1:-1.000000
Root2:-1.000000

Output 3:
Enter the Values of A, B & C
3
5
2
Imaginary Roots
Root1:-0.666667
Root2:-1.000000

DEPT OF CS TSBAS, JAMKHANDI Page 36

You might also like