C File
C File
C File
PRACTICAL FILE
ON
PROGRAMMING IN C LANGUAGE
SESSION: 2014-15
SUBMITTED BY :
GUIDED BY:
MADHAVI SAHU
ASSISTANT PROFESSOR
AT
AFFILIATED TO
Pt. RAVISHANKAR SHUKLA UNIVERSITY, RAIPUR (C.G.)
CERTIFICATE
Subject Teacher
Ms. Lalima Choudhary
Date:
Principal
INDEX
S.No.
1.
2.
Name Of Program
Write a program in which you declare variable of all data types
supported by C language get input from user and print the value
of each variable with alignment left, right and column width 10.
for real numbers print their values with two digits right to the
decimal.
Write a program to print all combination of 123?
Write program to generate following pattern
3.
a) A B C D E F G
AB C E F G
AB
FG
A
G
b) 1
12
123
Page
No.
1-4
6-9
c) *
* *
* * *
4.
5.
10
11-14
a
b
c
d
e
f
7.
8.
9.
15-21
22-25
26-27
28-33
Remarks
Program - 1 Write a program in which you declare variable of all data types supported by C
language get input from user and print the value of each variable with alignment left, right and
column width 10. for real numbers print their values with two digits right to the decimal.
CODING:-
#include<stdio.h>
#include<conio.h>
void main()
{
int a;
unsigned int aa;
char b;
unsigned char bb;
long c;
unsigned long cc;
float d;
double e;
long double ee;
clrscr();
printf("\n enter an integer");
scanf("%d",&a);
printf("\ninteger =:%10d",a);
printf("\ninteger =:%-10d",a);
printf("%-10.2f",d);
getch();
}
Output:-
#include<stdio.h>
#include<conio.h>
Void main()
{
int i,j,k;
clrscr();
for(i=1;i<=3;i++)
for(j=1;j<=3;j++)
for(k=1;k<=3;k++)
if(i!=j&&j!=k&&i!=k)
printf(\n %d%d%d,i,j,k);
getch();
}
Output:-
b) 1
c) *
AB C E FG
12
* *
AB
FG
123
* * *
a) CODING:-
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,k;
clrscr();
for(i=1;i<=4;i++)
{
for(j=1;j<=5-i;j++)
printf("%c",(char)(64+j));
for(k=1;k<=2*i-1;k++)
printf(" ");
printf("\b\b");
for(j=j-2;j>=0;j--)
printf("%c",(char)(65+j));
printf("\n");
}
getch();
}
Output:-
b.) CODING:#include<stdio.h>
#include<conio.h>
void main()
{
int i,j;
clrscr();
for(i=1;i<=3;i++)
{
for(j=1;j<=i;j++)
printf("%d",j);
printf("\n");
}
getch();
}
Output:-
c.) CODING:-
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,k;
clrscr();
for(i=1;i<=3;i++)
{
for(j=3;j>=i;j--)
printf(" ",j);
{
for(k=1;k<=i;k++)
printf(" *");
printf("\n");
}
}
getch();
}
Output:-
10
Program-5 Write a program to perform following tasks using switchcase, loop, and
conditional operator.
a) Find factorial of a number.
b) Print Fibonacci series up to n terms and its sum.
c) Print prime number up n terms.
d) Print whether a given year is leap or not.
CODING:#include<stdio.h>
#include<conio.h>
void main()
{
int n,p,f,i;
int num,f1=0,f2=1;
clrscr();
do
{
printf("\n 1. find factorial");
printf("\n 2. fibonacci series and sum");
printf("\n 3. print prime number n terms ");
printf("\n 4. check leap year");
printf("\n 5. exit");
printf("\n enter choice");
scanf("%d",&n);
switch(n)
{
case 1:
printf("\n enter a value to know factorial");
scanf("%d",&n);
11
f=1;
for(i=2;i<=n;i++)
f=f*i;
printf("\n factorial=%d",f);
break;
case 2:
printf("\n how many terms to generate:");
scanf("%d",&num);
p=0;
for(i=1;i<=num;i++)
{
printf("\t %d",f1);
p=p+f1;
f=f1+f2;
f1=f2;
f2=f;
}
printf("\n sum=%d",p);
break;
case 3:
printf("\n enter no. to check primality:");
scanf("%d",&num);
for(i=2;i<num;i++)
{
12
if(num%i==0)
break;
}
if(i>=num)
printf("\n prime");
else
printf("\n not prime");
break;
case 4:
printf("\n enter year to check leap:");
scanf("%d",&num);
(num%100==0)
?
(num%400==0)?printf("\n leap"):printf("not leap")
:
(num%4==0)?printf("\n leap"):printf("not leap");
break;
}
}
while(n!=5);
getch();
}
Output:-
13
14
Program-6 Create a single program to perform following tasks using switch, if else, loop and
a
b
c
d
e
f
CODING:-
#include <stdio.h>
#include <conio.h>
#include <string.h>
void main()
{
char a[80],b[80],s;
int n;
char temp;
char p[30][50];
int i,j=0,k=0;
int v=0,c=0;
do
{
printf("\n1. to reverse string");
printf("\n2. count no. of characters");
printf("\n3. copy one string to other");
printf("\n4. check palindrome");
printf("\n5. count vowels,consonant in each word");
printf("\n6. arrange the alphabets in ascending order");
printf("\n7. exit");
15
i=i-1;
n=i;
i=0;
for(;n>=i;i++,n--)
{
temp=a[i];
a[i]=a[n];
a[n]=temp;
}
printf("\n string is %s",a);
break;
case 2:
printf("\n enter string\n");
scanf("%s",a);
printf("\n string is %s",a);
for(i=0;a[i]!='\0';i++);
16
scanf("%s",a);
for(i=0;a[i]!='\0';i++)
{
b[i]=a[i];
}
b[i]='\0';
case 5:
fflush(stdin);
printf("\n enter a sentence below\n");
gets(a);
17
v=0;
c=0;
for(i=0;i<=j;i++)
{
for(k=0;p[i][k]!='\0';k++)
{
switch(p[i][k])
{
case 'a':
case 'A':
case 'e':
case 'E':
case 'i':
case 'I':
18
case 'o':
case 'O':
case 'u':
case 'U':
v++;
break;
case ' ':
break;
default:
if((p[i][k]))
c++;
}
}
if(v>0||c>0)
{
printf("\n word=%s",p[i]);
printf("\n vowels=%d,consonant=%d",v,c);
}
v=0;
c=0;
}
break;
case 6:
printf("\n enter string:");
scanf("%s",a);
for(n=0;a[n]!='\0';n++);
19
n=n-1;
for(i=0;i<n-1;i++)
for(j=0;j<n-i;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
printf("\n string is %s",a);
break;
}
while (n!=7);
getch();
}
Output:-
20
21
Program-7 Create a single program to perform following tasks using single dimension integer
array:
a. Sort the elements.
b. Search for presence of particular value in array element using linear search.
c. Search for presence of particular value in array element using binary search.
CODING:-
#include <stdio.h>
#include <conio.h>
void main()
{
int a[5],v,low,mid,high;
int i,j,n,temp;
do
{
printf("\n1. to sort");
printf("\n2. to linear search");
printf("\n3. to binary search");
printf("\n4. exit");
printf("\n enter your choice");
scanf("%d",&n);
switch(n)
{
case 1:
printf("\n enter 5 values\n");
for(i=0;i<5;i++)
scanf("%d",&a[i]);
22
for(i=0;i<5-1;i++)
for(j=0;j<5-i-1;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
printf("\n sorted array is\n");
for(i=0;i<5;i++)
printf("\t%d",a[i]);
break;
case 2:
printf("\n enter 5 values\n");
for(i=0;i<5;i++)
scanf("%d",&a[i]);
23
for(low=0,high=5,mid=(low+high)/2;low<=high;mid=(low+high)/2)
{
if(v>a[mid])
low=mid+1;
else if(v<a[mid])
high=mid-1;
else
break;
}
if(low<=high)
printf("\n value found at %d",mid);
else
break;
}while (n!=4);
24
getch();
}
Output:-
25
Program-8 Write a program that read the afternoon day temperature for each day of the month
and then report the month average temperature as well as the days on the days on which hottest
and coolest days occurred.
CODING:-
#include<stdio.h>
#include<conio.h>
void main()
{
float a [31];
float sum=0,avg,hot,cool;
int n,i,hotday,coolday;
clrscr();
printf(\n enter no. of days in current month );
scanf(%d,&n);
printf(\n Enter each day temperature );
for(i=0;i<=n;i++)
scanf(%f,&a[i]);
for(i=0;i<n;i++)
sum=sum+a[i];
avg=sum/n;
hot=a[0];
cool=a[0];
hotday=1;
coolday=1;
for(i=1;i<n;i++)
{
if(hot<a[i])
{
hot=a[i];
26
hotday=i;
}
if(cool>a[i]);
{
cool=a[i];
coolday=i;
}
}
printf(\n average temperature=%f,avg);
printf(\n hottest temperature=%f on day %d,hot,hotday);
printf(\n coolest temperature=%f on day %d,cool,coolday);
getch();
}
output:-
Program-9 Create a single program to perform following tasks using switch, if else, loop and
double dimension integer array of size 3*3:
27
CODING:-
#include <stdio.h>
#include <conio.h>
#include <string.h>
#define row 3
#define col 3
void main()
{
float a[row][col],b[row][col],c[row][col];
int n;
int i,j,k;
float fd,bd;
clrscr();
do
{
printf("\n1. addition of matix");
printf("\n2. subtraction of matrix");
printf("\n3. multiplication of matrix");
printf("\n4. sum of diagonal element of matrix");
printf("\n5. exit");
printf("\n enter your choice");
scanf("%d",&n);
switch(n)
28
{
case 1:
printf("\n enter %d values below\n",row*col);
for(i=0;i<row;i++)
for(j=0;j<col;j++)
scanf("%f",&a[i][j]);
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
c[i][j]=a[i][j]+b[i][j];
}
29
scanf("%f",&a[i][j]);
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
c[i][j]=a[i][j]-b[i][j];
}
break;
case 3:
printf("\n enter %d values below\n",row*col);
for(i=0;i<row;i++)
for(j=0;j<col;j++)
scanf("%f",&a[i][j]);
30
for(j=0;j<col;j++)
scanf("%f",&b[i][j]);
for(i=0;i<row;i++)
{
for(k=0;k<col;k++)
{
c[i][k]=0;
for(j=0;j<col;j++)
c[i][k]=c[i][k]+a[i][j]*b[j][k];
}
}
break;
case 4:
printf("\n enter %d values below\n",row*col);
fd=0;
bd=0;
for(i=0;i<row;i++)
for(j=0;j<col;j++)
scanf("%f",&a[i][j]);
31
fd=0;
bd=0;
for(i=0;i<row;i++)
{
fd=fd+a[i][i];
bd=bd+a[i][2-i];
}
printf("\n forward diagonal=%f",fd);
printf("\n backward diagonal=%f",bd);
break;
}while (n!=5);
}
Output:-
32
33
Program-10 Create a single program to perform following tasks using double dimension
character array of size 5*40:
a) Sorting of string.
b) Finding the largest string.
c) Finding the smallest string.
d) Searching for presence of a string in array.
CODING:-
#include <stdio.h>
#include <conio.h>
#include <string.h>
void main()
{
char a[5][40],s[40];
char temp[40];
int n,i,j;
clrscr();
printf("\n enter five strings below\n");
for(i=0;i<5;i++)
scanf("%s",a[i]);
34
{
if(strcmpi(a[i],s)==0)
{
break;
}
}
if(i==5)
printf("\n string no found");
else
printf("\n string found at index %d",i);
strcpy(temp,a[0]);
for(i=1;i<5;i++)
{
if(strcmpi(a[i],temp)>0)
{
strcpy(temp,a[i]);
}
}
printf("\n smallest string= %s",temp);
strcpy(temp,a[0]);
for(i=1;i<5;i++)
{
if(strcmpi(a[i],temp)<0)
{
strcpy(temp,a[i]);
}
}
printf("\n largest string= %s",temp);
35
for(i=0;i<4;i++)
{
for(j=0;j<4-i;j++)
{
if(strcmpi(a[j],a[j+1])>0)
{
strcpy(temp,a[j]);
strcpy(a[j],a[j+1]);
strcpy(a[j+1],temp);
}
}
}
printf("\n after sorting");
36
Program-11 Write program using the function power (a, b) to calculate the value of a raised to.
37
CODING:-
#include<stdio.h>
#include<conio.h>
void power(int,int);
void main()
{
int a,b;
clrscr();
printf("Enter any number of a & power b:");
scanf("%d%d",&a,&b);
power(a,b);
getch();
}
void power(int a,int b)
{
int i,z;
z=a;
for(i=2;i<=b;i++)
a=a*z;
printf("\nvalue of a raised to b=%d",a);
}
Output:-
38
39
Program-12 Write program to demonstrate difference between static and auto variable.
CODING:-
#include<stdio.h>
#include<conio.h>
void main()
{
void staticdisplay();
void autodisplay();
staticdisplay();
staticdisplay();
staticdisplay();
autodisplay();
autodisplay();
autodisplay();
getch();
}
void staticdisplay()
{
static int a=0;
printf("\n static count =%d",a);
a++;
}
void autodisplay()
{
int a=0;
40
41
Program-13 Write program to demonstrate difference between local and global variable.
CODING:-
#include<conio.h>
#include<stdio.h>
int gb=10;
void main()
{
int gb=30;
void display();
printf("\n value of local gb in main=%d",gb);
printf("\n value of global gb in main=%d",::gb);
::gb=20;
display();
printf("\n value of global gb in main after function display=%d",::gb);
getch();
}
void display()
{
int p=20;
printf("\n value of global variable before alteration made by display= %d",gb);
printf("\n value of local variable=%d",p);
gb=40;
}
Output:-
42
43
Program-14 Write a program to perform following tasks using switchcase, loop, and
conditional operator.
a) Find factorial of a number.
b) Print Fibonacci series up to n terms and its sum.
CODING:-
#include<stdio.h>
#include<conio.h>
void main()
{
int n,p,f,i;
int num,f1=0,f2=1;
clrscr();
do
{
printf("\n 1. find factorial");
printf("\n 2. fibonacci series and sum");
printf("\n enter choice");
scanf("%d",&n);
switch(n)
{
case 1:
printf("\n enter a value to know factorial");
scanf("%d",&n);
f=1;
for(i=2;i<=n;i++)
f=f*i;
printf("\n factorial=%d",f);
44
break;
case 2:
printf("\n how many terms to generate:");
scanf("%d",&num);
p=0;
for(i=1;i<=num;i++)
{
printf("\t %d",f1);
p=p+f1;
f=f1+f2;
f1=f2;
f2=f;
}
printf("\n sum=%d",p);
break;
}
}while(n!=3);
getch();
}
Output:-
45
46
Program-15 Write a program to perform following tasks using switch case, loop and
function .
a) find factorial of a number
b) print Fibonacci series upto ten terms and its sum
c) Print natural series upto n terms and its sum
CODING:-
#include<stdio.h>
#include<conio.h>
long fact(int n)
{
long f=1;
int i;
for(i=2;i<=n;i++)
f=f*i;
return f;
}
long fib(int n)
{
long p=0,f1=0,f2=1,f;
int i;
for(i=1;i<=n;i++)
{
printf(\t %d,f1);
p=p+f1;
f=f1+f2;
f1=f2;
f2=f;
47
}
return p;
}
long natu(int n)
{
float f=0;
int i;
for ( i=1;i<=n;i++)
{
printf(\t %d,i);
f=f+i;
}
return f;
}
void main()
{
int n,m,I;
float x,s,mf,term,q;
long f,p;
do
{
printf(\n 1. find factorial );
printf(\n 2. Fibonacci series and sum );
printf(\n 3. natural series and sum );
printf("\n 4. Exit ");
printf(\n enter the choice);
scanf(%d,&n);
48
switch(n)
{
case 1:
printf(\n Enter a value to know factorial );
f=fact(n);
printf(\n factorial =%ld,f);
break;
case 2:
printf(\n How many terms to generate :);
scanf(%d ,&num );
p=fib(num);
printf(\n sum =%ld,p);
break;
case 3:
printf(\n how many terms of natural series to generate :);
scanf(%d,&num);
f=0;
f=natu(num);
printf(\n sum=%ld,f);
break;
}
}while(n!=4);
getch();
}
Output:-
49
50
Program-16 Write a function to accept 10 characters and display whether each input character
is digit uppercase letter or lower case letter.
CODING:-
#include<stdio.h>
#include<conio.h>
void main()
{
void disp();
disp();
getch();
}
void disp()
{
int m;
char c;
clrscr();
for (m=1;m<=10;m++)
{
printf("\nenter character %d : ",m);
fflush(stdin);
scanf("%c",&c);
if (c>=65 && c<=90)
printf("\n uppercase letter");
else if (c>=97 && c<=122)
printf("\n lower case letter");
else if (c>=48 && c<=57)
printf("\n digit");
51
else
printf("\n it is neither an alphabet nor digit");
}
}
Output:-
52
Program-17 Create a single program to perform the following tasks using switch, if..else, loop,
function and double dimension integer array of size 3*3
a) Addition of two matrix.
b) Substraction of two matrix.
c) Multiplication of two matrix.
d) Inverse of matrix.
e) transpose of matrix.
Coding:-
#include<stdio.h>
#include<conio.h>
#include<string.h>
#define row 3
#define col 3
53
{
for(j=0;j<col;j++)
printf(\t %.2f ,a[i] [j]);
printf(\n);
}
}
void add(float a[] [col], float b[] [col], float c[] [col])
{
int i,j;
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
c[i][j]=a[i][j]-b[i][j];
}
}
54
for(i=0;i<row;i++)
{
m=x[i] [i];
for(j=0;j<col;j++)
{
x[i][j]=x[i][j]/m;
55
y[][j]=y[i][j]/m;
}
for(k=0;k<row;k++0)
{
if (i!=k)
{
m=x[k] [i];
for(j=0;j<col;j++)
{
x[k] [j]=x[k] [j]-m*x[i] [j];
y[k] [j]=y[k] [j]-m*y[i] [j];
}
}
}
}
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
b[i] [j]=y[i] [j];
}
}
}
void main ()
{
float a[row] [col],b[row] [col],c[row] [col],d[row] [col];
int n;
do
56
{
printf(\n1. addition of matrix);
printf(\n2. subtraction of matrix);
printf(\n3. multiplication of matrix);
printf(\n4. inverse of matrix);
printf(\n5.transpose of matrix);
printf(\n6. exit);
printf(\n Enter your choice );
scanf(%d, &n);
switch(n)
{
case 1:
input (a);
input (b);
add (a,b,c);
output (c);
break;
case 2:
input (a);
input (b);
sub (a,b,c);
output (c);
break;
case 3:
input (a);
input (b);
mul (a,b,c);
57
output (c);
break;
case 4:
input (a);
inverse (a,b);
output (b);
break;
case 5:
input (a);
tran (a,b);
output (b);
break;
}
}while (n!=6);
}
Output:-
58
Program-18 Create a single program to perform following task using switch, ifelse, loop. user
defined function and single dimension character array:
a) To reverse the string.
b) To count the number of characterstic string.
c) To copy the one string to other string.
d) To find wether a given string is palindrome or not.
e) To count no. of vowels ,consonant in each word of a sentence and no of punctuations in
sentence.
CODING:-
#include<stdio.h>
#include<conio.h>
#include<string.h>
59
n=count(a);
n=n-1;
for (i=0;i<=n;i++;n--)
{
temp=a[i];
a[i]=a[n];
a[n]=temp;
}
}
60
n=count(a);
n=n-1;
for(i=0;a[n]==a[i] && n>=i; i++;n--)
if(n>=i)
return 0;
else
return 1;
}
61
for(k=0;p[i] [k]!=\0;k++)
{
switch(p[i] [k])
{
case a:
case A:
case e:
case E:
case i:
case I:
case o:
case O:
case u:
case U:
v++;
break;
case :
break;
default:
if(isalpha (p[i] [j]))
c++;
}
}
if (v>0||c>0)
{
printf(\n word=%s,p[i]);
printf(\n vowels=%d,consonant=%d,v,c);
}
v=0;
c=0;
62
}
}
void main ()
{
char a[80],b[80],s;
int n;
do
{
printf(\n1. to reverse string)
printf(\n 2. count no. of characters );
printf(\n 3. copy one string to other );
printf(\n 4. check palindrome);
printf(\n 5. check vowels, consonant in each word);
printf(\n enter your choice );
scanf(%d,&n);
switch(n)
{
case 1:
input(a);
reverse(a);
output(a);
break;
case 2:
input(a);
output(a);
printf(\n no. of characters =%d,count(a));
break;
case 3:
63
input(a);
copy(b,a);
output(b);
break;
case 4:
input(a);
n=palindrome(a);
output(a);
if(n==1)
printf(\n palindrome );
else
printf(\n not palindrome );
break;
case 5:
fflush(stdin);
printf(\n Enter a sentence below \n );
gets(a);
output(a);
wordprocessor(a);
break;
}
}while(n!=6);
getch();
}
64
Output:-
65
#include<stdio.h>
#include<conio.h>
#include<string.h>
66
67
void main()
68
{
char a[5][40],s[40];
int n;
input(a);
output(a);
printf("\n Enter string to search: ");
scanf("%s",s);
n=search(a,s);
if(n==5)
printf("\n String not found");
else
printf("\n string found at index %d",n)
largest(a);
smallest(a);
sort(a);
printf("\n After sorting");
output(a);
getch();
}
Output:-
69
70
CODING:-
#include<stdio.h>
#include<conio.h>
#include<string.h>
71
char temp;
int i,j;
for(i=0;i<=4;i++)
for(j=0;j<=4-i;j++)
{
if(strcmpi(a[j],a[j+1]>0)
{
strcpy(temp,a[j]);
strcpy(a[j],a[j+1]);
strcpy(a[j+1], temp);
}
}
}
void main()
{
char a[5][40],s[40];
72
int n;
input(a);
output(a);
printf("\n Enter string to search: ");
scanf("%s",s);
n=search(a,s);
if(n==5)
printf("\n String not found");
else
printf("\n string found at index %d",n)
sort(a);
printf("\n After sorting");
output(a);
getch();
}
Output:-
73
Program-21 Create a structure student having data members to store roll number, name of
student, name of three subjects, max marks, min marks obtained marks. Declare a structure
wariable of student provide facilities to input data in data members and display result of
student.
CODING:-
#include <stdio.h>
#include <conio.h>
struct student
{
char rollno[10];
char name[30];
char sname[3][20];
int max[3];
int min[3];
int obtained[3];
};
void main()
{
int i,maxtotal=0,total=0;
struct student s;
printf("\n enter rollno of student");
scanf("%s",s.rollno);
printf("\n enter name of student");
scanf("%s",s.name);
printf("\n enter name of subject,max. mark,min. marks,obtained \nmarks for three subjects\n");
for(i=0;i<3;i++)
{
74
Output:-
75
76
Program-22 Create structure Date with data members dd,mm,yy(to store date). Create another
structure employee with data members to hold name of employee,employee id and date of
joining(date of joining will be hold by variable of structure Date which appears as data member
inn Employee structure).store data of an employee and print the same.
CODING:-
#include<stdio.h>
#include<conio.h>
#include<string.h>
struct date
{
int mm,dd,yy;
};
struct employee
{
char empid[10];
char name[30];
float salary;
struct date doj;
};
void main()
{
struct employee e;
77
scanf("%s",e.salary);
getch();
}
Output:-
78
Program-23 Create structure student having data members to store roll number,name of
student,name of three subjects, max, min marks, obtained marks.Declare array of structure to
hold data of 3 students.provide facilities to display result of all students.provide facility to
display result of specific student whose roll number is given.
CODING:-
#include <stdio.h>
#include <conio.h>
struct student
{
char rollno[10];
char name[30];
char sname[3][20];
int max[3];
int min[3];
int obtained[3];
};
void main()
{
int i,maxtotal=0,total=0;
struct student s;
printf("\n enter rollno of student");
scanf("%s",s.rollno);
printf("\n enter name of student");
scanf("%s",s.name);
printf("\n enter name of subject,max. mark,min. marks,obtained \nmarks for three subjects\n");
79
for(i=0;i<3;i++)
{
printf("\n enter name of subject");
scanf("%s",s.name[i]);
printf("\n enter max. mark of subject");
scanf("%d",&s.max[i]);
printf("\n enter min. mark of subject");
scanf("%d",&s.min[i]);
printf("\n enter obtained mark of subject");
scanf("%d",&s.obtained[i]);
}
printf("\nrollno=%s",s.rollno);
printf("\nname of student=%s",s.name);
printf("\nsubject\tmax. mark\tmin mark\tobtained");
for(i=0;i<3;i++)
{
printf("\n%s\t\t%d\t\t%d\t\t%d",s.name[i],s.max[i],s.min[i],s.obtained[i]);
total=total+s.obtained[i];
maxtotal=maxtotal+s.max[i];
}
printf("\nscored %d out of %d",total,maxtotal);
printf("\n%% scored is %.2f",(float)total/maxtotal*100);
getch();
}
Output:-
80
81
Program-24 Define union Emp having data members:-one integer, one float and one single
dimension character array. Declare a union variable in main and test the union veriable .
CODING:-
#include<stdio.h>
#include<conio.h>
void main()
{
union emp
{
char name[20];
int age;
float salary;
} e;
clrscr();
strcpy(e.name,"ram");
printf("\nname= %s",e.name);
e.age=44;
printf("\nage= %d",e.age);
e.salary=4353;
printf("\nsalary= %f",e.salary);
getch();
}
82
Output:-
83
Program-25 Define an enum Days_of _week members of which will be days ofweek. Declare an
enum variable in main and test it.
CODING:-
#include<stdio.h>
#include<conio.h>
enum days_of_week {sun=1,mon=2,tue,wed,thu,fri,sat};
void main()
{
enum days_of_week e; /* declaration of enum variable which can take value either false or true */
e=sun;
printf("\n value of sun=%d",e);
e=mon;
printf("\n value of mon=%d",e);
getch();
}
Output:-
84
Program-26 Write a program of swapping two numbers and demonstrates call by value and call
by reference.
CODING:-
#include<stdio.h>
#include<conio.h>
void main()
{
int a=3,b=4;
void byvalswap(int ,int );
void byrefswap(int *,int *);
85
*y=t;
}
void byvalswap(int x,int y)
{
int t;
t=x;
x=y;
y=t;
}
Output:-
86
CODING:-
#include<stdio.h>
#include<conio.h>
#include<stdlib.h> /* for malloc library function */
void main()
{
char *name[5];
void sortbyptr(char **,int); /* function prototype */
int i;
clrscr();
printf("\n enter 5 names below\n");
for(i=0;i<5;i++)
{
name[i]=(char *)malloc(20);
scanf("%s",name[i]);
}
sortbyptr(name,5);
printf("\n names in sorted order ");
for(i=0;i<5;i++)
{
printf("\n%s",name[i]);
}
getch();
}
void sortbyptr(char **a,int n)
87
{
char *t;
int i,j;
for(i=0;i<n-1;i++)
for(j=0;j<n-1-i;j++)
{
if (strcmp(a[j],a[j+1])>0)
{
t=a[j];
a[j]=a[j+1];
a[j+1]=t;
}
}
}
Output:-
88
Program-28 Write a program in c using pointer and function to receive a string and a character
as a argument and return the no. of occurrences of this character in the string.
CODING:-
#include<stdio.h>
#include<stdlib.h>
void main()
{
int occur(char *,char);
char p[40],c;
printf("\n enter string");
scanf("%[^\n]",p);
fflush(stdin);
printf("\n enter character to count:");
scanf("%c",&c);
89
90
Program-29 Write program to create structure Employee having data members to store name of
employees id, salary.use pointer to structure to store data of employees and print the stored data
using pointer to structure.
CODING:-
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct emp
{
char name[20];
int empid;
int salary;
};
void main()
{
91
getch();
}
Output:-
92
Program-30 Write a program to create a structure employee having data member to store name
of employee,employee id,salary use pointer to structure to simulate dynamic array of structure
store data of employee and print the stored data using pointer to structure.
CODING:-
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct emp
{
char name[20];
int empid;
int salary;
};
void main()
{
struct emp *e;
93
int i;
e=(struct emp *)malloc(sizeof (struct emp));
94
Program-31 Write a program to sort a single dimension array of integers ofn elements
simulated by pointer to integer.Use function for sortingthe dynamic array.
CODING:-
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
int *p;
int n,i;
void sort(int *,int);
printf("\n enter no. of elements");
scanf("%d",&n);
p= (int *) malloc (sizeof(int)*n); /* allocate 2*n bytes to generate an array p[n] */
printf("\n enter %d values",n);
for(i=0;i<n;i++)
scanf("%d",&p[i]); /* or (p+i)
*/
95
sort(p,n);
printf("\n values after sorting are\n");
for(i=0;i<n;i++)
printf("\t%d",p[i]); /* or *(p+i) */
getch();
}
void sort(int *p,int n)
{
int i,j,temp;
for(i=0;i<n-1;i++)
for(j=0;j<n-1-i;j++)
{
if(p[j]>p[j+1])
{
temp=p[j];
p[j]=p[j+1];
p[j+1]=temp;
}
}
}
Output:-
96
Program-32 Write a program to sum elements of a double dimension array ofintegers of m rows
and n columns simulated by pointer to pointer tointeger. Use function for sum the elements of
the dynamic array.
CODING:-
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
int **p;
int m,n,i,j;
int s=0;
printf("\n enter no. of rows and columns");
97
scanf("%d %d",&m,&n);
for(i=0;i<m;i++)
p[i]=(int *)malloc(sizeof(int)*n);
*/
/* or *(*(p+i)+j) */
s=s+p[i][j];
}
printf("\n");
}
printf("\n sum of elements=%d",s);
getch();
}
Output:-
98
CODING:-
#include<stdio.h>
void main()
{
int a[]={1,2,3,4,5};
int *p;
int *q;
int *r;
p=&a[0];
99
q=&a[2];
int z;
/* r=p+q; not allowed */
z=p-q;
printf("\n the no. of element apart=%d",z);
100
Program-34 Write a program to demonstrate difference between constant pointer and pointer
of constant .
Coding:#include<stdio.h>
int main(void)
{
int var1 = 0, var2 = 1;
int *ptr = &var1;
ptr=&var2;
printf("output for constant pointer =%d\n", *ptr);
*ptr = 1;
printf("output for pointer of constant =%d\n", *ptr);
return 0;
101
}
Output:-
CODING:-
#include<stdio.h>
#include<stdlib.h>
void main()
{
int *allocate(int);
int *p,n,i;
printf("\n Enter no. of elements required");
scanf("%d",&n);
p=allocate(n);
102
for(i=0;i<n;i++)
{
printf("\t%d",p[i]);
}
getch();
}
int *allocate(int n)
{
int i;
int *a;
a=(int*)malloc(sizeof(int)*n);
for(i=0;i<n;i++)
a[i]=2*i;
return a;
}
Output:-
103
104
Program-36 Write program to copy content of one file removing extra spacebetween words
name of files should come from command line arguments.
CODING:-
#include<stdio.h>
#include<conio.h>
#include<dos.h>
void main(int argc,char *argv[])
{
FILE *fp,*ft;
if (argc!=3)
{
printf("\n usage: programfilename sorucefilename targetfilename");
getch();
exit (1);
}
fp=fopen(argv[1],"r"); /* open source file in read mode */
ft=fopen(argv[2],"w");/* open target file in write mode */
/* let us check either of the file open operations failed, if failed then terminate program */
if (fp==NULL || ft == NULL)
{
printf("\n file open operation failed");
getch();
exit (1);
}
while((ch=fgetc(fp))!=EOF)
fputc(ch,ft);
fclose(fp);
105
fclose(ft);
getch();
}
Output:-
106
Program-37 Write a program to count no. of tabs, new lines, character and space of file.
CODING:-
#include<stdio.h>
#include<conio.h>
Void main()
{
FILE *fp;
char ch;
int nol=0,no=0,nob=0,noc=0;
fp=fopen(PR1.C,r);
while(1)
{
ch=fgetc(fp);
if(ch==EOF)
break;
noc++;
if(ch==\n)
nol++;
if(ch==\t)
no++;
}
fclose(fp);
printf(\nNumber of chracters=%d,noc);
printf(\nNumber of blanks=%d,nob);
printf(\nNumber of tabs=%d,no);
printf(\nNumber of lines=%d,nol);
getch();
}
107
Output:
108
Program-38 Write a program to read item number, rate and quantity from an inventory file and
print the followings:
#include<stdio.h>
#include<conio.h>
#include<process.h>
struct item
{
int itemnumber;
int quantity;
float rate;
};
void main()
{
FILE *fp;
struct item e;
float cost=0;
int n;
clrscr();
fp=fopen("inv","wb");
if(fp==NULL)
{
printf("\n file open operation failed");
exit (1);
}
109
while(1)
{
printf("\n enter itemnumber,quantity and rate:\n");
scanf("%d %d %f",&e.itemnumber,&e.quantity,&e.rate);
fwrite(&e,1,sizeof(e),fp);
printf("\n enter more item if yes then enter 1,if no enter 0:");
scanf("%d",&n);
if(n==0)
break;
}
fclose(fp);
fp=fopen("inv","rb");
while(fread(&e,1,sizeof(e),fp)!=0)
{
cost=cost+e.rate*e.quantity;
if(e.quantity>5)
printf("\n item number=%d,item quantity=%d,item rate=%f",e.itemnumber,e.quantity,e.rate);
}
printf("\n total cost of inventory=%f",cost);
getch();
}
Output:-
110
111
Program-39 write a program to demonstrate difference between character array and pointer to
character.
Coding:#include<stdio.h>
#include<conio.h>
void print(char*c)
{
int i=0;
while(c[i] !='\0')
{
printf("%c",c[i]);
i++;
}
printf("\n");
}
int main()
{
char name[]={'a','n','i','l','\t'};
char sirname[]="shetty";
char c[20]="hello";
printf("%s",name);
printf("%s\n",sirname);
printf(c);
getch();
return 0;
}
Output:-
112
113
Program-40 write a program to find biggest number among the three number using pointer and
function.
Coding:-
#include <stdio.h>
int main()
{
int array[3], *maximum, size, c, location = 1;
printf("Enter the number of elements in array\n");
scanf("%d", &size);
printf("Enter %ld integers\n", size);
for ( c = 0 ; c < size ; c++ )
scanf("%ld", &array[c]);
maximum = array;
*maximum = *array;
for (c = 1; c < size; c++)
{
if (*(array+c) > *maximum)
{
*maximum = *(array+c);
location = c+1;
}
}
printf("Maximum element is present at location number %ld and it's value is %ld.\n", location,
*maximum);
return 0;
}
Output:-
114
115