Professional Documents
Culture Documents
Operators
PRACTICAL 1
Write a program that will output this passage by Michael singer. Make sure your output
looks exactly as shown here (including spacing, line brakes, punctuation, and the title and
author). Use Required Escape Sequence and ASCII value.
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
printf("%c %c %c %c %c %c %c %c %c %c %c %c %c %c %c %c %c %c %c %c %c %c %c %c %c %c %c %c %c
%c %c %c %c %c %c %c %c\n",1,4,3,1,4,3,1,4,3,1,4,3,1,4,3,1,4,3,1,4,3,1,4,3,1,4,3,1,4,3,1,4,3,1,4,3,1);
printf("%c \"If you are resisting something, you are feeding it. %c\n",4,4);
printf("%c \tAny energy you fight, you are feeding it. %c\n",3,3);
printf("%c \t\tIf you are pushing something away, %c\n",1,1);
printf("%c \t\t\tYou are inviting to stay.\"by Michael Singer %c\n",4,4);
printf("%c %c %c %c %c %c %c %c %c %c %c %c %c %c %c %c %c %c %c %c %c %c %c %c %c %c %c %c %c
%c %c %c %c %c %c %c %c",1,4,3,1,4,3,1,4,3,1,4,3,1,4,3,1,4,3,1,4,3,1,4,3,1,4,3,1,4,3,1,4,3,1,4,3,1);
getch();
}
OUTPUT :
CSPIT(CE) Page 1
PRACTICAL 2
In a town, the percentage of men is 52. The percentage of total literacy is 48. If total
percentage of literate men is 35 of the total population, write a program to find the total
number of illiterate men and women if the population of the town is 80,000.
Write Algorithms and Flowchart of this program.
INPUT :
#include<stdio.h>
#include<conio.h>
void main()
{
long int tp=80000,lm,lw,im,iw,tm,tw,tl;
clrscr();
tm=52*tp/100;
printf("The total of men is %ld. \n",tm);
tw=48*tp/100;
printf("The total of women is %ld. \n",tw);
tl=48*tp/100;
printf("The total of literate is %ld. \n",tl);
lm=35*tp/100;
printf("The total literate men is %ld. \n",lm);
lw=tl-lm;
printf("The total literate women is %ld. \n",lw);
im=tm-lm;
printf("The total illiterate men is %ld. \n",im);
iw=tw-lw;
printf("The total illiterate women is %ld. \n",iw);
CSPIT(CE) Page 2
printf("The total number of illiterate men and women is %ld and %ld",im,iw);
getch();
}
OUTPUT :
ALGORITHMS :
Step 1 : Start
Step 5 : End
CSPIT(CE) Page 3
FLOW CHART:
Start
tp=80000,lm,lw,im,iw,tm,tw,tl
tm=52*tp/100, tw=48*tp/100
tl=48*tp/100, lm=35*tp/100
lw=tl-lm, im=tm-lm
iw=tw-lw
Output
The total number of tp,lm,lw,im,iw,tm,tw,tl
End
CSPIT(CE) Page 4
Knowledge Required: I/O Operations , Decision Making and Branching,
Decision Making and Looping
PRACTICAL 3
While purchasing certain items, a discount of 10% is offered if the quantity purchased is
more than 1000.If quantity and price per item are input through the keyboard, write a
program to calculate the total expenses. Use Simple If statement.
INPUT :
#include<stdio.h>
void main()
{
floatq,ppi,te;
printf("Enter the quantity: ");
scanf("%f",&q);
te=q*ppi;
if(q>1000)
te=te*0.9;
OUTPUT :
CSPIT(CE) Page 5
PRACTICAL 4
Given three points (x1, y1), (x2, y2) and (x3, y3), write a program to check if all the three
points fall on one straight line. Use fabs() function of < maths.h> header file. Use if…else
statement.
#include<stdio.h>
void main()
{
double x1,x2,x3,y1,y2,y3,s1,s2;
printf("Enter the first point: ");
scanf("%lf %lf",&x1,&y1);
printf("Enter the second point: ");
scanf("%lf %lf",&x2,&y2);
s1=(y2-y1)/(x2-x1);
s2=(y3-y2)/(x3-x2);
if(s1==s2)
printf("The three points are in a straught line.");
else
printf("The three points are not in a straught line.");
}
OUTPUT :
CSPIT(CE) Page 6
PRACTICAL 5
If the three sides of a triangle are entered through the keyboard, write a program to check
whether the triangle is valid or not. The triangle is valid if the sum of two sides is greater
than the largest of the three sides. Use nested if…else statement. Write Algorithms and
Flowchart of this program.
INPUT :
#include<stdio.h>
void main()
{
float s1,s2,s3,Sum,Large;
if(s1>s2)
{
if(s1>s3)
{
Large=s1;
Sum=s2+s3;
}
else
{
Large=s3;
Sum=s1+s2;
}
}
else
{
if(s2>s3)
CSPIT(CE) Page 7
{
Large=s2;
Sum=s1+s3;
}
else
{
Large=s3;
Sum=s1+s2;
}
}
printf("The largest side of triangle is %d \n",(int)Large);
if(Sum>Large)
{
printf("The triangle is valid.");
}
else
{
printf("The triangle is invalid.");
}
}
OUTPUT :
CSPIT(CE) Page 8
PRACTICAL 6
Write a Program that receives month and date of birth as input and prints the
corresponding Zodiac Sign based on the following table: Use Else…if Ladder.
INPUT :
#include<stdio.h>
void main()
{
int d,m;
if(d>31||m>12||d<=0||m<=0)
{
printf("Invalid input.");
}
CSPIT(CE) Page 9
else if((d>=21&&m==3)||(d<=19&&m==4))
{
printf("Your zodiac sign is Aries.");
}
else if((d>=20&&m==4&&d<31)||(d<=20&&m==5))
{
printf("Your zodiac sign is Taurus.");
}
else if((d>=21&&m==5)||(d<=20&&m==6))
{
printf("Your zodiac sign is Gemini.");
}
else if((d>=21&&m==6&&d<31)||(d<=22&&m==7))
{
printf("Your zodiac sign is Cancer.");
}
else if((d>=23&&m==7)||(d<=22&&m==8))
{
printf("Your zodiac sign is Leo.");
}
else if((d>=23&&m==8)||(d<=22&&m==9))
{
printf("Your zodiac sign is Virgo.");
}
else if((d>=23&&m==9&&d<31)||(d<=22&&m==10))
{
printf("Your zodiac sign is Libra.");
}
else if((d>=23&&m==10)||(d<=21&&m==11))
{
printf("Your zodiac sign is Scorpio.");
}
else if((d>=22&&m==11&&d<31)||(d<=21&&m==12))
{
printf("Your zodiac sign is Sagittarius.");
}
else if((d>=22&&m==12)||(d<=19&&m==1))
{
printf("Your zodiac sign is Capricorn.");
}
else if((d>=20&&m==1)||(d<=18&&m==2))
{
CSPIT(CE) Page 10
printf("Your zodiac sign is Aquarius.");
}
else if((d>=19&&m==2&&d<29)||(d<=20&&m==3))
{
printf("Your zodiac sign is Pisces.");
}
else
{
printf("The value is invalid.");
}
}
OUTPUT :
CSPIT(CE) Page 11
PRACTICAL 7
Write a program using Conditional Operators to determine whether Year entered through
the keyboard is a leap year or not. A year occurring once every four years, which has 366
days including 29 February is called a leap year. (Hint: The year is Leap if it is fully
divisible by 100 & 400 else the year is leap if it is fully divisible by 4).
INPUT :
#include<stdio.h>
int main()
{
int year;
return 0;
}
OUTPUT :
CSPIT(CE) Page 12
PRACTICAL 8
If the ages of Ram, Shyam and Ajay are input through the keyboard, write a program to
determine the youngest of the three. Use Nested Switch Statement.
#include<stdio.h>
void main()
{
int r,s,a;
switch(r<s)
{
case 1:
{
switch(r<a)
{
case 1:
printf("Ram is the youngest.");
break;
case 0:
printf("Ajay is the youngest.");
break;
}
}
break;
case 0:
{
switch(s<a)
{
case 1:
CSPIT(CE) Page 13
printf("Shyam is the youngest.");
break;
case 0:
printf("Ajay is the youngest.");
break;
}
}
break;
}
}
OUTPUT :
CSPIT(CE) Page 14
PRACTICAL 9
INPUT :
#include<stdio.h>
void main()
{
inti=1,sum=0,n;
start:
if(i<=n)
{
sum=sum+(i*i);
i++;
goto start;
}
printf("sum = %d",sum);
}
OUTPUT :
CSPIT(CE) Page 15
PRACTICAL 10
Write a program to find number of 1’s in the binary representation of a number. For
example, the binary of 5 is 0000 0000 0000 0101 contains two 1’s. Use for loop. Use Bitwise
AND & and Shift Right >> operator.
INPUT :
#include<stdio.h>
void main()
{
intx,count=0;
printf("enter the value of x: ");
scanf("%d",&x);
for( ;x;x=x>>1)
{
if((x&1)==1)
{
count++;
}
}
printf("count= %d",count);
}
OUTPUT :
CSPIT(CE) Page 16
PRACTICAL 11
An “Armstrong number” is an n-digit number that is equal to the sum of the nth powers of
its individual digits. For example, 153 is an Armstrong number because it has 3 digits and
13+ 53+33 =153. Similarly, 1634 is an Armstrong number because it has 4 digits and 14+
64+34 + 44 = 1634. Write a program to find whether the entered number is Armstrong or
not using While Loop. Write Algorithms and Flowchart of this program.
INPUT :-
#include<stdio.h>
#include<math.h>
void main()
{
intn,or;
double length=0,rem,sum=0;
or=n;
while(n > 0)
{
length++;
n = n / 10;
}
n=or;
while(n>0)
{
rem = n % 10;
sum = sum + pow(rem,length);
n = n / 10;
}
if(sum == or)
{
CSPIT(CE) Page 17
printf("the number is an ARMSTRONG NUMBER.");
}
else
{
printf("the number is not an ARMSTRONG NUMBER.");
}
OUTPUT :
ALGORITHMS :-
Step 1: Start
Step 5: Initialize n = or
CSPIT(CE) Page 18
FLOW CHART :-
Start
n>0
True False
Length++;
n = or;
n = n/10
n>0
True False
rem = n % 10
sum = sum + pow ( rem , length )
n = n / 10
End
CSPIT(CE) Page 19
PRACTICAL 12
Write a menu driven program which has following options:
1. Prime or not
2. Perfect number or not
3. Factorial of a number
4. Exit
Use do...while statement so that the menu is displayed at least once. Also use Switch
statement.
#include<stdio.h>
#include<stdlib.h>
void main()
{
int choice;
inti,n,flag=0,sum=0,f=1;
do
{
printf("Enter 1 for PRIME \nEnter 2 for PERFECT \nEnter 3 for FACTORIAL \nEnter 4 for EXIT");
printf("\nEnter the choice: ");
scanf("%d",&choice);
switch(choice)
{
case 1:
{ printf("Enter the value of n: ");
scanf("%d",&n);
for(i=2;i<n;i++)
{
if((n%i)==0)
{
flag=1;
CSPIT(CE) Page 20
break;
}
}
if(flag==0)
printf("Number is Prime.");
else
printf("Number is not Prime.");
}
break;
case 2:
{ inti,n,sum=0;
printf("Enter the value of n: ");
scanf("%d",&n);
for(i=1;i<n;i++)
{
if((n%i)==0)
{
sum=sum+i;
}
}
if(sum==n)
{
printf("The number is a PERFECT number.");
}
else
{
printf("The number is not a PERFECT number.");
}
}
break;
case 3:
{ inti,n,f=1;
printf("Enter the value of n: ");
scanf("%d",&n);
for(i=1,f=1;i<=n;i++)
{
f=f*i;
CSPIT(CE) Page 21
}
printf("Factorial of %d is %d.",n,f);
}
break;
case 4:
exit(0);
break;
default:
printf("Please enter valid input");
break;
}
}while(1);
}
Output :-
CSPIT(CE) Page 22
PRACTICAL 13
Write a program to print the following pattern using Nested for loop.
(i) 1
1 0
1 0 1
1 0 1 0
1 0 1 0 1
#include<stdio.h>
int main()
{
inti,n,j;
for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
{
if(j%2==1)
{
printf("1 ");
}
else
{
printf("0 ");
}
}
printf("\n");
}
return 0;
}
CSPIT(CE) Page 23
Output :-
CSPIT(CE) Page 24
(ii)
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
INPUT :-
#include<stdio.h>
int main()
{
inti,j,k,n,s;
for(i=1;i<=n;i++)
{
for(k=1;k<=(n-i);k++)
{
printf(" ");
}
for(j=1;j<=i;j++)
{
printf("%d ",j);
}
printf("\n");
}
return 0;
}
CSPIT(CE) Page 25
Output :-
CSPIT(CE) Page 26
(iii) 1 2 3 4 5
2 3 4 5
3 4 5
4 5
5
INPUT
#include<stdio.h>
int main()
{
inti,j,n;
for(i=1;i<=n;i++)
{
for(j=i;j<=n;j++)
{
printf("%d ",j);
}
printf("\n");
}
return 0;
}
Output :-
CSPIT(CE) Page 27
(iv) A B C D E
1 2 3 4
A B C
1 2
A
INPUT :-
#include<stdio.h>
int main()
{
inti,j,n,k,num=1;
charch=65;
j=k;
for(i=1;i<=n;i++)
{
for(k=1;k<i;k++)
{
printf(" ");
}
if(k%2==0)
CSPIT(CE) Page 28
{
for(j=i;j<=n;j++)
{
printf("%d ",num);
num++;
}
num=1;
}
else
{
for(j=i;j<=n;j++)
{
printf("%c ",ch);
ch++;
}
ch=65;
}
printf("\n");
}
return 0;
}
Output :-
CSPIT(CE) Page 29
(v) 1
1 2 1
1 2 3 2 1
1 2 3 4 3 2 1
INPUT :-
#include<stdio.h>
int main()
{
inti,j,k,n,s;
for(i=1;i<=n;i++)
{
for(k=1;k<=(n-i);k++)
{
printf(" ");
}
for(j=1;j<=i;j++)
{
printf("%d ",j);
}
for(s=i-1;s>=1;s--)
CSPIT(CE) Page 30
{
printf("%d ",s);
}
printf("\n");
}
return 0;
}
Output :-
CSPIT(CE) Page 31
PRACTICAL 14
Write a program for a match-stick game between the computer and a user. Your Program
should ensure that the computer always wins. Rules for the games are as follows:
#include<stdio.h>
int main()
{
int m=21,p,c;
while(1)
{
printf("\nNo of Matches left = %d \n",m);
printf("\nPick up 1,2,3 or 4 Matches\n");
scanf("%d",&p);
if(p>4||p<1)
{
continue;
}
m=m-p;
printf("\nNo of Matches left = %d \n",m);
c=5-p;
printf("\nOut of which computer picked up %d",c);
m=m-c;
if(m==1)
{
CSPIT(CE) Page 32
printf("\n No of Matches left = %d \n",m);
printf("\n You lost the game");
break;
}
}
return 0;
}
Output :-
CSPIT(CE) Page 33
Knowledge Required: Arrays & Strings
PRACTICAL 15
INPUT :-
#include<stdio.h>
#define MAX 5
void bubble_sort(int a[]);
int main(void)
{
int a[MAX];
bubble_sort(a);
CSPIT(CE) Page 34
return 0;
}
void bubble_sort(int a[])
{
int t,x;
t = a[j];
a[j] = a[j+1];
a[j+1] = t;
x = 1;
}
}
if (!x)
{
break;
}
}
}
Output :-
CSPIT(CE) Page 35
PRACTICAL 16
INPUT :-
#include<stdio.h>
int main()
{
int a[10][10], b[10][10], c[10][10], i, j, k;
int sum = 0;
CSPIT(CE) Page 36
}
for (i=0;i<3;i++)
{
for (j=0;j<3;j++)
{
for (k=0;k<=2;k++)
{
sum = sum + a[i][k] * b[k][j];
}
c[i][j] = sum;
}
}
CSPIT(CE) Page 37
Output :-
CONCLUSION:
CSPIT(CE) Page 38
PRACTICAL 17
Write a program to find whether two given strings are permutations of each
other. A string str1 is a permutation of str2 if all the characters in str1 appear
the same number of times in str2 and str2 is of the same length as str1. For
example if two strings are listen and silent the answer is Yes.
INPUT :-
#include<conio.h>
#include<string.h>
void main()
{
int n1,n2,i,j=0,flag,k;
char str1[50],str2[50],temp;
clrscr();
flushall();
printf("\n\t\t\t\tPractical 21");
printf("\nEnter string 1 : ");
gets(str1);
printf("\nEnter string 2 : ");
gets(str2);
n1=strlen(str1);
n2=strlen(str2);
if(n1==n2)
{
for(i=0;i<n1-1;i++)
{
for(j=0;j<n1-i-1;j++)
{
if(str1[j]>str1[j+1])
{
temp=str1[j];
str1[j]=str1[j+1];
str1[j+1]=temp;
}
}
}
CSPIT(CE) Page 39
for(i=0;i<n2-1;i++)
{
for(j=0;j<n2-i-1;j++)
{
if(str2[j]>str2[j+1])
{
temp=str2[j];
str2[j]=str2[j+1];
str2[j+1]=temp;
}
}
}
for(k=0;k<n1;k++)
{
if(str1[k]==str2[k])
{
flag=1;
}
Else
{
flag=0;
}
}
if(flag==1)
printf("\nThey are permutations");
else
printf("\nThey are not permutations");
}
else
{
printf("\nThe strings are not permutations of each other.");
}
printf(“\n\t\t\t\t\tMade By : Jay Pandya”);
}
}
CSPIT(CE) Page 40
Output :-
CONCLUSION:
string str1 is a permutation of str2 if all the characters in str1 appear the same number of times in
str2 and str2 is of the same length as str1.
CSPIT(CE) Page 41
PRACTICAL 18
INPUT :-
#include<stdio.h>
#include<string.h>
int main()
{
int i,j,count;
char str[25][25],temp[25];
printf("Enter number of strings:");
scanf("%d",&count);
CSPIT(CE) Page 42
puts(str[i]);
}
return 0;
}
Output :-
CONCLUSION:
CSPIT(CE) Page 43
Knowledge Required: User-Defined Functions, Structures & Union
PRACTICAL 19
Write a Program to print the table of Squares and Cubes of 1 to 10. The
Program uses the following four Function: Functions Category printline():
draws the line using ‘ - ’ character. Function with No Arguments, No return
type printnum() : prints number, square and cube. Function with Arguments
,No Return type square() : computes square of a number. Function with
Arguments, with Return Type cube() : computes cube of a number. Function
with Arguments, with Return Type
INPUT :-
#include<stdio.h>
void printline(void);
void printnum(int);
int square(int);
int cube(int);
main()
{
int n;
printline();
printf("NUMBER | SQUARE | CUBE |");
printf("\n");
printline();
for(n=1;n<=10;n++)
{
square(n);
cube(n);
printnum(n);
}
printline();
}
void printline(void)
{
CSPIT(CE) Page 44
int i;
for(i=1;i<=25;i++)
{
printf("-");
}
printf("\n");
}
void printnum(n)
{
printf("%d \t| %d \t| %d \t| \n",n,square(n),cube(n));
}
int square(int n)
{
int sqr;
sqr=n*n;
return sqr;
}
int cube(int n)
{
int cube;
cube=(n*n)*n;
return cube;
}
Output :-
CSPIT(CE) Page 45
PRACTICAL 20
INPUT :-
#include<stdio.h>
void factor(int);
void prime(int);
main()
{
int n;
printf("enter the value of n:");
scanf("%d",&n);
factor(n);
}
void factor(int a)
{
int i;
for(i=1;i<a;i++)
{
if(a%i==0)
{
printf("factor is %d\n",i);
prime(i);
}
}
}
void prime(int a)
CSPIT(CE) Page 46
{
if(a%2!=0)
{
printf("prime num is %d\n",a);
}
else
{
printf("enter num %d is not prime \n",a);
}
}
Output :-
CONCLUSION:
find the factors of a number and check whether the factors are prime or
not.
CSPIT(CE) Page 47
PRACTICAL 21
INPUT :-
#include<stdio.h>
int fibo(int n);
void main()
{
int n,i=0,c;
printf("enter value of number:");
scanf("%d",&n);
for(c=0;c<n;c++)
{
printf("%d\n",fibo(i));
i++;
}
}
int fibo(int n)
{
if(n==0)
return 0;
if(n==1)
return 1;
return(fibo(n-1)+fibo(n-2));
}
Output :-
CSPIT(CE) Page 48
PRACTICAL 22
Write a Program to compute the standard Deviation of N Numbers using Arrays & Function. Use
pow() and sqrt() function of maths.h header file.
INPUT :-
#include<stdio.h>
#include<conio.h>
#include<math.h>
float mean(float[],int);
void store(float[],int);
void sub_square(float[],int,float);
void main()
{
int i,n;
float a[50],b[50],meanval,dev;
CSPIT(CE) Page 49
scanf("%d",&n);
store(a,n);
meanval=mean(a,n);
printf("\n\nThe mean is : %f",meanval);
for(i=0;i<n;i++)
{
b[i]=a[i];
}
sub_square(b,n,meanval);
dev=sqrt(mean(b,n));
printf("\nThe value of standard deviation is : %2f",dev);
printf("\n\n\t\t\t\t\tMade By : Jay Pandya");
}
float mean(float c[],int n)
{
int i;
float sum=0;
for(i=0;i<n;i++)
{
sum=sum+c[i];
}
return (sum/n);
}
void store(float a[],int n)
{
int i,sum=0;
printf("\nEnter the elements : ");
for(i=0;i<n;i++)
{
scanf("%f",&a[i]);
}
}
void sub_square(float c[],int n,float val)
{
int i=0;
for(i=0;i<n;i++)
{
CSPIT(CE) Page 50
c[i]=c[i]-val;
c[i]=c[i]*c[i];
}
}
Output :-
CONCLUSION:
CSPIT(CE) Page 51
PRACTICAL 23
INPUT :-
#include<stdio.h>
#include<string.h>
main()
{
char str[20];
CSPIT(CE) Page 52
Output :-
CONCLUION:
reverse a string using Recursive Function and check whether it is palindrome or not.
CSPIT(CE) Page 53
PRACTICAL 24
Write a Program to find the upper triangle in the given matrix. Consider the following 4 x
4 Matrix.
If all the elements denoted by X are non-zero, then the matrix has upper triangle. For the
upper triangle, all the elements of principle diagonal and above must be non – zero. Pass
two dimensional arrays to the function.
XXXX
0 XXX
0 0 XX
0 0 0 X
#include<stdio.h>
void main()
{
int m[4][4],i,flag,j;
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
printf("Enter the value of element m[%d][%d]",i+1,j+1);
scanf("%d",&m[i][j]);
}
}
printf("enter the matrix is:");
printf("\n");
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
printf("%d",m[i][j]);
printf(" ");
}
printf("\n");
CSPIT(CE) Page 54
}
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
if(m[i][j]==0)
{
flag=0;
}
}
}
for(i=0;i<4;i++)
{
for(i=0;i<4;i++)
{
if(m[i][j]!=0)
{
flag=0;
}
}
}
if(flag==0)
{
printf("Given matrix is not upper triangular.");
}
else
{
printf("Given matrix is upper triangular.");
}
}
CSPIT(CE) Page 55
Output :-
CONCLUSION:
CSPIT(CE) Page 56
PRACTICAL 25(1)
Create a Structure called library to hold accession number, title of the book, author name,
price of the book and flag indicating whether the book is issued or not. (flag = 1 if the book
is issued, flag = 0 otherwise). Write a program to enter data of one book and display the
data. Write this same program with Union also.
INPUT :-
#include<stdio.h>
struct library
{
int acc_no;
char tittle[20];
char author_name[20];
int book_price;
int flag;
}s[3];
int main()
{
int i;
CSPIT(CE) Page 57
for(i=0;i<3;i++)
{
printf("%d\n",s[i].acc_no);
printf("\n");
puts(s[i].tittle);
printf("\n");
puts(s[i].author_name);
printf("\n");
printf("%d\n",s[i].book_price);
if(s[i].flag==1)
{
printf("your book is issued");
}
else
{
printf("your book is not issued");
}
}
}
Output :-
CSPIT(CE) Page 58
PRACTICAL 25(2)
Create a Structure called library to hold accession number, title of the book, author name,
price of the book and flag indicating whether the book is issued or not. (flag = 1 if the book
is issued, flag = 0 otherwise). Write a program to enter data of one book and display the
data. Write this same program with Union also.
INPUT :-
#include<stdio.h>
union library
{
int acc_no;
char tittle[20];
char author_name[20];
int book_price;
int flag;
}s[3];
int main()
{
int i;
CSPIT(CE) Page 59
for(i=0;i<3;i++)
{
printf("%d\n",s[i].acc_no);
printf("\n");
puts(s[i].tittle);
printf("\n");
puts(s[i].author_name);
printf("\n");
printf("%d\n",s[i].book_price);
if(s[i].flag==1)
{
printf("your book is issued");
}
else
{
printf("your book is not issued");
}
}
}
Output :-
CSPIT(CE) Page 60
PRACTICAL 26
Define a structure called Result for students. Structure will have members
like Roll number, marks for three subjects and total of three subjects. Write a
program to enter data for 5 students and display the merit list of students. Use
Array of Structures. For example, if Roll No and marks of three subjects of
each student are entered through the keyboard, the output should look like
the following:
INPUT :-
#include<stdio.h>
struct student
{
int r;
int s1;
int s2;
int s3;
int total;
}a[50],temp[50];
void main()
{
int n,i,j;
printf("Enter number of students:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("enter roll num of student:");
scanf("%d",&a[i].r);
CSPIT(CE) Page 61
scanf("%d",&a[i].s3);
a[i].total=a[i].s1+a[i].s2+a[i].s3;
}
printf("\n \n MERIT LIST -->\n \n");
printf("Roll no Sub 1 Sub 2 Sub 3 Total\n");
for(j=0;j<n-1;j++)
{
for(i=0;i<n-1;i++)
{
if(a[i].total<a[i+1].total)
{
temp[i]=a[i];
a[i]=a[i+1];
a[i+1]=temp[i];
}
}
}
for(i=0;i<n;i++)
{
printf("%7d",a[i].r);
printf("%8d",a[i].s1);
printf("%9d",a[i].s2);
printf("%10d",a[i].s3);
printf("%11d",a[i].total);
printf("\n");
}
}
Output :-
CSPIT(CE) Page 62
PRACTICAL 27
INPUT :-
#include<stdio.h>
struct outer
{
char emp_name[20];
char emp_designation[20];
char dep_name[20];
int basicpay;
struct inner_product
{
int dA;
int hRA;
int City_A;
}i;
}o;
int main()
{
int t;
printf("enter an employ name,\ndesignation,\ndepartment name,\nbasic payment");
gets(o.emp_name);
fflush(stdin);
gets(o.emp_designation);
fflush(stdin);
gets(o.dep_name);
fflush(stdin);
scanf("%d",&o.basicpay);
CSPIT(CE) Page 63
fflush(stdin);
Output :-
Conclusion:
program to read and display information of salary of an employee using Structure within a
Structure.
CSPIT(CE) Page 64
PRACTICAL 28
Define a structure named Date that contains three member’s day, month and
Year. Write a program that compares two given dates. If the dates are equal,
then display message as “Equal” otherwise “Unequal”. Write a function
Check_Date to check whether the entered .
INPUT :-
#include<stdio.h>
#include<conio.h>
}
else if(D1.day<1 || D1.day>31)
{
printf("\nInvalid Date");
printf(“\n\t\t\t\t\tMade by : Jay Pandya”);
getch();
exit(0);
}
else if(D1.year<1000 || D1.year>9999)
{
printf("\nInvalid Date");
printf(“\n\t\t\t\t\tMade by : Jay Pandya”);
getch();
exit(0);
}
}
void main()
{
struct Date D1,D2;
CSPIT(CE) Page 65
clrscr();
printf("\n\t\t\t\tPractical 34\n\n");
printf("\nEnter date 1 : \n");
printf("\nEnter Day : ");
scanf("%d",&D1.day);
printf("\nEnter Month : ");
scanf("%d",&D1.month);
printf("\nEnter Year : ");
scanf("%d",&D1.year);
printf("\n%d/%d/%d",D1.day,D1.month,D1.year);
Check_Date(D1);
printf("\nThe entered date 1 is valid \n\n");
printf("\nEnter date 2 : \n");
printf("\nEnter Day : ");
scanf("%d",&D2.day);
printf("\nEnter Month : ");
scanf("%d",&D2.month);
printf("\nEnter Year : ");
scanf("%d",&D2.year);
Check_Date(D2);
printf("\nThe entered date 2 is valid \n\n");
Output :-
CSPIT(CE) Page 66
Knowledge Required: Pointers, File Management, Dynamic Memory
Allocation
PRACTICAL 29
Write a program to read the marks of 10 students for the subject CE141
Computer concepts and Programming and computes the number of students
in categories FAIL, PASS, FIRST CLASS and DISTINCTION using Pointers
and Arrays. Marks Categories 70 or Above DISTINCTION 69 to 60 FIRST
CLASS 59 to 40 PASS Below 40 FAIL For example, if following marks of 10
students are entered: 34 56 78 98 12 31 67 75 91 23 Then the output
should be DISTINCTION 4 FIRST CLASS 1 PASS 1 FAIL 4
INPUT :-
#include<stdio.h>
void main()
{
int a[100],i,n,d=0,fc=0,pass=0,f=0;
int *p;
for(i=0,p=&a;i<n;i++,p++)
{
printf("\nEnter marks of student num %d:",i+1);
scanf("%d",p);
}
for(i=0,p=&a;i<n;i++,p++)
{
if(*p>=70)
{
d++;
}
else if(*p<=69 && *p>=60)
CSPIT(CE) Page 67
{
fc++;
}
else if(*p<=59 && *p>=40)
{
pass++;
}
else
{
f++;
}
}
printf("\nDistinction=%d \nFirst class=%d \npass=%d \nFail=%d",d,fc,pass,f);
}
Output :-
Conclusion:
CSPIT(CE) Page 68
PRACTICAL 30
INPUT :-
#include <stdio.h>
#include <string.h>
int main()
{
char a[1000], b[1000];
strcat(a, b);
return 0;
}
Output :-
CSPIT(CE) Page 69
PRACTICAL 31
Write a program that counts the number of ‘e’ in the following array of
pointers to strings. (Array of Pointers)
char *s[ ] = { "We will teach you how to...", "Move a mountain", "Level a
building", "erase the past", "Make a million", "...all through C!" } ;
INPUT :-
#include<stdio.h>
#include<string.h>
void main()
{
char *s[ ] = { "We will teach you how to...", "Move a mountain", "Level a building", "erase the
past", "Make a million", "...all through C!" } ;
char *p;
int cnt=0,i;
for(i=0,p=s[i];*p;p++,i++)
{
if(*p=='e');
{
cnt++;
}
}
printf("number of letters are %d",cnt);
}
Output :-
Conclusion:
program that counts the number of ‘e’ in the following array of pointers to strings.
(Array of Pointers)
CSPIT(CE) Page 70
PRACTICAL 32(1)
Write a program to swap two values using function and illustrate the
difference between call by value and call by address. (Pointers as Function
Arguments)
INPUT :-
#include <stdio.h>
int main()
{
int x, y;
swap(x, y);
return 0;
}
temp = b;
b = a;
a = temp;
printf(“Values of a and b is %d %d\n”,a,b);
}
CSPIT(CE) Page 71
Output :-
Conclusion:
swap two values using function and illustrate the difference between call
by value and call by address.
CSPIT(CE) Page 72
PRATICAL-32(2)
Write a program to swap two values using function and illustrate the
difference between call by value and call by address. (Pointers as Function
Arguments)
INPUT :-
#include <stdio.h>
int main()
{
int x, y;
swap(&x, &y);
return 0;
}
temp = *b;
*b = *a;
*a = temp;
}
Output :-
CSPIT(CE) Page 73
Parameters Call by value Call by reference
Alteration of value Does not allow you to make any Allows you to
make
changes in the actual variables. Changes in the
value
of variables
CSPIT(CE) Page 74
PRACTICAL-33
CSPIT(CE) Page 75
r = t;
while(*s!='\0')
{ *t=*s;
t++;
s++;
}
*t='\0';
return(r);
}
3. GATE CS 2019
#include<stdio.h>
int r()
{
int static num=7;
return num--;
}
int main()
{
for(r();r();r())
{
printf("%d ",r());
};
return 0;
}
Output :-
(1)
CSPIT(CE) Page 76
(2)
(3)
CONCLUSION:
CSPIT(CE) Page 77
PRACTICAL-34
INPUT :-
#include<stdio.h>
struct engine_parts
{
char sno[4];
int year;
int qnt;
}p[5],*ptr;
void main()
{
int i;
ptr=p;
for(i=0;i<3;i++)
{
printf("enter serial num:");
gets(ptr->sno);
fflush(stdin);
printf("\n enter year of manufacturing:");
scanf("%d",&ptr->year);
fflush(stdin);
printf("\n Enter quantity of manufacturing:");
scanf("%d",&ptr->qnt);
fflush(stdin);
printf("\n");
ptr++;
}
ptr=&p[0];
printf("\n parts of info. \n");
for(i=0;i<3;i++)
{
CSPIT(CE) Page 78
printf("\n serial num=%s\n",ptr->sno);
printf("\n manufacturing year=%d\n",ptr->year);
printf("\n manufacturing quantity=%d\n",ptr->qnt);
ptr++;
printf("\n");
}
}
Output :-
CONCLUSION:
to swap two values using function and illustrate the difference between call by
value and call by address.
CSPIT(CE) Page 79
PRACTICAL-35
Write a Program to read data from the keyboard. Write it to the file called
input.txt and again read the same data from the input.txt file, copy the
contents of input.txt to output.txt file and display the contents on the output
screen. Use fopen(), fclose(), getc() and putc() , ferror() and feof() functions.
Also use NULL pointer for Invalid file name.
INPUT :-
#include<stdio.h>
main()
{
FILE *f1,*f2,*f3,*f4;
char c;
char ch;
char fn[20];
printf("Input data:");
f1=fopen("input.txt","w");
while((c=getchar())!=EOF)
{
putc(c,f1);
}
fclose(f1);
printf("Output data:");
f1=fopen("input.txt","r");
f2=fopen("output.txt","w");
while((c=getc(f1))!=EOF)
{
putc(c,f2);
printf("%c",c);
}
fclose(f1);
fclose(f2);
openf:
printf("enter file name:");
gets(fn);
if((f3=fopen(fn,"r"))==NULL)
{
CSPIT(CE) Page 80
printf("can not open this file\n");
printf("Try again\n");
goto openf;
}
F4=fopen("f4.txt","w");
ch=getc(f4);
if(ferror(f4))
printf("error in reading file\n");
while(ch=getc(f1))
{
if(feof(f3))
{
printf("\n end of file\n");
break;
}
else
{
putc(ch,f4);
printf("%c",ch);
}
}
fclose(f3);
fclose(f4);
}
Output :-
CONCLUSION:
Program to read data from the keyboard. Write it to the file called input.txt and
again read the same data from the input
CSPIT(CE) Page 81
PRACTICAL-36
Write a C program that illustrates how to write into a file using putw()
function and how to read the same file using getw() function.Use fopen(),
fclose(), getw() and putw() functions.
INPUT :-
#include<stdio.h>
void main()
{
FILE *f;
int n,num,i;
f=fopen("abc.txt","w");
printf("\n Enter n= ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\n enter number %d" ,i+1);
scanf("%d",&num);
putw(num,f);
}
fclose(f);
f=fopen("abc.txt","r");
printf("\n data of abc file:\n");
while((num=getw(f))!=EOF)
{
printf("%d\n",num);
}
fclose(f);
}
CSPIT(CE) Page 82
Output :-
CONCLUSION:
program that illustrates how to write into a file using putw() function and how to
read the same file using getw() function.Use fopen(), fclose(), getw() and putw()
functions.
CSPIT(CE) Page 83
PRACTICAL-37
Write a program to print last n characters of a file. Use the function fopen(),
fclose(), ftell(), fseek() and rewind().
INPUT :-
#include<stdio.h>
void main()
{
FILE *fp;
char ch;
long int n,m;
fp=fopen("input.txt","r");
if(fp==NULL)
printf("file can not be open");
else
{
printf("enter value of n to read lats n char:");
scanf("%1d",&n);
fseek(fp,-n,2);
m=ftell(fp);
printf("\n file pointer location is %1d\n",m);
while((ch=getc(fp))!=EOF)
{
printf("%c",ch);
}
m=ftell(fp);
printf("\n current file pointer position: %1d",m);
}
rewind(fp);
m=ftell(fp);
printf("\n After rewind file pointer position:%1d",m);
fclose(fp);
}
CSPIT(CE) Page 84
Output :-
CONCLUSION:
CSPIT(CE) Page 85
PRACTICAL-38
INPUT :-
#include<stdio.h>
main()
{
FILE *fp;
char s[20];
int t;
fp=fopen("xyz.txt","w");
printf("enter string and num:");
fscanf(stdin,"%s %d",s,&t);
fprintf(fp,"%s %d",s,t);
fclose(fp);
fp=fopen("xyz.txt","r");
fscanf(fp,"%s %d",s, &t);
fprintf(stdout,"%s %d\n",s,t);
fclose(fp);
}
Output :-
CONCLUSION:
program to illustrate the use of functions fscanf() and fprintf().
CSPIT(CE) Page 86
PRACTICAL-39
Two files Data1.txt and Data2.txt contains list of integers. Write a program to
produce file Data3.txt which holds as merged list of these two list. Use
command line argument to specify the file name.
INPUT :-
#include<stdio.h>
#include<conio.h>
void main(int argc, char *argv[])
{
FILE *f, *f1, *f2;
int i;
int a[20], b[10];
clrscr();
CSPIT(CE) Page 87
fclose(f1);
fclose(f2);
fclose(f);
f = fopen(argv[1],"r");
for(i=0;i<20;i++)
{
fscanf(f,"%d",&a[i]);
printf("%d\t",a[i]);
}
fcloseall();
getch();
}
Output :-
CONCLUSION:
CSPIT(CE) Page 88
PRACTICAL-40
Write a program to enter N numbers into array and find average. Enter the
size of the array through keyboard. (Dynamic Array). Use malloc () to allocate
memory and use free() to free the memory after the use.
INPUT :-
#include<stdio.h>
#include<conio.h>
void main()
{
int *p;
float *avg;
int i,n;
printf("\nEnter size of array: ");
scanf("%d",&n);
p=(int *)calloc(n,sizeof(int));
avg=(float *)malloc(sizeof(float));
*avg=0;
for(i=0;i<n;i++)
{
printf("\nEnter the number %d: ",i+1);
scanf("%d",p);
*avg=*avg+*p; p++;
}
*avg=*avg/n;
printf("\n Average =%f",*avg);
free(p);
free(avg);
getch();
}
Output :-
CSPIT(CE) Page 89
CONCLUSION:
program to enter N numbers into array and find average. Enter the size of the array
through keyboard.
CSPIT(CE) Page 90
PRACTICAL 41
INPUT :-
#include<stdio.h>
#include<conio.h>
void main()
{
char *a;
a=(char *)calloc(10,sizeof(char));
printf("\nEnter a string: ");
scanf("%s",a);
a=(char *)realloc(a,20*sizeof(char));
printf("\nString after reallocation of memory for array: ");
puts(a);
free(a);
getch();
Output :-
CSPIT(CE) Page 91