Q1.Program To Swap The Value of Two Variable With The Help of Third Variable
Q1.Program To Swap The Value of Two Variable With The Help of Third Variable
#include<conio.h>
void main()
{
int a,b,temp;
clrscr();
printf("enter the value of a");
scanf("%d",&a);
printf("enter the value of b");
scanf("%d",&b);
temp=a;
a=b;
b=temp;
printf("\n a=%d",a);
printf("\n b=%d",b);
getch();
}
Output:-
Pag
e1
Q2.Program to swap the value of two variable without third
variable:-
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
clrscr();
printf("enter the value of a");
scanf("%d",&a);
printf("enter the value of b");
scanf("%d",&b);
a=a+b;
b=a-b;
a=a-b;
printf("\n a=%d",a);
printf("\n b=%d",b);
getch();
}
Output:-
Pag
e2
Q3.Program to create equivalent of a four function calculator
by using if else staement:-
#include<stdio.h>
#include<conio.h>
void main()
{
float a,b,result;
char oper;
clrscr();
printf("enter the value of a");
scanf("%f",&a);
printf("enter the value of b");
scanf("%f",&b);
fflush(stdin);
printf("enter the operator");
scanf("%c",&oper);
if(oper=='+')
result=a+b;
else if(oper=='-')
result==a-b;
else if(oper=='*')
result=a*b;
else if (oper=='/')
result=a/b;
else
printf("wrong operator:\n");
printf("the result is=%f",result);
getch();
}
Pag
e3
Output:-
Pag
e4
Q4.Program to calculate the compound interest:-
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
float p,r,ci,t;
clrscr();
printf("enter the principle.\n");
scanf("%f",&p);
printf("enter the rate.\n");
scanf("%f",&r);
printf("enter the time.\n");
scanf("%f",&t);
ci=p*pow((1+r/100),t)-p;
printf("compound interest is =%2.2f",ci);
getch();
}
Output:-
Pag
e5
Q5.Program to show the result of the students:-
#include<stdio.h>
#include<conio.h>
void main()
{
int marks;
clrscr();
printf("enter the marks of students");
scanf("%d",&marks);
if(marks>=75)
printf("distingtion");
else if (marks<75&&marks>60)
printf("first division");
else if(marks<60&&marks>=45)
printf("second division");
else if(marks<45&&marks>=33)
printf("third division");
else if(marks<33)
printf("fail");
getch();
}
Output:-
Pag
e6
Q6.Program which illustrate the switch statement:-
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
int a,b,c,choice;
clrscr();
printf("enter the value of a");
scanf("%d",&a);
printf("enter the value of b");
scanf("%d",&b);
printf("enter your choice::\n enter 1 for add\n enter 2 for sub \n enter 3
for mmult\n enter 4 for div");
scanf("%d",&choice);
switch(choice)
{
case 1:
c=a+b;
break;
case 2:
c=a-b;
Pag
e7
break;
case 3:
c=a*b;
break;
case 4:
c=a/b;
break;
case 5:
exit(0);
printf("\t\t\tenter your choice between 1 to 5");
}
printf("\t\t\ result is =%d",c);
getch();
}
Output:-
Pag
e8
Q7.Program which illustrate the break statement:-
#include<stdio.h>
#include<conio.h>
void main()
{
char choice;
clrscr();
printf("menu\n");
printf("g for green\n");
printf("p for pink\n");
printf("r for red\n");
printf("enter your choice");
scanf("%s",&choice);
switch(choice)
{
case 'g':
printf("green");
break;
case 'p':
Pag
e9
printf("pink");
break;
case 'r':
printf("red");
break;
}
getch();
}
Output:-
Pag
e 10
Q8.Program to find greatest among three variables:-
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
clrscr();
printf("enter the value of a");
scanf("%d",&a);
printf("enter the value of b");
scanf("%d",&b);
printf("enter the value of c");
scanf("%d",&c);
if(a>b&&a>c)
printf("a is greatest");
Pag
e 11
if(b>a&&b>c)
printf("b is greatest");
if(c>a&&c>b)
printf("c is greatest");
getch();
}
Output:-
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
clrscr();
printf("enter the value of a,b and c");
scanf("%d%d%d",&a,&b,&c);
if(a>b)
if(a>c)
printf("a is greatest");
Pag
e 12
else
printf("c is greatest");
else
if(b>c)
printf("b is greatest");
else
printf("c is greatst");
getch();
}
Output:-
#include<stdio.h>
#include<conio.h>
void main()
{
int n;
clrscr();
printf("enter a number");
scanf("%d",&n);
if(n>0)
printf("given number is positive");
Pag
e 13
else
if(n<0)
printf("given number is negative");
else
printf("given number is zero");
getch();
}
Output:-
#include<stdio.h>
#include<conio.h>
void main()
{
int year;
clrscr();
printf("enter the year");
scanf("%d",&year);
if(year%4==0)
Pag
e 14
{
printf("given year is a leap year");
}
else
printf("given year is not a leap year");
getch();
}
Output:-
#include<stdio.h>
#include<conio.h>
void main()
{
char ch;
clrscr();
printf("enter the character between a-d");
Pag
e 15
scanf("%c",&ch);
switch(ch)
{
case 'a':
printf("you pressed a");
break;
case 'b':
printf("you pressed b");
break;
case 'c':
printf("you pressed c");
break;
case 'd':
printf("you pressed d");
break;
default:
printf(" wrong character ");
}
getch();
}
Output:-
Pag
e 16
Q.13.Program to check given character is vowel or not:-
#include<stdio.h>
#include<conio.h>
void main()
{
char ch;
Pag
e 17
clrscr();
printf("enter a character");
scanf("%c",&ch);
switch(ch)
{
case 'a':
printf("vowel");
break;
case 'e':
printf("vowel");
break;
case 'i':
printf("vowel");
break;
case 'o':
printf("vowel");
break;
case 'u':
printf("vowel");
default:
printf("not a vowel");
}
getch();
}
Output:-
Pag
e 18
Pag
e 19
Q.17.Program to do arithmetic operation as desired:-
#include<stdio.h>
#include<conio.h>
void main()
{
char ch;
int a,b,c;
clrscr();
printf("enter the value of a&b");
scanf("%d%d",&a,&b);
printf("\nadd(a)");
printf("\nsub(s)");
printf("\nmultiply(m)") ;
printf("\ndivide(d)");
scanf("%c",&ch);
switch(ch)
{
case 'a':
c=a+b;
break;
case 's':
c=a-b;
break;
case 'm':
c=a*b;
break;
case 'd':
c=a/b;
break;
}
printf("result=%d",c);
getch();
Pag
e 20
}
Output;-
Pag
e 21
Q.18 Program to generate table of given number:-
#include<stdio.h>
#include<conio.h>
void main()
{
int i=1,n,t;
clrscr();
printf("enter th number");
scanf("%d",&n);
tab_para:t=n*i;
printf("\n%d",t);
i=i+1;
if(i<=10)
goto tab_para;
getch();
}
Output:-
Pag
e 22
Q.19 Program to get sum of number and average:-
#include<stdio.h>
#include<conio.h>
void main()
{
int n,i;
float a,avg=0,sum=0;
clrscr();
printf("how many number");
scanf("%d",&n);
for(i=0;i<n;++i)
{
printf("enter a no");
scanf("%f",&a);
sum=sum+a;
}
avg=sum/n;
Pag
e 23
printf("sum is =%f",sum);
printf("average is -%f",avg);
getch();
}
Output:-
#include<stdio.h>
#include<conio.h>
void main()
{
long int num,rem,i,jt;
long int ctr=0;
clrscr();
printf("enter the value");
scanf("%ld",&num);
for(i=1;i<num;i++)
{
Pag
e 24
ctr=0;
for(j=2;j<i;j++)
{
rem=i%j;
if(rem==0)
ctr++;
}
if(ctr==0)
printf("%d\n",i);
}
getch();
}
Output:-
Pag
e 25
Q.21 Program to find the reverse of the number:-
#include<stdio.h>
#include<conio.h>
void main()
{
int num,rem,sum=0,i;
clrscr();
printf("enter the elements");
scanf("%d",&num);
for(i=0;i<num;i++)
{
while(num!=0)
Pag
e 26
{
rem=num%10;
sum=sum*10+rem;
num=num/10;
}}
printf("reverse is=%d",sum);
getch();
}
Output:-
#include<stdio.h>
#include<conio.h>
void main()
{
int num,a;
int counter=0,sum=0;
clrscr();
printf("enter the digit");
scanf("%d",&num);
Pag
e 27
while(num!=0)
{
a=num%10;
num=num/10;
sum=sum+a;
counter++;
}
printf("sum of digit is=%d",sum);
printf("count=%d",counter);
getch();
}
Output:-
#include<stdio.h>
#include<conio.h>
void main()
{
int a,x,y;
int z=0;
clrscr();
printf("enter a no");
Pag
e 28
scanf("%d",&x);
y=x;
while(x!=0)
{
a=x%10;
x=x/10;
z=z+(a*a*a);
}
if(y==z)
printf("given no is armstrong");
else
printf("given no is not armstrong");
getch();
}
Output:-
#include<stdio.h>
#include<conio.h>
void main()
{
char string[30];
int count=0;
clrscr();
Pag
e 29
printf("enter the string");
scanf("%s",&string);
while(string[count]!='\0')
count++;
printf("total no is=%d",count);
getch();
}
Output:-
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
Pag
e 30
char str[100];
int i=0,count=0;
clrscr();
printf("enter the strings");
scanf("%s",&str);
while(str[i]!='\0')
{
if(str[i]=='a'||str[i]=='e'||str[i]=='i'||str[i]=='o'||str[i]=='u')
{
count++;
}
i++;
}
printf("total no is=%d",count);
getch();
}
Output:-
#include<stdio.h>
#include<conio.h>
void main()
{
Pag
e 31
int n;
clrscr();
printf("enter the number");
scanf("%d",&n);
if(n%2==0)
printf("even number");
else
printf("odd number");
getch();
}
Output:-
#include<stdio.h>
#include<conio.h>
void main()
Pag
e 32
{
int n;
printf("enter the no to check odd number");
scanf("%d",&n);
if(n&1==1)
printf("odd");
else
printf("even");
getch();
}
Output:-
#include<stdio.h>
Pag
e 33
#include<conio.h>
void main()
{
int base,exp;
long int pow=1;
int i;
clrscr();
printf("enter the base:");
scanf("%d",&base);
printf("enter exponent");
scanf("%d",&exp);
for(i=1;i<exp;i++)
{
pow=pow*base;
}
printf("%d^%d=%ld",base,exp,pow);
getch();
}
Output:-
Pag
e 34
#include<stdio.h>
#include<conio.h>
void main()
{
int marks[10],n,i;
float avg=0,sum=0;
clrscr();
printf("enter the number of terms");
scanf("%d",&n);
printf("\n enter the numbers:\n");
for(i=1;i<=n;i++)
{
scanf("%d",&marks[i]);
}
for(i=1;i<=n;i++)
{
sum=sum+marks[i];
avg=sum/n;
}
printf("\n avg of enterd number are:%f",avg);
getch();
}
Output:-
Pag
e 35
Q.30 Program to find the largest and smallest elements in
array:-
#include<stdio.h>
#include<conio.h>
void main()
{
int a[20],n,i,large,small;
clrscr();
printf("how many elements in array\n");
scanf("%d",&n);
printf("enter the elements");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("the elements of the array are\n");
for(i=0;i<n;i++)
{
printf("%d",a[i]);
}
large=a[0];
small=a[0];
for(i=1;i<n;i++)
{
if (a[i]>large)
{
large=a[i];
}
else if (a[i]<small)
{
Pag
e 36
small=a[i];
}
}
printf("large element =%d smallest element %d",large,small);
getch();
}
Output:-
Pag
e 37
Q31. Program of bubble sort:-
#include<stdio.h>
#include<conio.h>
void main()
{
int array[20],i,j,k,m,n,t;
clrscr();
printf("how many elements in array you want: \n");
scanf("%d",&n);
printf("enter the values in array\n");
for(i=0;i<n;i++)
scanf("%d",&array[i]);
m=n-1;
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
if(array[j]>array[j+1])
{
t=array[j];
array[j]=array[j+1];
array[j+1] =t;
}
}
m--;
}
for(k=0;k<n;k++)
Pag
e 38
printf("%d\t",array[k]);
getch();
}
Output:-
Pag
e 39
Q. 32 Program to concatenate two strings:-
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char str1[20],str2[20],i=0,j=0;
clrscr();
printf("enter the first sttring");
scanf("%s",&str1);
printf("enter the second string");
scanf("%s",&str2);
while(str1[i]!='\0')
{
i++;
}
while(str2[j]!='\0')
{
str1[i]=str2[j];
i++;
j++;
}
str1[i]='\0';
printf("concatenated string=%s",str1);
getch();
Pag
e 40
}
Output:-
#include<stdio.h>
#include<conio.h>
void main()
{
int i=25;
int *j;
j=&i;
clrscr();
printf("\n address of i=%u",&i);
printf("\n address of i=%u",j) ;
printf("\n address of j=%u",&j);
printf("\n address of j=%u",j);
printf("\n address of i=%d",i);
printf("\n address of i=%d",*(&i));
printf("\n address of i=%d",*j);
getch();
}
Output:-
Pag
e 41
Q.34 Program that calls a function by reference:-
#include<stdio.h>
#include<conio.h>
void main()
{
int a=60,b=10;
void addab(int*x,int*y);
clrscr();
printf("\n before function call a=%d,b=%d",a,b);
addab(&a,&b);
printf("\n\n after funcion call a=%d,b=%d",a,b);
getch();
}
void addab(int*x,int*y)
{
*x=*x+10;
*y=*y+10;
printf("\n\n inside the function a=%d,b=%d",*x,*y);
Pag
e 42
}
Output:-
#include<stdio.h>
#include<conio.h>
void main()
{
int n,i;
float a,avg=0.0,sum=0.0;
clrscr();
Pag
e 43
}
avg=sum/n;
printf("sum is =%2.2f",sum);
printf("avg is=%2.2f",avg);
getch();
}
Output:-
#include<stdio.h>
#include<conio.h>
void main()
{
int num,i;
clrscr();
printf("enter the no. ");
scanf("%d",&num);
printf("the n prime factor is:");
for(i=2;i<=num;i++)
if(num%i==0)
{
printf("%d",i);
num=num/i;
Pag
e 44
i--;
}
getch();
}
Output-
#include<stdio.h>
#include<conio.h>
void main()
{
long int num,a,b;
long int counter=0,sum=0;
clrscr();
printf("enter the digit");
scanf("%ld",&num);
do
{
a=num%10;
num=num/10;
Pag
e 45
sum=sum+a;
counter++;
}while(num!=0);
printf("sum=%ld",sum);
printf("count=%ld",counter);
getch();
}
Output:-
#include<stdio.h>
#include<conio.h>
void main()
{
int i=0,n;
clrscr();
printf("enter the number");
scanf("%d",&n);
while(i<=n)
{
if(i%2==0)
printf("%d",i);
Pag
e 46
i=i+2;
}
getch();
}
Output—
#include<stdio.h>
#include<conio.h>
void main()
{
int i=1,n,sum=0;
clrscr();
printf("enter the number");
scanf("%d",&n);
while(i<=n)
{
Pag
e 47
if(i%2!=0)
sum=sum+i;
i=i+2;
}
printf("sum=%d",sum);
getch();
output-
#include <stdio.h>
#include<conio.h>
void main()
{
char line[150];
int i, vowels, consonants, digits, spaces;
Pag
e 48
vowels = consonants = digits = spaces = other = 0;
printf("Vowels: %d",vowels);
printf("\nConsonants: %d",consonants);
printf("\nDigits: %d",digits);
printf("\nWhite spaces: %d", spaces);
printf("\nWhite spaces: %d", other);
getch();
}
Output-
Pag
e 49
Q.41 program to illustrate the use of a structure pointer
#include<stdio.h>
#include<conio.h>
void main()
{
Pag
e 50
int i,j,n;
int a[10],large,small;
clrscr();
printf("how many element in array");
scanf("%d”,&n);
printf("enter the array element \n");
for(i=0;i<n;i++)
{
scanf(“%d”,&a[i]);
}
printf("the element of the array”);
for(i=0;i<n;i++)
{
if(a[i]>large)
large=a[i];
else if(a[i]<small)
small=a[i];
}
printf(“\n largest element in array is %d”,large);
printf(“\n smallest element in array is %d”,small);
getch();
}
Output-
Pag
e 51
Q.42 Program to access the element of array using pointer
Pag
e 52
#include<stdio.h>
#include<conio.h>
void main()
{ int a[5]={1,4,6,8,3};
int i, *ptr;
clrscr();
ptr=&a[0];
for(i=0;i<5;i++)
{
printf("\n\t%d - %d",ptr, *ptr);
ptr++;
}
getch();
}
Output-
Pag
e 53
Q.43 Program that uses different way to access the element of
array
#include<stdio.h>
#include<conio.h>
void main()
{ int a[5]={1,4,6,8,3};
int i;
int *ary=a;
clrscr();
for(i=0;i<5;i++)
{
printf("\n%d %d ",a[i],*(a+i));
printf(" %d %d %d\n",*(i+a),i[a],*ary);
}
getch();
}
Output-
Pag
e 54
Q.44 Program that passes an array to a function
#include<stdio.h>
#include<conio.h>
void sort(int *b,int);
void main()
{
int a[20],i,n,b;
clrscr();
printf("enter the number of element of array");
scanf("%d",&n);
printf("enter the element");
for(i=0;i<n;i++)
scanf("%d",a+i);
sort(a,n);
printf("the sorted array");
for(i=0;i<n;i++)
printf("%d",*(a+i));
sort(int *b,int n)
{
int i=0,j,f=0,temp;
while(i++<n&&!f)
{
f=1;
for(j=0;j<n;j++)
if(b[j]>b[j+1])
{
temp=b[j];
b[j]=b[j+1];
b[j+1]=temp;
f=0;
Pag
e 55
}
}
getch();
}
Output-
Pag
e 56
Q.45 Declaration and use of pointers
#include<stdio.h>
#include<conio.h>
void main()
{
int *ptr;
int **ptrtoptr;
int data;
clrscr();
ptr=&data;
ptrtoptr=&ptr;
*ptr=100;
printf("variable data contain %d \n",data);
**ptrtoptr=200;
printf("cariable data contains %d \n",data);
data=300;
printf("ptrtoptr is pointing to %d",**ptrtoptr);
getch();
}
Output-
Pag
e 57
Q.46 No argument with no return value
#include<stdio.h>
#include<conio.h>
void sum();
void main()
{
clrscr();
sum();
getch();
}
void sum()
{
int a,b,c;
printf("enter two values");
scanf("%d%d",&a,&b);
c=a+b;
printf("sum=%d",c);
}
Output-
Pag
e 58
Q.47 Argument with no return value
#include<stdio.h>
#include<conio.h>
void sum(int,int);
void main()
{
int a,b;
printf("enter two values");
scanf("%d%d",&a,&b);
sum(a,b);
getch();
}
void sum(int a,int b)
{
int c=a+b;
printf("sum=%d",c);
}
Pag
e 59
Output-
#include<stdio.h>
#include<conio.h>
int sum(int,int);
void main()
{
int a,b,result;
printf("enter two values");
scanf("%d%d",&a,&b);
result=sum(a,b);
printf("sum=%d",result);
getch();
}
int sum(int a,int b)
{
int c=a+b;
Pag
e 60
return(c);
}
Output-
#include<stdio.h>
#include<conio.h>
void sum(int,int);
void readdata();
void print(int);
void main()
{
clrscr();
readdata();
getch();
}
void readdata()
{
Pag
e 61
int a,b;
printf("enter two value");
scanf("%d%d",&a,&b);
sum(a,b);
}
void sum(int x,int y)
{
int c=x+y;
print(c);
}
void print(int c)
{
printf("sum = %d",c);
}
Output-
Pag
e 62
Q.50 Program to find the power
#include<stdio.h>
#include<conio.h>
double pf(double a,float b)
{
if(b==0)
return 1;
else if(b<0)
return((a*pf(a,b+1)));
else
return((a*pf(a,b-1)));
Pag
e 63
}
void main()
{
double mant;
float expo;
clrscr();
printf("enter the maintissa==");
scanf("%lf",&mant);
printf("enter the exponent==");
scanf("%f",&expo);
if(expo<0)
printf("\n maintissa^exponent:%lf",1/pf(mant,expo));
else
printf("\nmaintissa^exponent:%lf",pf(mant,expo));
getch();
}
Output-
Pag
e 64
Q.51 Program to find the maximum and minimum from a list
#include<stdio.h>
#include<conio.h>
void mm(int *,int,int,int);
void mm(int l[],int n,int max,int min)
{
int i;
max=min=l[0];
Pag
e 65
for(i=1;i<n;i++)
{
if(l[i]>max)
max=l[i];
if(l[i]<min)
min=l[i];
}
printf("maximum=%d",max);
printf("minimum=%d",min);
}
void main()
{
int i,list[50];
int n,max,min;
clrscr();
printf("how many elements in the list");
scanf("%d",&n);
printf("enter the elements in the list");
for(i=0;i<n;i++)
scanf("%d",&list[i]);
mm(list,n,max,min);
getch();
}
Output-
Pag
e 66
Q.52 Program to make a table of given no using recursion
#include<stdio.h>
#include<conio.h>
int table(int,int);
void main()
{int i,n;
Pag
e 67
clrscr();
printf("enter any no");
scanf("%d",&n);
for(i=1;i<=10;i++)
printf("\n\t%d x %d = %d",n,i,table(n,i));
getch();
}
int table(int n,int i)
{
if(i==1)
return(n);
else
return(table(n,i-1)+n);
}
Output-
#include<stdio.h>
#include<conio.h>
void rev_str(char *string)
{
Pag
e 68
if(*string)
{
rev_str(string+1);
putchar(*string);
}
}
void main()
{
char str[100];
clrscr();
printf("enter the string");
gets(str);
printf("\nreverse string is \t");
rev_str(str);
getch();
}
Output-
#include<stdio.h>
#include<conio.h>
int fact(int);
Pag
e 69
void main()
{
int num,result;
clrscr();
printf("enter the number");
scanf("%d",&num);
result=fact(num);
printf("factorial=%d",result);
getch();
}
int fact(int n)
{
int f;
if(n==1||n==0)
return(1);
else
f=n*fact(n-1);
return(f);
}
Output-
#include<stdio.h>
#include<conio.h>
Pag
e 70
int fact(int);
void main()
{
int num,i=0;
clrscr();
printf("how many terms for fibonacci series==");
scanf("%d",&num);
printf("fibonacci series as follows");
while(i<num)
{
++i;
printf("\t\n%d",fibo(i));
}
getch();
}
int fibo(int n)
{
int f;
if(n==1||n==0)
return 0;
else if(n==2)
return 1;
else
return(fibo(n-1)+fibo(n-2));
}
Output-
Pag
e 71
Pag
e 72
Q.56 Program to compare the allocate memory using struct
and union
#include<stdio.h>
#include<conio.h>
struct employee
{
int ecode;
char ename[20];
float sal;
}emp;
union
{
int ecode;
char ename[20];
float sal;
}employee;
void main()
{
clrscr();
printf("\nthe size of structure is %d\n",sizeof(emp));
printf("\nthe size of union is %d",sizeof(employee));
getch();
}
Output-
Pag
e 73
Q.57 Passing address of structure variables
#include<stdio.h>
#include<conio.h>
struct employee
{
int ecode;
char ename[20];
float sal;
};
void main()
{
struct employee emp;
void display(struct employee);
clrscr();
printf("enter employee code");
scanf("%d",&emp.ecode);
printf("enter employee name");
scanf("%s",&emp.ename);
printf("enter employee salary");
scanf("%f",&emp.sal);
display(emp);
getch();
}
void display(struct employee emp1)
{
Pag
e 74
printf("\n%d",emp1.ecode);
printf("\n%s",emp1.ename);
printf("\n%f",emp1.sal);
}
Output:-
Pag
e 75
Q.58 Program to assign the value of a structure ro another
structure
#include<stdio.h>
#include<conio.h>
struct student
{
int roll;
char name[20];
int total;
};
void main()
{
struct student s1={123,neha,50};
struct student s2;
s2=s1;
clrscr();
printf("\nroll=%d",s1.roll);
printf("\nname=%s",s1.name);
printf("\ntotal=%d",s1.total);
getch();
}
Pag
e 76
Output-
#include<stdio.h>
#include<conio.h>
struct employee
{
int ecode;
char ename[20];
float sal;
};
void main()
{
struct employee e1={110,"babita",91};
struct employee e2={110,"neha",85};
clrscr();
if(e1.ecode==e2.ecode)
printf("both employee code are same");
else
{
printf("\nenter employee code",e1.ecode);
printf("\nenter employee name",e1.ename);
printf("\nenter employee salary",e1.sal);
Pag
e 77
printf("\nenter employee code",e2.ecode);
printf("\nenter employee name",e2.ename);
printf("\nenter employee salary",e2.sal);
}
getch();
}
Output-
#include<stdio.h>
#include<conio.h>
struct product
{
int pcode;
int qty;
float price;
};
void main()
{
struct product p={102,5,35.50};
int size;
clrscr();
printf("\nproduct code=%d",p.pcode);
printf("\nproduct quantity=%d",p.qty);
printf("\nproduct price=%f",p.price);
size=sizeof(p);
Pag
e 78
printf("\ntotal memory occupied by structure variables in bytes is
%d",size);
getch();
}
Output-
#include<stdio.h>
#include<conio.h>
union product
{
int pcode;
int qty;
float price;
};
void main()
{
union product p;
int size;
p.pcode=102;
p.qty=5;
p.price=35.30;
clrscr();
Pag
e 79
printf("\nproduct code=%d",p.pcode);
printf("\nproduct quantity=%d",p.qty);
printf("\nproduct price=%f",p.price);
size=sizeof(p);
printf("total memory =%d",size);
getch();
}
Output-
#include<stdio.h>
#include<conio.h>
struct product
{
int pid;
char pname[20];
int qty;
int price;
};
void main()
{
struct product p[5];
int i;
clrscr();
Pag
e 80
for(i=0;i<5;i++)
{
printf("product id");
scanf("%d",&p[i].pid);
printf("product name");
scanf("%s",&p[i].pname);
printf("product quantity");
scanf("%d",&p[i].qty);
printf("product price");
scanf("%d",&p[i].price);
}
for(i=0;i<5;i++)
{
printf("id=%d",p[i].pid);
printf("name=%s",p[i].pname);
printf("quantity=%d",p[i].qty);
printf("price=%d",p[i].price);
}
getch();
}
Output-
Pag
e 81
Q.63 Program for passing structure as a perameter
#include<stdio.h>
#include<conio.h>
struct book
Pag
e 82
{
char bname[20];
char author[20];
int page;
float price;
};
void display(struct book p);
void main()
{
struct book b;
clrscr();
printf("enter book name");
scanf("%s",&b.bname);
printf("enter author name");
scanf("%s",&b.author);
printf("enter number of pages");
scanf("%d",&b.page);
printf("enter the price");
scanf("%f",&b.price);
printf("this is print by main funcation");
printf("\n%s",b.bname);
printf("\n%s",b.author);
printf("\n%d",b.page);
printf("\n%f",b.price);
display(b);
getch();
}
void display(struct book p)
{
printf("this is print by show funcation");
printf("\n%s",p.bname);
printf("\n%s",p.author);
printf("\n%d",p.page);
Pag
e 83
printf("\n%f",p.price);
}
Output-
Q.64 Program to read a line of text from a data file and display
it on the screen
Pag
e 84
#include<stdio.h>
#include<conio.h>
void main()
{
FILE *fpt;
char c;
clrscr();
if((fpt=fopen("sample.dat","r"))==NULL)
printf("cannot open the file");
else
do
putchar(c=getc(fpt));
while(c!='\n');
fclose(fpt);
getch();
}
Output-
Pag
e 85
Q.65 Program to count characters,spaces,tabs and newline in a
file
#include<stdio.h>
#include<conio.h>
void main()
{
FILE *fpt;
char c;
int line=0,ch=0,tb=0,sp=0;
clrscr();
if((fpt=fopen("sample.dat","r"))==NULL)
printf("cannot open the file");
else
while(1)
{
c=fgetc(fpt);
if(c==EOF)
break;
ch++;
if(c==' ')
sp++;
if(c=='\n')
line++;
if(c=='\t');
tb++;
}
fclose(fpt);
printf("\nnumber of line =%d",line);
printf("\nnumber of space=%d",sp);
printf("\nnumber of character=%d",ch);
printf("\nnumber of tabs=%d",tb);
Pag
e 86
getch();
}
Output:-
Pag
e 87
Q.66 Program to accept and display the arguments
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main(noa,loa)
int noa;
char *loa[];
{
int i;
clrscr();
if(noa!=3)
{
printf("improper number of arguments");
exit(0);
}
printf("total arguments in command line is=%d",noa);
printf("the arguments as you have given");
for(i=0;i<noa;i++)
{
printf("\t");
printf(loa[i]);
}
getch();
}
Output-
Pag
e 88
Q67 Program that calls a function by value
#include<stdio.h>
#include<conio.h>
void main()
int a=40,b=10;
clrscr();
addab(a,b);
getch();
x=x+10;
Pag
e 89
Y=Y+10;
Output:-
Pag
e 90
Q 68a program to demonstrate invoking of a funcation using
pointer
#include<stdio.h>
int sum(int,int);
main()
int a,b,c;
int(*p)(int,int);
scanf("%d%d",&a,&b);
p=sum;
c=(*p)(a,b);
printf("\nsum=%d",c);
Pag
e 91
}
return(a+b);
Output:-
Pag
e 92
Q 69 program for the removal of duplicate elements of an
array
#include<stdio.h>
#include<conio.h>
void main()
int n,i,j,k;
int a[20],count=0;
clrscr();
scanf("%d",&n);
for(i=0;i<n;i++)
Pag
e 93
scanf("%d",&a[i]);
for(i=0;i<n;i++)
for(j=j+1;j<(n-count);j++)
if(a[i]==a[j])
for(k=j;k<(n-count);k++)
a[k]=a[k+1];
count++;
i--;
for(i=0;i<(n-count);i++)
printf("%d\n",a[i]);
getch();
Output:-
Pag
e 94
Q70program to access the elements of two dimensional array
#include<stdio.h>
#include<conio.h>
void main()
int a[3][3],i,j;
Pag
e 95
clrscr();
for(i=0;i<=2;i++)
for(j=0;j<=2;j++)
scanf("%d",&a[i][j]);
}}
for(i=0;i<=2;i++)
for(j=0;j<=2;j++)
printf("%d",a[i][j]);
printf("\n");
getch();
Output:-
Pag
e 96
Q71.program to convert uppercase to lowercase
#include<stdio.h>
#include<conio.h>
Pag
e 97
#include<ctype.h>
void main()
char line[75];
int i;
clrscr();
scanf("%s",line);
i=0;
while(line[i]!='\0')
printf("%c",tolower(line[i]);
i++;
getch();
Output:-
Pag
e 98
Q72. Program to illustrate the use of structre:-
#include<stdio.h>
Pag
e 99
#include<conio.h>
void main();
struct student
{
int sno;
char name[30];
}stud;
void readdata()
{
printf("enter code");
scanf("%d",&stud.sno);
printf("enter name");
scanf("%s",&stud.name);
}
void printdata()
{
printf("%d",stud.sno);
printf("%s",stud.name);
}
void main()
{
struct student stud;
clrscr();
readdata();
printdata();
getch();
}
Pag
e
100
Output:-
Pag
e
102
Q74.Program to solve the exponential of any number:-
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,k,ans;
clrscr();
printf("enter a number");
scanf("%d",&i);
printf("enter exponential of given number");
scanf("%d",&j);
ans=i;
for(k=2;k<=j;k++)
{
ans=ans*i;
}
printf("result=%d",ans);
getch();
}
Output:-
Pag
e
103
Q75.Program to make a table of given number:-
#include<stdio.h>
#include<conio.h>
void main()
{
int i,n,product;
clrscr();
printf("enter the no\n");
scanf("%d",&n);
for(i=1;i<=10;i++)
{
product=n*i;
printf("%d x %d=%d\n",n,i,product);
}
getch();
}
Output:-
Pag
e
104
Q76 Program to get the ascii value of a character:--
#include<stdio.h>
#include<conio.h>
void main()
{
char ch;
clrscr();
printf("enter a character");
scanf("%c",&ch);
printf("ascii value of given character is %d\n",ch);
getch();
}
Output:-
Pag
e
105
Q77 Program to access the elements of two dimensional
array:-
#include<stdio.h>
#include<conio.h>
void main()
{
int a[3][3],i,j;
clrscr();
printf("enter the array elements");
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("the elements of array are");
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
printf("%d",a[i][j]);
}
printf("\n");
}
getch();
Pag
e
106
}
Output:-
Pag
e
107
Q78.Program of linear search:-
#include<stdio.h>
#include<conio.h>
void main()
{
int a[100] ,n,i,item,loc=-1;
clrscr();
printf("\n how many elements");
scanf("%d",&n);
printf("\n enter the numbers");
for(i=0;i<=n-1;i++)
{
scanf("%d",&a[i]);
}
printf("enter the no to be search\n:");
scanf("%d",&item);
for(i=0;i<=n-1;i++)
{
if(item==a[i])
{
loc=i;
break;
}
Pag
e
108
}
if(loc>=0)
printf("\n%d is found in position %d",item ,loc+1);
else
printf("\n item does not exist");
getch();
}
Output:-
Pag
e
109
Q79.Program to illustrate the use of strcpy()function:-
#include<conio.h>
#include<stdio.h>
#include<string.h>
void main()
{
char name1[10],name2[10];
clrscr();
printf("enter the name");
scanf("%s",name1);
strcpy(name2,name1);
printf("%s\n",name1);
printf("%s\n",name2);
getch();
}
Output:-
Pag
e
110
Q80. Program to illustrate the use of strlen()fuction:-
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char name[30]="my first program";
int name1;
clrscr();
name1=strlen(name);
printf("\n name= %s its length=%d",name,name1);
getch();
}
Output:-
Pag
e
111
Q81. Program for nested structure:-
#include<stdio.h>
#include<conio.h>
struct product
{
char pname;
int qty;
};
struct customer
{
char name[20];
struct product prod;
};
void main()
Pag
e
112
{
struct customer cust;
clrscr();
printf("\n enter customer name");
scanf("%s",cust.name);
printf("\n enter product name");
scanf("%s",&cust.prod.pname);
printf("enter product qty");
scanf("%d",&cust.prod.qty);
printf("\n\n\n custmer/product detail");
printf("\n customer name:%s",cust.name);
printf("\n product sold:%s",cust.prod.pname);
printf("\n product qty : %d",cust.prod.qty);
getch();
}
Output:-
Pag
e
113
Q82. Program to initialize of structure variables:-
#include<stdio.h>
#include<conio.h>
struct student
{
int roll;
char name[20];
int total;
};
void main()
Pag
e
114
{
struct student s={123,"neha",50};
clrscr();
printf("\n roll number=%d",s.roll);
printf("\n name=%s",s.name);
printf("\n total marks=%d",s.total);
getch();
}
Output:-
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,n,fact;
float sum=0;
clrscr();
Pag
e
115
printf("enter the number");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
fact=1;
for(j=1;j<=i;j++)
fact=fact*j;
sum=sum+((float)i/fact);
}
printf("sum of series is %f",sum);
getch();
}
Output:-
#include<stdio.h>
#include<conio.h>
void main()
{
printf("enter your age");
scanf("%d",&age);
Pag
e
116
if(age>=18)
printf("you can vote");
else
printf("you cannot vote");
getch();
}
Output:-
Pag
e
117
clrscr();
printf("enter the no:");
scanf("%d",&n);
printf("prime factor is :");
for(i=2;i<=n;i++)
if(n%i==0)
{
printf("%d",i);
n=n/i;
i--;
}
getch();
}
Output:-
#include<stdio.h>
#include<conio.h>
void main()
{
Pag
e
118
int i=0,n;
clrscr();
printf("enter the number");
scanf("%d",&n);
while(i<=n)
{
if(i%2==0)
printf("%d",i);
i=i+2;
}
getch();
}
Output:-
Pag
e
119
#include<stdio.h>
#include<conio.h>
void main()
{
int array[25],i,j,k,t,n;
clrscr();
printf("how many element in an array\n");
scanf("%d",&n);
printf("enter the element in the array\n");
for(i=0;i<n;i++)
{
scanf("%d",&array[i]);
}
printf("\n entered array is as follows:\n");
for(i=0;i<n;i++)
{
printf("%d\t",array[i]);
}
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
if(array[i]>array[j])
{
t=array[i];
array[i]=array[j];
array[j]=t;
}
}
}
printf("\nsorted array is as following:\n");
for(i=0;i<n;i++)
{
Pag
e
120
printf("%d\t",array[i]);
}
getch();
}
Output:-
Pag
e
121
Q88. Program to find the total memory occupied by structure
variable
#include<stdio.h>
#include<conio.h>
struct product
{
intpcode;
intqty;
float price;
};
void main()
{
struct product p={102,5,35.50};
int size;
clrscr();
printf("\nproduct code=%d",p.pcode);
printf("\nproduct quantity=%d",p.qty);
printf("\nproduct price=%f",p.price);
size=sizeof(p);
printf("\ntotal memory occupied by structure variables in bytes is
%d",size);
getch();
}
Output:-
Pag
e
122
Q89 program to find the sum of marks obtained in different
subject:-
#include<stdio.h>
#include<conio.h>
void main()
{
int marks[3][3];
int i,n,j,sum;
sum=0;
clrscr();
printf("enter the marks of comp,phy&math.\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
scanf("%d",&marks[i][j]);
}
printf("\n\n entered marks of comp,phy&math are:\n\n");
for(i=0;i<3;i++)
{
printf("student %d has\n",i+1);
for(j=0;j<3;j++)
{
printf("\t%d",marks[i][j]);
sum+=marks[i][j];
}
printf("\t total is :%d\n",sum);
sum=0;
}
getch();
}
Pag
e
123
Output:-
Pag
e
124
Q90. program for the removal of duplicate element of an
array.:-
#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,j,k;
int a[20],count=0;
clrscr();
printf("how many element in the array");
scanf("%d",&n);
printf("enter the element:\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n;i++)
for(j=i+1;j<(n-count);j++)
if(a[i]==a[j])
{
for(k=j;k<(n-count);k++)
a[k]=a[k+1];
count++;
i--;
}
printf("the array after the removal of dublicate element is:\n");
for(i=0;i<(n-count);i++)
printf("%d\n",a[i]);
getch();
Pag
e
125
}
Output:-
Pag
e
126
Q91. –program to find transpose of matrix:-
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10][10],b[10][10],i,j,m,n;
clrscr();
printf("enter the rows\n:");
printf("no of rows");
scanf("%d",&n);
printf("enter the column\n:");
scanf("%d",&m);
printf("enter the matrix element \n");
for(i=0;i<=n-1;i++)
{
for(j=0;j<=m-1;j++)
{
scanf("%d\n",&a[i][j]);
b[j][i]=a[i][j];
}
}
printf("matrix a was\n");
Pag
e
127
for(i=0;i<=n-1;i++)
{
for(j=0;j<=m-1;j++)
{
printf("%d\n",a[i][j]);
}
}
printf("transpose ofmatrix a is \n ");
for(i=0;i<=m-1;i++)
{
for(j=0;j<n-1;j++)
{
printf("%d\n",b[i][j]);
}
}
getch();
}
Output:-
Pag
e
128
Q92. program of binary search:-
#include<stdio.h>
#include<conio.h>
int search;
int flag;
int input(int*,int);
int array[20];
Pag
e
129
int binary_search(int*,int,int);
void display(int *,int);
int binary_search(int array[],int l,int s)
{
int found=0;
int mid,low=0,high=l;
mid=(low +high)/2;
while((!found)&&(high>=low))
{
if(s==array[mid])
found=1;
else if (s<mid)
high=mid-1;
else
low=mid+1;
mid=(low+high)/2;
}
return found;
}
void display(int array[],int n)
{
int i;
for(i=0;i<n;i++)
{
printf("%d",array[i]);
}
}
int input(int array[],int no)
{
int i;
printf("how many items in the array");
scanf("%d",&no);
for(i=0;i<no;i++)
Pag
e
130
{
scanf("%d",&array[i]);
}
return no;
}
void main()
{
int no,result,key,array[20];
clrscr();
no=input(array,no);
key=search;
printf("\n entered array as follows\n");
display(array,no);
printf("\n item to be searched:");
scanf("%d",&key);
result=binary_search(array,no,key);
printf("result of searching s=%d",result);
printf("\n in the following array\n");
display(array,no);
getch();
}
output:-
Pag
e
131
PROG 9.20 PROGRAM TO ILLUSTRTATE THE USE OF STREN()
FUNCTION
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char string1[20], string2[20];
clrscr();
strcpy(string1,"Manish");
printf("\n String1 = %s", string1);
strcpy(string2, string1);
printf("\n String2 = %s",string2);
Pag
e
132
getch();
}
OUTPUT-
Pag
e
133
#inciude<conio.h>
#include<string.h>
void main()
{
char string1 [20], strin92[20];
clrscr();
strcpy(string1, “Manish”);
printf(“\n String1 = %s “, string1);
strcpy(stringZ, string1);
printf(“\n String2 = %s “,string2);
getch();
}
OUTPUT
Pag
e
134
PROG9.23 PROGRAM TO ILLUSTRATE THE USE OF STRCAT()
FUNCTION
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char string1[] ="Manish";
char string2[25] ="Sunil";
clrscr();
strcat(string2, string1);
printf("\n String1=%s", string1);
Pag
e
135
printf("\n String2=%s", string2);
getch();
}
OUTPUT-
Pag
e
136
scanf("%s",&str2);
i=0;
bl=1;
while(bl)
{
if(str1[i] !='\0')
str3[i]=str1[i];
else
bl=0;
i++;
}
j=i-1;
bl=1;
k=0;
while(bl)
{
if(str2[k]!='\0')
Pag
e
137
str3[j]=str2[k];
else
bl=0;
k++;
j++;
}
printf("%s",str3);
getch();
}
OUTPUT-
Pag
e
138
PROG10.1-
#include<stdio.h>
#include<conio.h>
void main()
{int i=25;
int *j;
j=&i;
clrscr()
printf("\n address of i=%u",&i);
printf("\n address of i=%u",&j);
printf("\n address of j=%u",&j);
printf("\n address of j=%u",&j);
printf("\n address of i=%d",&i);
printf("\n address of i=%d",(&i));
printf("\n address of i=%d",&i);
getch();
Pag
e
139
OUTPUT-
#include<stdio.h>
#include<conio.h>
void main ()
{
int a: 40, b = 10;
void addab (int *x, int *y);
clrscr ();
Pag
e
140
printf ("\n before function call a =%d, b =%d",a,b);
addab (&a,&b);
printf ("\n\n after function call a =%d, b =%d",a, b);
getch ();
}
*x=*x+10;
*y=*y + 10;
Pag
e
141
}
OUTPUT-
#include<stdio.h>
#include<conio.h>
void main ()
{
int a: 40, b = 10;
void addab (int *x, int *y);
clrscr ();
printf ("\n before function call a =%d, b =%d",a,b);
Pag
e
142
addab (&a,&b);
printf ("\n\n after function call a =%d, b =%d",a, b);
getch ();
}
*x=*x+10;
*y=*y + 10;
Pag
e
143
OUTPUT-
PROG10.4
/* Program to illustrate the use of a structure pointer*/
#include<stdio.h>
#include<conio.h>
struct invent
{
char *name[20];
int number;
Pag
e
144
float price;
};
void main()
{
struct invent product[3], *ptr;
clrscr();
printf("INPUT\n\n");
for(ptr=product;ptr<product+3;ptr++)
{
scanf("%s%d%f",&ptr->name,&ptr->number,&ptr->price);
}
printf("\nOUTPUT\n\n");
ptr = product;
while(ptr<product+3)
{
printf("%-20s%5d%10.2f/n",ptr->name,ptr->number,ptr-
>price);
ptr++;
Pag
e
145
}
getch();
}
OUTPUT-
PROG-10.5
/* Program to access the element 0f array using pointer*/
#include<stdio.h>
#include<conio.h>
void main()
Pag
e
146
{
int array1[5] = {1,2,3,4,5};
int i,*ptr;
clrscr();
ptr=&array1[0];
for(i=0;i<5;i++)
{
printf("%d-%d\n",ptr,*ptr);
ptr++;
/*increments the pointer to the next element and not to the
next memory location*/
}
getch();
}
OUTPUT-
Pag
e
147
PROG10.7-
/*Program that uses different way to access the element of
arrray*/
#include<stdio.h>
#include<conio.h>
void main()
{
int array1[5]={1,2,3,4,5};
int i;
Pag
e
148
int *ary=array1;
clrscr();
for (i=0;i<5;i++,ary++)
{
printf("%d%d",array1[i],*(array1+i));
printf("%d%d%d\n",*(i+array1),i[array1],*ary);
}
getch();
}
OUTPUT-
Pag
e
149
PROG-10.8 PROOGRAM THAT PASSES AN ARRAY TOA
FUNCTION
#include<stdio.h>
#include<conio.h>
void sort_it(int*, int); /*function prototype*/
void main()
{
int a[20],i,n;
clrscr();
printf("enter the number of elements in the array:");
scanf("%d",&n);
printf("enter the elements:\n");
for(i=0;i<n;i++)
scanf("%d",a+i);
sort_it(a,n); /*call to function sort it by passing array a*/
printf("\nThe sorted array:\n");
for(i=0;i<n;i++)
{
Pag
e
150
printf("%d",*(a+i));
}
sort_it(int *b,n) /*sorts the array in ascending order */
int i=0,j,f=0,temp;
while(i++<n&& !f)
{
f=1;
for(j=0;j<n-i;j++)
if (b[j]>b[j+1])
{
temp = b[j];
b[j]=b[j+1];
b[i+1]=temp;
f=0;
}
}
getch();
Pag
e
151
}
OUTPUT-
Pag
e
152
{
int i,j;
char *temp;
for(i=0;i<n;i++)
for(j=0;j<n-1; j++)
{
if(strcmp(person[j],person[j+1])>0)
{
/*exchange pointers*/
temp=person [j];
person[j]= person[j+1];
person[j+1] = temp;
}
}
}
void main()
{
Pag
e
153
int i,n=0;
char*person[100];
char choice;
clrscr();
do
{
/* allocate space for a string*/
person[n]=(char*) malloc(40);
printf("enter name:");
scanf("%s",person[n++]);
printf("Enter another (y/n)?");
scanf("%c",&choice);
}
while(choice=='y');
printf("unsorted list:");
for(i=0;i<0;i++)
printf("/n%s",person[i]);
Pag
e
154
sortbyptrexchange(person,n);
printf("\nSorted list:");
for(i=0;i<n;i++)
printf("\n%s",person[i]);
for(i=0;i<n;i++)
free (person[i]);
getch();
}
OUTPUT-
Pag
e
155
#include<conio.h>
void main(void)
{
int *ptr;
int **ptrtoptr; /*pointer to int pointer */
int data;
clrscr();
ptr=&data; /*ptr now points to data */
ptrtoptr=&ptr; /*ptrtoptr points to ptr */
*ptr = 100; /* Same as data =1OO */
printf("Variable 'data' contains:%d\n",data); /*outputs 100 */
**ptrtoptr=200; /*Same as data =2OO */
printf("Variable 'data' contains:%d\n", data); /*outputs 200 */
data=300;
printf("ptrtoptr is pointing to :%d\n",**ptroptr); /* outputs
300 */
getch();
}
Pag
e
156
OUTPUT-
Pag
e
157
printf("address contents\n");
while(*(city+i)!="\0") /* check for end of string */
{
printf("%d %c %c %c \n",(city +i), city[i], *(city+i), i[city]);
i++;
}
getch();
}
OUTPUT-
PROG-10.12-
/* A program to demonstrate how to obtain address at a
tunct'ion.*/
#include<stdio.h>
void display();
Pag
e
158
main()
{
int display();
printf("address of function is %d",display);
display(); /* usual way of invoking a tunction */
}
void display()
{
printf("Hello User");
}
OUTPUT-
PROG10.13-
/*program to demonstrate invokmg of a function using
pointer*/
#include <stdio.h>
int sum (int,int);
Pag
e
159
main()
{
int a,b,c;
int (*p) (int,int);
printf("\n Enter two nos");
scanf("%d %d", &a, &b);
p=sum;
c=(*p)(a,b);
printf("\n Sum=%d", c);
}
Pag
e
160
PROG-11.1 No arguments and no return values
/* No arguments and no return values */
#include<stdio.h>
#include<conio.h>
void main()
{
void sum();
clrscr();
printf("\t\tThis program calculates the sum of two
variables\n\n\n");
Pag
e
161
sum();
getch();
}
void sum()
{
int x,y,result=0;
printf("Enter the first value");
scanf("%d",&x);
printf("Enter the second value");
scanf("%d",&y);
result=x+y;
printf("\n\nThe sum is%d",result);
}
OUTPUT-
Pag
e
162
PROG11.2- Arguments and no return values
/*Arguments and no return values.*/
#include<stdio.h>
#include<conio.h>
void main()
{
void sum(int, int);
int x, y;
clrscr();
Pag
e
163
scanf("%d",&x);
sum(x,y);
getch();
}
void sum(int a,int b)
{
int result =0;
Pag
e
164
result =a+b;
PROG11.3-
/*Arguments and no return values.*/
#include<stdio.h>
#include<conio.h>
void main()
{
int sum(int, int);
Pag
e
165
int x, y,result=0;
clrscr();
printf("\t\tThis program calculates the sum of two
variables\n\n\n");
printf("Enter the first vaiue");
scanf("%d",&x);
Pag
e
166
s=a+b;
return(s);
}
OUTPUT-
PROG-11.4-
#include<stdio.h>
#include<conio.h>
main()
{
void cal();
clrscr();
adder ();
cal();
}
Pag
e
167
void cal()
{
adder();
}
adder();
{
printf("\this comes from adder....");
getch();
}
OUTPUT-
PROG11.5-
/*Program to find the largest value in an array*/
#include<stdio.h>
#include<conio.h>
void main()
Pag
e
168
{
float largest();
clrscr( );
static float value[4]={2,5,-4.75,1.2};
printf("%f\n",largest[value,4]);
getch();
}
float largest(a,n)
float a[];
int n;
{
int i;
float max;
max = a[0];
for(i=1; i<n; i++)
if (max<a[i])
max=a[i];
Pag
e
169
return (max);
}
OUTPUT-
PROG 11.6-
/*Program to calculate the sum of array elements*/
#include<stdio.h>
#include<conio.h>
int sumofarray(int[],int);
void main()
{
Pag
e
170
int a[10], sum=0;
int i,n;
clrscr();
printf("How many elements of the array?\n");
scanf("%d",&n);
fflush(stdin);
printf("enter the elements of the array \n");
for(i=0; i<n; i++)
{
scanf("%d",&a[i]);
}
sum = sumofarray(a,n);
printf("The sum of array elements is%d",sum);
getch();
}
int sumofarray(int p[], int k)
{
Pag
e
171
int i,s=0;
for(i=0;i<k;i++)
{
s=s+p[i];
}
return(s);
}
OUTPUT-
Pag
e
172
PROG 11.7-
/*Program to traverse a two dimensional array*/
#include<stdio.h>
#include<conio.h>
int i,j;
int mat[10][10];
void Traverse (int,int);
void input(int,int);
void Traverse (int row,int col)
{
printf("\nTraversing in row order\n");
for(i=0; i< row; i++)
{
for(j=0; j< col; j++)
{
printf("mat[%d][%d]=",i,j);
printf("%d",mat[i][j]);
Pag
e
173
}
printf("\n");
}
printf("\nTraversing in column order\n");
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
printf("mat[%d][%d]=",i,j);
printf("%d",mat[i][j]);
}
printf("\n");
}
}
void input(int row,int col)
{
for(i=0;i<row; i++)
Pag
e
174
{
for( j= 0;j<col;j++)
{
printf("\n enter the value for : %d; %d",i+1,j+1);
scanf("%d",&mat[i][j]);
}
}
}
void main()
{ int r,c;
clrscr();
printf("\nEnter the number of rows:");
scanf("%d",&r);
printf("Enter the number of cols");
scanf("%d",&c);
input(r,c);
Traverse(r, c);
Pag
e
175
getch();
}
OUTPUT-
Pag
e
176
Pag
e
177
PROG11.15-
/* Program to find the maximum and minimum from a list */
#include<stdio.h>
#include<conio.h> . .
void max_min(int*,int,int,int);
void max_min(int l[], int n, int max, int min)
{
int i;
max= min=l[0];
for(i = 1; i< n;i++)
{
if(l[i] > max)
max = l[i];
if(l[i] < min)
min=l[i];
}
printf("\n Maximum =%d",max);
Pag
e
178
printf("\n Minimum =%d",min);
}
void main()
{
int i,
list[50];
int n,max, min;
clrscr();
printf("\n How many elements in the list.");
scanf("%d",&n);
printf("\n Enter the elements of the list");
for(i =0; i < n; i++)
scanf("%d", &list[i]);
max_min(list, n, max, min);
getch();
}
OUTPUT-
Pag
e
179
PROG-11.17-
/*PrOgram to make a table of given no. using recursion.*/
#include<stdio.h>
#include<conio.h>
int table(int,int);
void main()
{
int i,n;
clrscr();
printf("Enter any integer to make table");
scanf("%d",&n);
for(i=1;i>=10;i++);
printf("%d x %d=%d \n",n,i,table(n,i));
getch();
}
Pag
e
180
int table(int n,int i)
{
if(i==1 )
{
return(n);
}
else
{
return(table(n,i-1)+n);
}
}
OUTPUT-
Pag
e
181
PROG11.18-
/*Program to find the greatest comman divisor*/
#include<stdio.h>
#include<conio.h>
int greatcd(int , int);
int greatcd(int n, int m)
{
if((n>=m)&&((n% m)==0))
return(m);
else
greatcd(m,(n%m));
}
void main()
{
int n, m;
int result;
clrscr();
Pag
e
182
printf("\nEnter the first integer number:");
scanf("%d",&n);
printf("\n Enter the second integer number:");
scanf("%d",&m);
result=greatcd(n, m);
printf("\n Greatest Comman Diviser of : %d and %d is =
%d",n,m,result);
getch();
}
OUTPUT-
PROG12.1-
/*Program to illustrate the concept of passing structure
members to function.*/
#include<stdio.h>
#include<conio.h>
Pag
e
183
#define cur_yr 2003
float increment(float sal, int year, int inc)
{
if (cur_yr-year>25)
{
sal += inc;
return(sal);
}
}
void main()
{
typedef struct
{
int day;
int month;
int year;
}
Pag
e
184
date;
typedef struct
{
char name[25];
date birthday;
float salary;
}
emprec;
int n=500;
emprec per={"Manish K. Pandey",8,11,1971,8000.00};
clrscr();
printf("\nEmployee Details \n");
printf("Name: %s \n", per.name);
printf("Birthdate:%3d/%3d
%4d\n",per.birthday.day,per.birthday.month,per.birthday.year
);
printf("Salary:%7.2f\n",per.salary);
per.salary=increment(per.salary,per.birthday.year,n);
Pag
e
185
printf("\nEmployee Details\n");
printf("Name: %s \n",per.name);
printf("Birthdate:%3d/%3d/
%4d\n",per.birthday.day,per.birthday.month,per.birthday.year
);
printf("SaIary:%7.2f\n",per.salary);
getch();
}
OUTPUT-
PROG12.2-
Pag
e
186
/*Program to illustrate passing structure to functions*/
#include<stdio.h>
#include<conio.h>
typedef struct
{
int day;
int month;
int year;
}date;
typedef struct
{
char name[20];
date birthday;
float salary;
Pag
e
187
}
employee;
void readfunction(employee);
void printfunctin(employee);
void readfunction(employee rec)
{
printf("\nEnter the name:");
scanf("%s",rec.name);
printf("\nEnter the birthdate;");
printf("\nEnter the day :");
scanf("%d",&rec.birthday.day);
printf("\nEnter the month :");
scanf("%d",&rec.birthday.month);
printf("\nEnter the year :");
scanf("%d",&rec.birthday.year);
printf("\nEnter the salary:");
scanf("%f",&rec.salary);
Pag
e
188
}
void printfunction(employee emp)
{
printf("\n\n\nEmployee Details\n");
printf("\nName:%s\n",emp.name);
printf("Birthdate:%3d/%3d/
%4d\n",emp.birthday.day,emp.birthday.month,emp.birthday.
year);
printf("Salary:%7.2f\n",emp.salary);
}
void main()
{
employee emp;
clrscr();
readfunction(emp);
printfunction(emp);
getch();
}
Pag
e
189
OUTPUT-
PROG12.3-
#include<stdio.h>
void main()
{
union
{
int i;
char c;
float f;
};
i = 10;
c = 9;
f = 4.5;
printf("The value of i is %d\n",i);
}
Pag
e
190
NO OUTPUT
PROG 12.4-
/*Program to compare the allocate memory using struct and
union*/
#include<stdio.h>
#include<conio.h>
struct
{
char name[25];
int code;
float salary;
}
emp;
union
{
char name[25];
int code;
float salary;
Pag
e
191
}
employee;
void main()
{
clrscr();
printf("The size of the structure is %d \n", sizeof(emp));
printf("The size of the union is %d",sizeof(employee));
getch();
}
OUTPUT-
Pag
e
192
{
int sno;
char name[25];
}
stud;
void readdata()
{
printf ("Enter code");
scanf ("%d",&stud.sno);
printf("Enter name");
scanf("%s",&stud.name);
}
void printdata()
{
printf("%d",stud.sno);
printf("%s",&stud.name);
}
Pag
e
193
void main()
{
struct student stud;
clrscr();
readdata();
printdata();
getch();
}
OUTPUT-
PROG12.6-
Pag
e
194
#include<stdio.h>
#include<conio.h>
struct employee
{
int empcode;
char name[25];
float salary;
};
void main()
{
void display(struct employee);
struct employee emp;
clrscr();
Pag
e
195
printf("Enter employee code:");
scanf("%d", &emp.empcode);
printf("Enter employee name");
scanf("%s",emp.name);
printf("Enter employee salary");
scanf("%f",&emp.salary);
display(emp);
getch();
}
Pag
e
196
}
OUTPUT-
PROG12.10-
/*Program to assign the values oi a structure to another
structure.*/
#include<stdio.h>
#include<conio.h>
Pag
e
197
struct student
{
int roll; char name[20];
int total;
};
void main()
{
struct student stud1= {8291,"pal",85};
struct student stud2;
clrscr();
stud2=stud1;
printf("\nRoll Number =%d",stud2.roll);
printf("\nName =%s",stud2.name);
printf("\nTotal Marks = %d" ,stud2.total);
getch();
Pag
e
198
}
OUTPUT-
PROG 12.11-
/*Program to compare the value of structure varibles*/
#include<stdio.h>
#include<conio.h>
struct emp
{
int code;
char name[20];
Pag
e
199
float salary;
};
void main()
{
struct emp e1 = {110,"john",91};
struct emp e2= {110,"pal",85};
clrscr();
if(e1.code==e2.code)
printf("both employees has same code");
else {
printf("\ncode=%d",e1.code);
Pag
e
200
printf("\nName = %s",e1.name);
printf("\nsalary = %f",e1.salary);
printf("\n\nSecond Employee");
printf("\nCode =%d",e2.code);
printf("\nName = %s",e2.name);
printf("\nSalary =%f",e2.salary);
}
getch();
}
OUTPUT-
Pag
e
201
PROG12.12-
/*program to find the total memory occupied by structure
variable*/
#include<stdio.h>
#include<conio.h>
struct product
{
int pcode;
int qty;
float price;
};
void main()
{
struct product p={102,5,35.50};
int size;
clrscr();
printf("\nproduct code=%d",p.pcode);
printf("\nproduct quantity =%d",p.qty);
Pag
e
202
printf("\nproduct price =%f",p.price);
size=sizeof(p);
printf("\n total memory occupied by structure varibles in
bytes is %d,size");
getch();
}
OUTPUT-
Pag
e
203
Pag
e
204
Pag
e
205