C programs
C programs
#include<stdio.h> Back
#include<conio.h>
main( ) 72. Program which does the below process
{ after reading on odd no of integer.
struct hotel a) a) Print them in given order.
{ b) b) Replace second elements by
char name[20]; product of first and last element
char city[10]; c) c) Replace middle value by average
char grade; of all elements.
int rc,nr; d) d) Replace all –ve no’s by zero’s.
};
struct hotel ht[20],t; #include<stdio.h>
int i,n,j,c; #include<conio.h>
char gr; main( )
clrscr( ); {
printf(“enter no. of hotels\n”); int a[10],i,n,sum=0;
scanf(“%d”,&n); clrscr( );
for(i=0;i<n;i++) printf(“enter the array sixe “);
{ scanf(“%d”,&n);
printf(“enter name of hotel \n”); printf(“enter the elements”);
scanf(“%s”,&ht[i].name); for(i=0;i<n;i++)
printf(“enter name of city \n”); {
scanf(“%s”,&ht[i].city); scanf(“%d”,&a[i]);
printf(“enter the grade \n”); sum=sum+a[i];
scanf(“%s”.ht[i].grade); }
ht[i].grade=getche( ); printf(“The given arrays is: “);
printf(“enter room charge \n”); for(i=0;i<n;i++)
scanf(“%d”,&ht[i].rc); printf(“%d”,a[i]);
a[2]=a[1]*a[n-1]; #include<conio.h>
printf(“\n the given areay after #include<math.h>
replacing 2nd element is”); main( )
for(i=0;i<n;i++) {
printf(“%d”,a[i]); int a,b,n,evensum=0,oddsum=0,div;
a[(1+n/2)]=sum/n; clrscr( );
printf(“\n the given array after printf(“enter a number”);
replacing middle element by average scanf(“%d”,&n);
of all”); a=n;
for(i=0;i<n;i++) b=n/10;
if(a[i]<0) while(a>0)
a[i]=0; {
printf(“\n given array after replacing oddsum=oddsum+(a%10);
–ve values by zero”); a=a/10;
for(i=0;i<n;i++) }
printf(“%d”,a[i]); while(b>0)
printf(“\n”); {
getch(); evensum=evensum+(b%10);
} b=b/10;
Back }
div=abs(evensum-oddsum);
73. Program to sort the entered elements if(div%11==0)
using selection sort technique. printf(“The number is divisible by 11”);
else
#include<stdio.h> printf(“The number is not divisible by
#include<conio.h> 11”);
main( ) getch();
{ }
int a[100],i,n,j,t,min,pos; Back
clrscr();
printf(“enter the array size”); 75. Program to find maximum and
scanf(“%d”,&n); minimum of entered ’n’ number using
printf(“enter the elements”); arrays.
for(i=0;i<n;i++)
scanf(“%d”,&a[i]); #include<stdio.h>
for(i=0;i<n;i++) #include<conio.h>
{ main( )
min=a[i]; {
pos=i; int i,n,a[10],min,max;
for(j=0;j<n-1;j++) clrscr( );
if(min>a[j]) printf(“ enter how many number”);
{ scanf(“%d”,&n);
min=j; printf(“enter the elements”);
pos=j; for(i=0;i<n;i++)
} scanf(”%d”,&a[i]);
t=a[i]; min=a[0];
a[i]=a[pos]; for(i=0;i<n;i++)
a[pos]=t; if(min>a[i])
} min=a[i];
printf(“the sorted elements are”); printf(“minimum=%d”,min);
for(i=0;i<n;i++) max=0;
printf(“%2d”,a[i]); for(i=0;i<n;i++)
getch( ); if(max<a[i]);
} max=a[i];
Back printf(“\n maximum=%d”,max);
getch( );
74. Program to find whether a number is }
divisible by ‘11’ or not without actual division. Back
- #include<stdio.h>
6-10 6- #include<conio.h>
10 void read_array(int x[]);
void print_array(int y[]);
void rev_array(int z[]);
- main()
11-15 11- {
15 int a[5];
clrscr();
- read_array(a);
16-20 16- printf_array(a);
20 rev_array(a);
getch( );
}
- void read_array(int x[])
21-25 21- {
25 int i;
for(i=0;i<=4;i++)
{
- printf(“enter values for a[%d]:”,i);
scanf(“%d”,&x[i]); for(i=0;i<l;i++)
} {
} if(s[i]!=’ ‘)
void print_array(int y[]) {
{ nc=0;
int i; while(s[i]!=’ ‘)
for(i=0;i<=4;i++) {
printf(“%d”,y[i]); nc++;
} printf(“%c”,s[i]);
void rev_array(int z[]) i++;
{ if(s[i]=’\0’)
int i; break;
for(i=4;i>=0;i--) }
printf(“\n%d”,z[i]); printf(“\t\t %d”,nc);
} printf(“\n”);
Back }
}
79. Program to accept values into single getch();
dimensional array and print the array in }
reverse by using pointers. Back
#include<stdio.h>
#include<conio.h>
main( ) 85.Program to accept a string and print
{ reverse of the given string by using functions.
int ch[20];
clrscr ( ); #include<stdio.h>
printf(“enter a string”); #include<stdio.h>
read_array(ch); int getline (char str[]);
printf(“%s”,ch); void printline (char str[],int i);
getch( ); main( )
} {
void read_string (char*pt) char str[80];
{ int 1;
for(;(*pt=getchar( ))!=’/n’;pt++); clrscr( );
*pt=’\0’; 1=getline(str );
} printline(str,1);
Back printline(str,1);
getch ( );
83.Program to read a string and print the first }
two characters of each word in the string. int getline(char str[])
{
#include<stdio.h> int 1;
#include<conio.h> printf(“enter a string;”);
main( ) for(i=0;i<80&&((str[i]=getchar())!=’\n’);i++);
{ if(str[i]=’\0’;
char s[100]; return i;
int i,l; }
clrscr( ); void printline(char str[],int 1)
printf(“enter a string”); {
gets(s);l=strlen(s); int j;
for(i=0;i<l;i++) for(j=1;j<=0;j--)
{ printf(“%c”,str[j]);
if(s[i]!=’ ‘ && s[i]=’ ‘) printf(‘is the revefrse string”);
{ }
printf(“%c %c”,s[i],s[i+1]) Back
i=i+2;
while(s[i]!=’ ‘) 86. Program to accept two 3 dimensional
i++; array and store subtraction of those two
} arrays into third array..
} #include<stdio.h>
getch( ); #include<conio.h>
} main( )
Back {
int a[3][3],b[3][3],c[3][3],i,j;
84.Program to accept two numbers and print clrscr( );
the sum of given two numbers by using for(i=0;i<3;i++)
pointers for(j=0;j<3;j++)
{
#include<stdio.h> printf(“enter two values for a[%d][%d] &
#include<conio.h> b[%d][%d]:”,i,j,i,j);
main( ) scanf(“%d%d”,&a[i][j],&b[i][j]);
{ }
int a, b,c; for(i=0;i<3;i++)
clrscr( ); {
a=10; for(j=0;j<3;j++)
b=20; {
c=*(&a)+*(&b); c[i][j]=a[i][j]-b[i][j];
printf(“%d”,c); printf(“%d”,,c[i][j]);
getch( ); }
} printf(“\n”);
Back }
getch( );
return i;
Back }
Back
87.Program to accept a single dimensional
array and print them by using pointers 89.Program to print 4 dimentional matrix with
constant number.
#include<stdio.h>
#include<conio.h> #include<stdio.h>
#include<conio.h>
main( ) main( )
{ {
int a[5],*b,i; int a[4][4],i,j,c;
clrscr( ); clrscr( );
b=&a[0]; printf(“enter constant number”);
for(i=0;i<=4;i++) scanf(“%d”,&c);
{ for(i=0;i<4;i++)
printf(“enter the a value for a[%d]”,i) {
scanf(“%d”,b); for(j=0;j<4;j++)
b++; a[i][j]=c;
} for(i=0;i<4;i++)
b=&a[0]; {
for(i=0;i<=4;i++) for(j=0;j<4;j++)
{ printf(“%d”,a[i][j]);
printf(“\n%d”,*b); printf(“\n”);
b++; }
} getch( );
getch( ); }
} Back
Back
90.Prongram to accept a string and print each
88.Program to accept two strings and biggest word in reverse
among them
#include<conio.h>
#include<stdio.h> #include<stdio.h>
#include<conio.h> main( )
int getline(char line[],int lim); {
main( ) char name[80];
{ int i,j,start=0,end,len;
char str1[80],str2[80]; clrscr( );
int len1,len2; printf(“enter a string”);
clrscr( ); scanf(“%s”,name);
printf(“enter first string”); for(i=0;i<80 &&((name[i]=getchar( ) )!=’\n’);i++);
len1=getline(str1,80); len=i;
printf(“enter second string”); for(i=0;i<len;i++)
len2=getline(str1,80); if(name[i]==’ ‘|| name[i]==’\n’)
if(len1 >len2) {
printf(“first string bigger than second string”); end=i;
else while((end--)>=start)
if(len1<len2) {
printf(“second string bigger than first string”); printf(“%c”,name[end]);
else }
printf(“both strings are equal”); start=i+1;
getch( ); }
} getch( );
int getline(char line[],int lim) }
{ Back
int i;
for(Ii0;i<lim && ((line[i]=getchar( ))!=’\n’);i++) 91. Program to accept elements into single
if(line[i]==’\n’) dimensional array and print the array in
line[i]=’\0’; ascending order by using three different
arrays. if(c==eof( ))
break;
#include<conio.h> putc(c);
#include<stdio.h> }
void read_array(int x[]); fclose(fp);
void sort_array(int y[]); fp=fopen(“data.dat”,”r”);
void print_array(int z[]); while(1)
main() {
{ c=getc(fp);
int a[10]; if(c==eof( ))
clrscr( ); break;
read_array(a); putchar(c);
sort_array(a); }
print_array(a); getch( );
getch( ); fclose(fp);
} }
void read_array(int x[]) Back
{
int i; 93. Program to accept data in lower case and
for(i=0;i<10;i++) store the given data into file into upper case
{ and print the data.
printf(“enter value for a[%d]”,i);
scanf(“%d”,&x[i]); #include<conio.h>
} #include<stdio.h>
} main( )
void sort_array(int y[]) {
{ FILE *fp;
int i,j,k; Char c;
for(i=0;i<9;i++) fp=fopen(“data2.dat”,”w”);
for(j=i+1;j<=9;j++) clrscr( );
if(y[i]>y[j]) printf(“enter text”);
{ while((c=getchar( ))!=eof( ))
k=y[i]; {
y[i]=y[j]; putc(toupper(c),fp)
y[j]=k; }
} fclose(fp);
} fp=fopen(“data2.dat”,”r”);
void print_array(int z[]) while(1)
{ {
int i; c=getc(fp);
for(i=0;i<10;i++) if(c==eof( ))
printf(“%d\n”,z[i]); break;
} putchar(c);
Back }
getch( );
92.Program to accept data and store the fclose(fp);
given data into file print the data. }
Back
#include<conio.h>
#include<stdio.h> 94.Program to copy contents of one file into
main( ) another.
{
FILE *fp; #include<conio.h>
char c; #include<stdio.h>
fp=fopen(“data.dat”,”w”); main( )
clrscr(); {
printf(“enter text”); FILE * fp1,*fp2;
while(1) char ch;
{ fp1=fopen(“text1”,”w”);
c=getchar( ); printf(“enter the text”);
while((ch=getchar()!=EOF); for(i=0;i<length;i++)
putc(ch,fp1); {
fclose(fp1); str1[0]-=32;
fp1=fopen(“text1”,”r”); if(str1[i]= =’ ‘)
fp2=fopen(“text2”,”w”); str1[i+1]-=32;
while((ch=getc(fp1))!=EOF) printf(“%c”.str1[i]);
putc(ch,fp2); }
fcolse(fp1); getch();
fcolse(fp2); }
getch( ); int getline(char line [], int lim)
} {
Back int i;
for(i=0;i<lim &&
95. Program to create a file of numbers and ((line[i]=getchar( ))!=’\n’);i++);
copy odd number into second file and even if(line[i]= =’\n’)
number into third file line[i]=’\0’;
return i;
#include<conio.h> }
#include<stdio.h> Back
main( )
{ 97.Program to accept two numbers and
FILE *fp1,*fp2,*fp3; interchange two values using functions.
int i;
fp1=open(“data1”,w”); #include<conio.h>
printf(“enter the number”); #include<stdio.h>
scanf(“%d”,&i); void swap (int a, int b);
while(i!=eof) main( )
{ {
putw(i,fp1); int a,b;
scanf(“%d”,&i); clrscr( );
} printf(“enter value for a;”);
fcolse(fp1); scanf(“%d”,&a);
fp1=fopen(“data1”,”r”); printf(“enter value for b;”);
fp2=fopen(“data2”,”w”); scanf(“%d”,&b);
fp3=fopen(“data3”,”w”); swap(a,b);
while((i=getc(fp1))!=eof) getch( );
if(i%2==0) }
putc(i,fp3); void swap(int a,int b)
else }
putw(i,fp2); int c;
fcolse(fp1); c=a;
fcolse(fp2); a=b;
fcolse(fp3); b=c;
getch( ); printf(“\na=%d”,a);
} printf(“\nb=%d”,b);
Back }
Back
96.Program to accept a string in lower case
and print first character of each word in upper 98.Program for example of static variable.
case.
#include<conio.h>
#include<conio.h> #include<stdio.h>
#include<stdio.h> static int i=1;
main( ) main( )
{ {
char str1[80]; int j;
int length,i; clrscr( );
clrscr( ); for (j=1;j<=5;j++);
printf(“enter a string; “); fun( );
length=getline(str1,80); getch( );
}
fun( )
{
printf(“\n%d”,i);
i=i+1;
}
Back
#include<conio.h>
#include<stdio.h>
main( )
{
char n,n1;
clrscr ( );
printf(“enter a string;”);
while((n=getchar( )!=’\n’)
if(n>=’a’ && n<=’z’)
putchar(n);
else
if(n>=’a’ && n<=’z’)
putchar(n);
getch( );
}
Back
#include<conio.h>
#include<stdio.h>
main( )
{
int a[4][4],i,j,c;
clrscr( );
printf(“enter which number you want;”);
scanf(“%d”,&c);
for(i=0;i<4;i++)
for(j=0;j<4;j++)
if(i+j= =3)
a[i]]j]=c;
else
a[i][j]=0
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
printf(“%d”,a[i][j]);
printf(“\n”);
}
getch( );
}
Back