0% found this document useful (0 votes)
6 views

C Lab Manual

......bnnnnnn

Uploaded by

yogeshask925
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

C Lab Manual

......bnnnnnn

Uploaded by

yogeshask925
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 42

University of Mysore

Department of Computer Science and Applications


Government Science College
(Autonomous)
Hassan-573201

LAB MANUAL
Course Name : C-Programming Lab
Year : I Year BCA / I Year B.Sc
Semester : I Semester

Prepared by
Mamatha C MCA
Dept of Computer Science and Applications
Govt Science College(Autonomous)
Hassan
List of programs
PART A
1. Write a C Program to read radius of a circle and to find area and
circumference
2. Write a C Program to read three numbers and find the biggest of three
3. Write a C Program to demonstrate library functions in math.h
4. Write a C Program to generate n primes
5. Write a C Program to read a number, find the sum of the digits,
reverse the number and check it for palindrome
6. Write a C Program to read numbers from keyboard continuously
till the user presses 999 and to find the sum of only positive
numbers
7. Write a C Program to read percentage of marks and to display
appropriate message (Demonstration of else-if ladder)
8. Write a C Program to find the roots of quadratic equation
(demonstration of switch-case statement)
9. Write a C Program to remove Duplicate Element in a single dimensional
Array
10. Program to perform addition and subtraction of Matrices

PART B
1. Write a C Program to find the length of a string without using built in
function
2. Write a C Program to demonstrate string functions.
3. Write a C Program to check a number for prime by defining isprime( )
function
4. Write a C Program to read, display and to find the trace of a square matrix
5. Write a C Program to read, display and multiply two matrices using functions
6. Write a C Program to read a string and to find the number of
alphabets, digits, vowels, consonants, spaces and special
characters.
7. Write a C Program to Reverse a String using Pointer
8. Write a C Program to Swap Two Numbers using Pointers
9. Write a C Program to demonstrate student structure to read &
display records of n students.
10. Write a C Program to demonstrate the difference between structure & union.
PART A
1.Write a C Program to read radius of a circle and to find area and circumference

ALGORITHM : To find area and circumference of a circle


Step 1: Start
Step 2: Input radius
Step 3: let pi=3.14
Step 4: area=pi*radius*radius
Step 5: circumference=2*pi*radius
Step 6: print area
Step 7: print circumference
Step 8: Stop

PROGRAM IMPLEMENTATION:
/* Program to read radius of a circle and to find area and
circumference */
#include<stdio.h>
#include<conio.h>
void main()
{
int r;
float PI=3.14,area,circum;
clrscr();
printf(“Enter the radius of a circle\n”);
scanf(“%d”,&r);
area=PI*r*r;
circum=2*PI*r;
printf(“Area of a circle=%f\n”,area);
printf(“Circumference of a circle =%f\n”,circum);
getch();
}

OUTPUT:
2.Write a C Program to read three numbers and find the biggest of three

ALGORITHM : Biggest of 3 numbers


Step 1: Start
Step 2: Read 3 numbers a,b,c
Step 3: if a>b and a>c then print “a is biggest” and goto Step 6
Step 4: else if b>c , then print “b is biggest” and goto Step 6
Step 5: else print “c is biggest” and goto Step 6
Step 6: Stop

PROGRAM IMPLEMENTATION :
/* 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 any 3 integer numbers\n”);
scanf(“%d%d%d”,&a,&b,&c);
if(a>b && a>c)
printf(“%d is biggest\n”,a);
else if(b>c)
printf(“%d is biggest\n”,b);
else
printf(“%d is biggest\n”,c);
getch();
}
3. Write a C Program to demonstrate library functions in math.h
ALGORITHM: To find squareroot, power using library function
math.h
Step 1: Start
Step 2: Input x
Step 3: Calculate SqRt, Powr
SqRt=sqrt(x);
Powr=pow(x,3);
Step 4: print SqRt
Step 5: print Powr
Step 6: Stop
PROGRAM IMPLEMENTATION:
/* C Program to demonstrate library functions using math.h */
#include<stdio.h>
#include<math.h>
void main()
{
int x;
float SqRt,Powr;
clrscr();
printf("Enter any integer value for x\n");
scanf("%d",&x);
SqRt=sqrt(x);
printf("The Square Root of %d is %f\n",x,SqRt);
Powr=pow(x,3); //Returns the value of x power 3(x*x*x)
printf("The value of %d power 3 is %f\n",x,Powr);
getch();
}

OUTPUT
4.Write a C Program to generate n primes

ALGORITHM : To generate n prime numbers


Step 1: Start
Step 2: Set count=1,i=2
Step 3: while count <= n
Do
Flag=0
For j=2 to i/2
Do
If i%j = 0 then
Flag=1
Break
Fi
Done
If flag=0
Print “i” // Prints all prime numbers upto n
Count++
Fi
i++
done
Step 4 : Stop

PROGRAM IMPLEMENTATION
/* C Program to generate n primes */
#include<stdio.h>
#include<conio.h>
#include<math.h>
int main()
{
int n,count=1,flag,i=2,j;
clrscr();
printf("enter the value of n\n");
scanf("%d",&n);
printf("The first %d prime numbers are:\n",n);
while(count<=n)
{
flag=0;
for(j=2;j<=i/2;j++)
{
if(i%j == 0)
{
flag=1;
break;
}
}
if(flag==0)
{
printf("%d\n",i);
count++;
}
i++;
}
getch();
return 0;
}
OUTPUT:
5.Write a C Program to read a number, find the sum of the digits, reverse
the number and check it for palindrome

ALGORITHM: Find the sum of the digits, reverse the number and check it
for palindrome.
Step 1: Start
Step 2 : Read a valid integer number ‘n’
Step 3: Set rev=0,count=0,sum=0
Step 4: Let temp= n
Step 5: do
Rem= n%10
Rev=rev*10+rem
Sum=sum+rem
Count=count+1
N=n/10
While n>0
done
print “The given number(temp)”
print “Reverse of a given number(rev)”
print “Sum of digits of the given number(sum)”
print “The number of digits(count)”
if t=rev then
print “The given number is palindrome”
else
print “The given number is not palindrome”
fi
Step 6: Stop
PROGRAM IMPLEMENTATION
/* 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 n,rev=0, count=0,sum=0,t,rem;
clrscr();
printf("Enter the number\n");
scanf("%d",&n);
t=n;
do
{
rem=n%10;
rev=rev*10+rem;
sum=sum+rem;
count=count+1;
n=n/10;
}
while(n>0);
printf("The given number is = %d\n",t);
printf("Reverse of a given number is = %d\n",rev);
printf("Sum of digits of the given number = %d\n",sum);
printf("Number of digits = %d\n",count);
if(t==rev)
printf("The given number is palindrome\n");
else
printf("The given number is not a palindrome\n");
getch();
}

OUTPUT:
6.Write a C Program to read numbers from keyboard continuously till the
user presses 999 and to find the sum of only positive numbers

ALGORITHM : Read numbers from keyboard continuously till the user


presses 999 and to find the sum of only positive numbers
Step 1: Start
Step 2: Read the size of the array ‘n’
Step 3: Read n numbers to the array ‘a[]’
Step 4: Set sum=0, count=0
Step 5: for i=0 to n
Do
Read numbers one by one
Count++
If a[i] = 999 // Termination condition
Then
Print “The condition of termination occured”
Break
Fi
Done
For i=0 to count
Do
If a[i] > 0 then
Sum += a[i]
Fi
Done
Print “The sum of all positive integer numbers (sum)”
Step 6: Stop

PROGRAM IMPLEMENTATION :
/* 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>
int main()
{
int a[50],sum=0,j,n,i,count=0;
clrscr();
printf("Input the size of the arry \n");
scanf("%d",&n);
printf("Input the numbers:\n");
for(i=0; i<n; i++)
{
scanf("%d",&a[i]);
count++;
if(a[i]==999)
{
printf("\n The condition of termination occured\n");
break;
}
}
for(i=0; i<count; i++)
{
if(a[i] > 0)
{
sum += a[i];
}
}
printf("\nThe sum of positive integer numbers is : %d",sum);
printf("\n");
getch();
return 0;
}
OUTPUT:
7.Write a C Program to read percentage of marks and to display
appropriate message (Demonstration of else-if ladder)

ALGORITHM : To read percentage of marks and to display appropriate


message
Step 1: Start
Step 2: Read percentage of marks(per)
Step 3: if per>0 and per<=100
Then
If per>=75 and per<=100
Then Print “It’s a Distinction!!”
Else if per>=60 and per<75
Then print ”It’s a FIRST CLASS!!”
Else if per>=35 and per<60
Then print “It’s a SECOND CLASS!!”
Else print “FAIL!!”
Fi
Else
Print “INVALID PERCENTILE”
Step 4: Stop

PROGRAM IMPLEMENTATION
/* C Program to read percentage of marks and to display appropriate
message (Demonstration of else-if ladder) */

#include<stdio.h>
void main()
{
float per;
clrscr();
printf("Enter a valid percentage of marks\n");
scanf("%f",&per);
if(per>0 && per<=100)
{
if(per>=75 && per<=100)
printf("It's a DISTINCTION!!!\n");
else if(per>=60 && per<75)
printf("It's a FIRST CLASS!!!\n");
else if(per>=35 && per<60)
printf("It's a SECOND CLASS!!!\n");
else
printf("FAIL!!!!\n");
}
else
{
printf("INVALID PERCENTILE!!!\n");
getch();
}
getch();
}

OUTPUT:
8.Write a C Program to find the roots of quadratic equation
(demonstration of switch-case statement)

ALGORITHM: To find the roots of quadratic equation


Step 1: Start
Step 2: Read values for a,b,c
Step 3: discriminant= (b*b)-4*a*c
Switch(discriminant>0)
Do
Case 1: root1=(-b +sqrt(discriminant))/2*a
root2=(-b -sqrt(discriminant))/2*a
print “Roots are real and distinct”
case 0:
switch(discriminant < 0)
do
case 1: root1 = root2 = -b / (2 * a)
imaginary = sqrt(-discriminant) / (2 * a)
print"Two distinct complex roots exists”
case 0: root1 = root2 = -b / (2 * a);
print"Two equal and real roots exists”
done
done
Step 4: Stop

PROGRAM IMPLEMENTATION:
/* C Program to find the roots of quadratic equation*/
#include<stdio.h>
#include<math.h> /* Used for sqrt() */
int main()
{
float a, b, c;
float root1, root2, imaginary;
float discriminant;
clrscr();

printf("Enter values of a, b, c of quadratic equation (ax^2 + bx + c):\n");


scanf("%f%f%f", &a, &b, &c);

/* Calculate discriminant */
discriminant = (b * b) - (4 * a * c);

/* Compute roots of quadratic equation based on the nature of discriminant */


switch(discriminant > 0)
{
case 1:
/* If discriminant is positive */
root1 = (-b + sqrt(discriminant)) / (2 * a);
root2 = (-b - sqrt(discriminant)) / (2 * a);
printf("Two distinct and real roots: %.2f and %.2f", root1, root2);
getch();
break;

case 0:
/* If discriminant is not positive */
switch(discriminant < 0)
{
case 1:
/* If discriminant is negative */
root1 = root2 = -b / (2 * a);
imaginary = sqrt(-discriminant) / (2 * a);

printf("Two distinct complex roots exists: %.2f + i%.2f and %.2f - i%.2f",
root1, imaginary, root2, imaginary);
getch();
break;

case 0:
/* If discriminant is zero */
root1 = root2 = -b / (2 * a);

printf("Two equal and real roots exists: %.2f and %.2f", root1, root2);
getch();
break;
}
}

return 0;
}

OUTPUT:
9.Write a C Program to remove Duplicate Element in a single dimensional Array

ALGORITHM : To remove Duplicate Element in a single dimensional Array


Step 1: Start
Step 2: Read array elements
Step 3: for i=0 to n
Do
For j=i+1 to n
Do
If a[i]=a[j]
For k=j to n
Do
A[k]=a[k+1]
Done
n- -
j- -
fi
done
done
print “Array elements after deletion of duplicate elements”
Step 4: Stop

PROGRAM IMPLEMENTATION:
/* C Program to remove Duplicate Element in a single dimensional Array*/
#include<stdio.h>
#include<conio.h>
int main()
{
int a[20],i,j,k,n;
clrscr();
printf("Enter the size of the array\n");
scanf("%d",&n);
printf("Enter the elements of the array\n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(a[i]==a[j])
{
for(k=j;k<n;k++)
{
a[k]=a[k+1];
}
n--;
j--;
}
}
}
printf("Array elements after deletion of duplicate elements are:\n");
for(i=0;i<n;i++)
{
printf("%d\n",a[i]);
}
getch();
return 0;
}

OUTPUT:
10.Program to perform addition and subtraction of Matrices

ALGORITHM: Addition and subtraction of Matrices


Step 1: Start
Step 2: Read array elements for a[][] b[][]
Step 3: if m=p and n=q
Then
For i=0 to m
do
for j=0 to n
do
c[i][j]=0;
c[i][j]=a[i][j]+b[i][j];
done
done
for i=0 to m
do
for j=0 to n
do
d[i][j]=0;
d[i][j]=a[i][j]-b[i][j];
done
done
fi
else
print “Addition and Subtraction is impossible”
Step 4: Stop

PROGRAM IMPLEMENTATION:
/* C Program to perform addition and subtraction of Matrices */
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10][10],b[10][10],c[10][10],d[10][10],i,j,m,n,p,q;
clrscr();
printf("Enter the order of the first matrix\n");
scanf("%d%d",&m,&n);
printf("Enter the order of the second matrix\n");
scanf("%d%d",&p,&q);
if(m==p && n==q)
{
printf("Enter the elements of first matrix\n");
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
printf("Enter the elements of second matrix\n");
for(i=0;i<p;i++)
for(j=0;j<q;j++)
scanf("%d",&b[i][j]);
printf("The first matrix is:\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("%d\t",a[i][j]);
}
printf("\n");
}
printf("The second matrix is:\n");
for(i=0;i<p;i++)
{
for(j=0;j<q;j++)
{
printf("%d\t",b[i][j]);
}
printf("\n");
}

for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
c[i][j]=0;
c[i][j]=a[i][j]+b[i][j];
}
}

for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
d[i][j]=0;
d[i][j]=a[i][j]-b[i][j];
}
}
printf("Addition of two matrices is:\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("%d\t",c[i][j]);
}
printf("\n");
}
printf("Subtraction of two matrices is:\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("%d\t",d[i][j]);
}
printf("\n");
}
}
else
printf("Addition and Subtraction of two matrices cannot be performed\n");
getch();
}

OUTPUT:
PART B
1. Write a C Program to find the length of a string without using built in function

ALGORITHM : To find the length of a string without using built in function


Step 1 : Start
Step 2: Read String
Step 3: for i=0 to ‘\0’ ;
Step 4: print “The length of the string”
Step 5: Stop

PROGRAM IMPLEMENTATION:
/* C Program to find the length of a string without using built in function */
#include<stdio.h>
int main()
{
char s[]= "I am doing good";
int i;
clrscr();
for(i=0;s[i]!='\0';i++);
printf("Length of the String is %d\n",i);
getch();
return 0;
}

OUTPUT:
2. Write a C Program to demonstrate string functions.

ALGORITHM : To demonstrate string functions(puts,gets,strlen,strcat,strcpy)


Step 1 : Start
Step 2 : Read s1,s2
Step 3 : l1= strlen(s1)
Print “String1 length”
Strlen(s2)
Print “String2 length”
Strcat(s1,s2)
Print “The concatenated string”
Strcpy(s1,s2)
Print “The copied string”
Step 4 : Stop

PROGRAM IMPLEMENTATION:
/* C Program to demonstrate string functions*/
#include<stdio.h>
#include<string.h>
void main()
{
char s1[100],s2[100];
int l1,l2;
clrscr();
puts("Enter 1st string");
gets(s1);
puts("Enter 2nd string");
gets(s2);
l1=strlen(s1);
printf("Length of the string S1 is %d\n", l1);
l2=strlen(s2);
printf("Length of the string S2 is %d\n",l2);
strcat(s1,s2);
printf("The concatenated String is: %s\n",s1);
strcpy(s1,s2);
printf("The copied string is : %s\n",s1);
getch();
}

OUTPUT:
3.Write a C Program to check a number for prime by defining isprime( ) function

ALGORITHM : To check a number for prime by defining isprime( ) function


Step 1 : Start
Step 2 : Read any integer number n
Step 3 : Divide n with (n – 1 to 2)
Step 4 : if n is divisible by any value, it is not prime
Step 5 : else it is prime
Step 6 : Stop

PROGRAM IMPLEMENTATION:
/* C Program to check a number for prime by defining isprime( ) function*/

#include<stdio.h>
#include<conio.h>
void main()
{
int n,flag=0;
clrscr();
printf("Enter any integer number\n");
scanf("%d",&n);
flag=isPrime(n);
if(flag==0)
printf("\n %d is a Prime number\n",n);
else
printf("\n %d is not a Prime number\n",n);
getch();
}
int isPrime(int n)
{
int i;
for(i=2;i<=n/2;i++)
{
if(n%i != 0)
continue;
else
return 1;
}
return 0;
}
OUTPUT:
4. Write a C Program to read, display and to find the trace of a square matrix

ALGORITHM : To read, display and to find the trace of a square matrix


Step 1 : Start
Step 2 : Read the values for a matrix a[i][j]
Step 3 : Calculate Trace= Sum of all a11,a22,a33…aii
Step 4 : Print Trace
Step 5 : Stop

PROGRAM IMPLEMENTATION:
/* C Program to read, display and to find the trace of a square matrix */
#include<stdio.h>
void main()
{
int a[10][10],m,i,j,trace;
clrscr();
printf("Enter the order of a matrix\n");
scanf("%d",&m);
printf("Enter the elements for a square matrix\n");
for(i=0;i<m;i++)
for(j=0;j<m;j++)
scanf("%d",&a[i][j]);
printf("The square matrix is:\n");
for(i=0;i<m;i++)
{
for(j=0;j<m;j++)
{
printf("%d\t",a[i][j]);
}
printf("\n");
}
trace=0;
for(i=0;i<m;i++)
trace=trace+a[i][i];
printf("\n Trace of the given square matrix = %d\n",trace);
getch();
}
OUTPUT:
5. Write a C Program to read, display and multiply two matrices using functions

ALGORITHM : To read, display and multiply two matrices using functions


Step 1 : Start
Step 2 : Enter the order for 1st matrix a[i][j]
Step 3 : Enter the order for 2nd matrix b[i][j]
Step 4 : Enter elements for 1st matrix a[i][j]
Step 5 : Enter elements for 2nd matrix b[i][j]
Step 6 : Print matrix a[i][j]
Step 7 : Print matrix b[i][j]
Step 8 : Set a loop up to row
Step 9 : Set an inner loop up to the column
Step 10 : Set another inner loop up to the column
Step 11 : Multiply a[i][j] and b[i][j] and store product in c[i][j]
Step 12 : Print the resultant matrix c[i][j]
Step 13 : Stop

PROGRAM IMPLEMENTATION:
/* C Program to read, display and multiply two matrices using functions */
#include<stdio.h>
void readMat(int a[][10],int m,int n)
{
int i,j;
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
}
void mulMat(int a[][10],int b[][10],int prod[][10],int m,int n,int q)
{
int i,j,k;
for(i=0;i<m;i++)
for(j=0;j<q;j++)
prod[i][j]=0;
for(i=0;i<m;i++)
{
for(j=0;j<q;j++)
{
for(k=0;k<n;k++)
{
prod[i][j]=prod[i][j]+a[i][k]*b[k][j];
}
}
}
}
void dispMat(int a[][10],int m,int n)
{
int i,j;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("%d\t",a[i][j]);
}
printf("\n");
}
}
int main()
{
int a[10][10],b[10][10],prod[10][10],m,n,p,q;
clrscr();
printf("Enter order for 1st matrix\n");
scanf("%d%d",&m,&n);
printf("Enter order for 2nd matrix\n");
scanf("%d%d",&p,&q);
if(n!=p)
{
printf("Multiplication of 2 matrices is impossible\n");
getch();
exit();
}
printf("Enter elements for 1st matrix\n");
readMat(a,m,n);
printf("Enter elements for 2nd matrix\n");
readMat(b,p,q);
printf("1st matrix is:\n");
dispMat(a,m,n);
printf("2nd matrix is:\n");
dispMat(b,p,q);
mulMat(a,b,prod,m,n,q);
printf("The product matrix is:\n");
dispMat(prod,m,q);
getch();
return 0;
}
OUTPUT:
6. Write a C Program to read a string and to find the number of alphabets,
digits, vowels, consonants, spaces and special characters.

ALGORITHM : To read a string and to find the number of alphabets, digits,


vowels, consonants, spaces and special characters.
Step 1: Start
Step 2 : Read a String
Step 3 : Check with ASCII values from a-z and A-Z and count alphabets,vowels
and consonants
Step 4 : Check with the spaces present in the string and count the spaces
Step 5 : Check with the special characters and count them
Step 6 : Check with the digits present in the string and count them
Step 7 : Print the count of alphabets, digits,vowels,consonants,spaces and special
characters.
Step 8 : Stop

PROGRAM IMPLEMENTATION:
/* C Program to read a string and to find the number of alphabets, digits,
vowels, consonants, spaces and special characters */
#include<stdio.h>
#include<string.h>
int main()
{
char s[100];
int i, vow=0,cons=0,digit=0,space=0,symb=0,alph=0;
clrscr();
printf("Enter the string\n");
gets(s);
for(i=0;s[i]!='\0';i++)
{
if(s[i]=='a' || s[i]=='e' || s[i]=='i' || s[i]=='o' || s[i]== 'u' || s[i]=='A' || s[i]=='E' || s[i]=='I'
|| s[i]=='O' || s[i]=='U')
vow++;
else if((s[i]>='a' && s[i]<='z') || (s[i]>='A' && s[i]<='Z'))
cons++;
else if(s[i]>='0' && s[i]<='9')
digit++;
else if(s[i]==' ')
space++;
else
symb++;
if((s[i]>=65 && s[i]<=90) || (s[i]>=97 && s[i]<=122))
alph++;
}
printf("No. of alphabets=%d\n",alph);
printf("No. of Vowels=%d\n",vow);
printf("No. of Consonents=%d\n",cons);
printf("No. of digits=%d\n",digit);
printf("No. of Spaces=%d\n",space);
printf("No. of Symbols=%d\n",symb);
getch();
return 0;
}

OUTPUT:
7.Write a C Program to Reverse a String using Pointer

ALGORITHM: To Reverse a String using Pointer


Step 1 : Start
Step 2 : Read a String
Step 3 : Find the length of a string
Step 4 : Swap the position of array elements using for loop
Step 5 : Store the swapped positions
Step 6 : Print the reverse string
Step 7 : Stop

PROGRAM IMPLEMENTATION:
/* C Program to Reverse a String using Pointer */
#include<stdio.h>
#include<string.h>
void strRev(char *s)
{
int l,i;
char *start,*end,temp;
l=strlen(s);
start=s;
end=s;
for(i=0;i<l-1;i++)
end++;
for(i=0;i<l/2;i++)
{
temp=*end;
*end=*start;
*start=temp;
start++;
end--;
}
}
int main()
{
char s[100];
clrscr();
printf("Enter a string\n");
gets(s);
strRev(s);
printf("The reverse of a string is :%s\n",s);
getch();
return 0;
}
OUTPUT:
8.Write a C Program to Swap Two Numbers using Pointers

ALGORITHM : To Swap Two Numbers using Pointers


Step 1 :Start
Step 2 : Read 2 integer variables
Step 3 : Set value of x to temp
Step 4 : Set value of y to x
Step 5 : Set the value of temp to x
Step 6 : Print the numbers after swapping
Step 7 : Stop

PROGRAM IMPLEMEMNTATION:
/* C Program to Swap Two Numbers using Pointers*/
#include<stdio.h>
void main()
{
int x,y,*a,*b,temp;
clrscr();
printf("Enter any 2 integer numbers\n");
scanf("%d%d",&x,&y);
printf("Before swapping\nx=%d\ty=%d\n",x,y);
a=&x;
b=&y;
temp=*b;
*b=*a;
*a=temp;
printf("After swapping\nx=%d\ty=%d\n",x,y);
getch();
}

OUTPUT:
9.Write a C Program to demonstrate student structure to read & display
records of n students.

ALGORITHM : To demonstrate student structure to read & display records


of n students.
Step 1: Start
Step 2: Create a structure having variables (Name,RollNumber,Semester,
Marks)
Step 3: Read value for n
Step 4: Read student’s information (Name, RollNumber, Semester, Marks)
using for loop
Step 5: Print Student’s information using for loop
Step 6: Stop

PROGRAM IMPLEMENTATION:
/* C Program to demonstrate student structure to read & display records
of n students.*/

#include<stdio.h>
#include<conio.h>
struct student
{
char name[100];
int rollno;
int sem;
int marks;
}s[100];
int main()
{
int i,n;
clrscr();
printf("\nEnter the number of students\n");
scanf("%d",&n);
printf("\nEnter student's information one by one:\n");
for(i=0;i<n;i++)
{
printf("Enter name\n");
scanf("%s",s[i].name);
printf("Enter roll number\n");
scanf("%d",&s[i].rollno);
printf("Enter the semester\n");
scanf("%d",&s[i].sem);
printf("Enter percentage of marks obtained\n");
scanf("%d",&s[i].marks);
}
printf("\tTHE STUDENT DETAILS ARE:\n\n");
for(i=0;i<n;i++)
{
printf("\t Name = %s\n",s[i].name);
printf("\t Roll Number = %d\n",s[i].rollno);
printf("\t Semester = %d\n",s[i].sem);
printf("\t Percentage of marks scored = %d\n\n",s[i].marks);
}
getch();
return 0;
}

OUTPUT:
10.Write a C Program to demonstrate the difference between structure &
union

ALGORITHM : To demonstrate the difference between structure & union


Step 1 : Start
Step 2 : Create a structure for ‘student’ having its members
Step 3 : Create a union for ‘students’ having its members
Step 4 : Calculate the size of all the variables present in STRUCTURE and
UNION
Step 5 : Print the size of both
Step 6 : Stop

PROGRAM IMPLEMENTATION:
/* C Program to demonstrate the difference between structure & union */
#include<stdio.h>
struct student
{
int age;
char name[50];
int sem;
float marks;
};
union students
{
int ages;
char names[50];
int semester;
float markss;
};
void main()
{
struct student s;
union students s1;
printf("The size of Student STRUCTURE = %d\n",sizeof(s));
printf("The size of Students UNION = %d\n",sizeof(s1));
getch();
}

OUTPUT:

You might also like