Dharanyadevi blogspot subsume with E-books, Notes, Lab Manual, Question Banks, Interview Tips, Viva Questions, Basics and Interview Questions for engineering students. For Any Help Contact dharanyadevi@gmail.com

SEARCH

Image

Tuesday, May 15, 2012

First Year Basic C Program Part 2

41. Program to accept a string and check the given string is palindrome or not

main( )

{

int i,lim,c,check=1; char word[80]; clrscr( );

printf( enter a string”);

for(i=0;i<80 && ((word [i]= getchar())!=’\n’);i++);

lim=i-1; c=lim/2; for(i=0;i<=0;i++,lim--) if(word[i]!= word[lim])

{

check=0;

break;

}

if(check= =1)

printf(the given string is palindrome “);

else

printf( not palindrome);

getch( );

}

42. Program to accept values into 3 dimensional array and print.

main( )

{

int a[3][3],i,j; clrscr( ); for(i=0;i<=2;i++) for(j=0;j<=2;j++)

{

printf( enter the value for a[%d][%d] :”,i,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( );

}


43. Program to print upper triangle.

main( )

{

int a[4][4],i,j,c;

clrscr( );

printf( enter which no u want);

scanf(%d,&c); for(i=0;i<4;i++) for(j=0;j<4;j++) if(i

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( );

}

44. Program to accept two 3 dimensional array and store addition of those into arrays into the third array.

main( )

{

int a[3][3],b[3][3],c[3][3],i,j;

clrscr( ); for(i=0;i<3;i++) for(j=0;j<3;j++)

{

printf(“enter the two values for a[%d][%d] & b[%d][%d]”, i,j,i,j);

scanf(%d%d”,&a[i][j],&b[i][j]);

}

for(i=0;i<3;i++)

{

for(j=0;j<3;j++)

{ c[i][j]=a[i][j]+b[i][j]; printf(“%d,c[i][j]);

}

printf(“\n”);

}

getch( );

}

45. Program to accept a string and find the length of the given string by using functions

int getline(char str[]); main( )

{

char str[80]; int length; clrscr( );

printf( enter a string”);

length=getline(str);

printf(length of the given string is %d,length);

getch ( );

}

int getline(char str[])

{

int i;

for(i=0;i<80&&((str[i]=getchar( ))!=’\n’); i++);

if(str[i]= =’\n’) str[i]=’\0’; return i;

}

46. Program to count the number of words, characters, alphabets, vowels, consonants and digit in a line of text.

main( )

{

int noa=0,nob=0,noc=0,nov=0,now=0,noch=0,l,I;

char ch,s[100];

clrscr( );

printf(“enter 2 lines of text”);

gets(s); l=strlen(s); for(i=0;i<1;i++)

{

switch(s[i])

{

case a’: case e: case ‘i: case o’: case u’: case A: case E’: case I’: case O’: case U: nov++; break;

} if(isalpha(s[i])) noa++; if(isdigit(s[i])) nod++;

if(noa[i]==’ ‘) && (noa[i+1]!=’ ‘)

now++;

}

noch=l-nob;

noc=noa-nov;

printf(total no of words %d,now);

printf(total no of characters(without blanks)%d”,noch); printf(total no of characters(including blanks)%d”,l); printf(total no of alphabets %d”,noa);

printf(total no of vowels %d”,nov); printf(total no of characters %d”,noc); printf(total no of digits %d”,nod); getch( );

}

47. Program to accept two string and compare the strings are equal or not

int getline (char line[ ], int lim ); int strc(char str1[ ], char str2[] ); main( )

{

char str1[80],str2[80];

int comp;

clrscr( );

printf(“enter first string:);

getline(str1,80);

printf(“enter second string:”); getline(str2,80); comp=strc(str1,str2); if(comp>0)

printf(first string is bigger”);

else if(comp==0)

printf(both the strings are equal”);

getch( );

}

int getline(char str[], int lin)

{

int i; for(i=0;in&&((str[i]=getchar())!=’\n’);i++); if(str[i]=’\0’)

return i;

}

int strc(char str1[],char str2[])

{

int i; for(i=0;str1[i];i++) if(str1[i]!=str2[i]) return str1[i]-str2[i]; return str1[i]-str2[i];

}

48. Program to sort the entered numbers using bubble sort.

main( )

{

int a[100],i,j,n,t;

clrscr( );

printf(“enter the array size); scanf(%d,&n); for(i=1;i;i++) scanf(%d,&a[i]); for(i=1;i<=n;i++) for(j=i+1;j++)

if(a[i]>a[j])

{ t=a[i] a[i]=a[j]; a[j]=t;

}

printf(the sorted elements are ); for(i=1;i<=n;i++) print(%d,a[i]);

getch( );

}

49. Program to read date,month, year and print the next day’s date,month,year.

main( )

{

int month[12]={31,28,31,30,31,30,31,31,30,31,30,31};

int d,m,y,nd,nm,ny,ndays;

clrscr( );

printf(“enter the date,month,year”); scanf(%d%d%d”,&d,&m,&y); ndays=month[m-1];

if(m==2)

{

if(y%100==0)

{ if(y%400==0) ndays=29;

} else if(y%4==0) ndays=29;

}

nd=nd+1; nm=m; ny= y;

if(nd>ndays)

{ nd=1; nm++;

}

if(nm>12)

{ nm=1; ny++;

}

printf(“Given date is %d:%d:%d\n”,d,m,y); printf(“next days date is %d:%d:%d”,nd,nm,ny); getch( );

}

50. Program to interchange two values using pointers.

void interchange(int *x,int *y);

main( )

{

int a,b;

clrscr( );

printf(“enter values of a and b”); scanf(“%d%d”,&a,&b); interchange(&a,&b);

}

void interchange(x,y)

int *x,*y;

{

int t;

t=*x;

*x=*y;

*y=t;

printf(“%d=x, %d= y”,*x,*y);

getch( );

}

51. Program to print “PASCAL TRIANGLE.

main()

{

int n,p=1,q,num,sp;

clrscr( );

printf(“enter the number of rows); scanf(%d,&n); for(p=0;p<=n;p++)

{

for(sp=1;sp<=40-(3*p);sp++)

printf( “);

for(q=0;q

{ if((q==q)||(q==0)) num=1;

else

num=num*((q-q)+1)/q; printf(“%2d”,num); printf(“\n”);

}}

getch( );

}

52. Program to check whether a given number is perfect or not.

main( )

{

int i,n,s=0;

clrscr();

printf(“enter the number”); scanf(%d,&n); for(i=1;i<n/2;i++) if(n%i==0)

s+=i;

if(s= =n)

printf(the number is perfect no);

else

printf(the number is not perfect “);

getch( );

}

53. Program to check whether a given number is prime number.

main( )

{

int i,n,c=0;

clrscr( );

printf(“enter a number”); scanf(%d,&n); for(i=0;i<=n;i++) if(n%i==0)

c++;

if(c==2)

printf(“given number is a prime number”);

else

printf(“given number is not prime number”);

getch( );

}

54. Program to read ‘n number and print them in matrix terms in all orders.

main( )

{

int i,n,c,p,q,r,k,a[20];

clrscr();

printf(“enter the array size);

scanf(”%d”,&n); printf(“enter the elements”); for(i=1;i<=n;i++) scanf(%d,&a[i]);

i=1;

while(i<=n)

{

if(n%i==0)

{ r=i; c=n/i; k=1;

for(p=1;p<=r;p++)

{ for(q=1;q<=c;q++) printf(“%d,a[k++]) printf(“\n”);

}

i++;

getch( );

}

55. Program to search an element using binary search

main( )

{

int a[100],i,n,x, mid, top, bot,c;

clrscr();

printf(“enter the array size;);

scanf(%d,&n);

printf(“enter the array elements); for(i=1;i<=n;i++) scanf(%d,&a[i]);

top=1; bot=n; c=0;

printf(“enter the element to searched);

scanf(%d,&x);

while((top <=bot)&&(c==0))

{ mid=(top+bot)/2; if(a[mid]<x) top=mid+1;

else

if(a[mid]>x) bot=mid-1; else

c=1;

}

if(c==1)

printf(elements is at position;%d”,mid);

else

printf(elements is not in list”);

getch( );

}

56. Program to accept two numbers and print the sum of given two numbers by using pointers

main( )

{

int a, b,c; clrscr( ); a=10; b=20;

c=*(&a)+*(&b); printf(“%d”,c); getch( );

}

57. Programs to multiply two Matrices

main( )

{

int a[10][10],b[10][10],c[10],[10],i,j,m,n,p,q,k;

clrscr( );

printf(“enter the size of first matrices”);

scanf(“%d%d’,&m,&n);

printf(“enter the size of second matrix”);

scanf(“%d%d’,&p,&q);

if(n==p)

{

printf(“enter first matrices elements”);

for(i=1;i<m;i++) for(j=1;j;j++) scanf(%d,&a[i][j]);

printf(“enter second matri x elements);

for(i=1;ifor(j=1;jscanf(%d,&b[i][j]); for(i=1;i<m;i++) for(j=1;j;j++)

{

c[i][j]=0; for(k=1;k;k++) c[i][j]=c[i][j]+a[i][k]*b[k][j];

}

printf(the multiplication matrix is”);

for(i=1;i<m;i++)

{ for(j=1;j;j++) print(“%2d”,c[i][j]); printf(“\n”);

}

}

else

printf(“multiplication is not possible”);

getch( );

}

58. Program to print prime number between 1-100

main( )

{

int i,n,c; clrscr( ); for(n=1;n<=100;n++)

{

c=0; for(i=1;i<=n;i++) if(n%i==0)

c++; if(c==2) printf(“\n%d”,n);

}

getch( );

}

59. Program to accept a string and find the length of the string

main( )

{

char name[80];

int i;

clrscr( );

printf(“enter a string ;”); for(i=0;i<80&&((name[i]=getchar( ))!=\n);i++); printf(“%d is the size of string”,i);

getch( );

}

60. Program to fibanocci of matrix

# include th.h>

main( )

{

int a[10][10],i,j,m,n sum=0;

float norm;

clrscr( );

printf(enter the matrix size);

scanf(“%d%d”,&m,&n);

printf(“enter the element of matrix”);

for(i=1;i<=m;i++)

for(j=1;j<=n;j++)

{

scanf(%d,&a[i][j]); sum=sum+(a[i][j]*a[i][j])

} norm=sqrt(sum); printf(“norm=%f”,norm); getch( );

}

61. Program a structure which reads n students information (name,3 subjects marks) and calculate total marks, result print them in a particular format.

main( )

{

struct student

{

char name[20];

int m1,m2,m3, tot;

char result[10];

}stud[10]; int i,n; clrscr( );

printf(“enter no of students \n);

scanf(%d,&n);

for(i=0;i;i++)

{

printf(”enter %d student deatails \n,i);

printf(enter name\n); scanf(“%s, stud[i].name); printf(“enter marks of 3 subjects \n”);

scanf(“%d%d%d, &stud[i].m1,&stud[i].m2,&stud[i].m3); stud[i].tot=stud[i].m1+stud[i].m2+stud[i].m3; if((stud[i].m1>35)&&(stud[i].m2>35)&&(stud[i].m3>35)) strcpy(stud[i].result,”pass”);

else strtcpy(stud[i].result,”fail);

}

clrscr( );

printf(“name total result \n);

for(i=0;i;i++)

{

printf(“%s %d %s \n, stud[i].name,stud[i].tot,stud[i].result);

}

getch( );

}

62. Program to find whether a square matrix is a) symmetric b) skew symmetric c) none of two.

main( )

{

int a[10][10],i,j,m,n,c=0,c1=0;

clrscr( );

printf(“enter the array size);

scanf(%d,&n); printf(“enter the elements”); for(i=1;i<=m;i++) for(j=1;j<=n;j++) scanf(%d,&a[i][j]); for(i=1;i<=m;i++) for(j=1;j<=n;j++)

{

if(a[i][j]==a[j][i])

c=1; else if(a[i][j]==a[j][i]) c1=1;

}

printf(the given matrix is \n);

for(i=1;i<=m;i++)

{ for(j=1;j<=n;j++) printf(“%4d”,a[i][j]); printf(“\n”);

}

if(c==0)

printf(the given matrix is symmetric”);

else if(c1==0)

printf(the matrix is skew symmetric”);

else

printf(“none of two”);

}

getch( );

}

63. Program to find area of a triangle when their sides are given.

main( )

{

int a,b,c; float s, area; clrscr( );

printf(“enter their sides of the triangle”); scanf(“%d%d%d”,&a,&b,&c); if((a+b)<c||(b+c)||(a+c)<b) printf(finding area is not possible”);

else s=(a+b+c)/2;

area=sqrt(s*(s-a)*(s-b)*(s-c)); printf(“area=%.2f,area); getch( );

}

64. Program to print Armstrong number between 1-500.

main( )

{

int i,n,s,r; clrscr( ); for(i=1;i<=500;i++)

{

n=i; s=0; while(n>0)

{ r=n%10; s=s+(r*r*r); n=n/10;

}

if(i==s)

printf(“\n%d”,s);

}

getch();

}

65. Program to check whether a given number is Armstrong or not.

main( )

{

int i,n,s,r,k;

clrscr( );

printf(“enter a number”);

scanf(%d,&n);

k=n; s=0; while(n>0)

{ r=n%10; s=s+(r*r*r); n=n/10;

}

if(k==s)

printf(“given number is Armstrong %d”,k);

else

printf(“given number is not Armstrong %d”,k);

}

getch();

}

66. Program to print the floyd’s triangle.

main( )

{

int i,n,s,r k=1;

clrscr( );

printf(“enter a number of rows”); scanf(%d,&n); for(i=1;i<=n;i++)

{

for(s=1;s<=40-i;s++) printf( ”); for(j=1;j<=i;j++) printf(“%3d,k++); printf(“\n”);

}

getch( );

}

67. Program to read data in 3 structures and print

# include<stdio.h>

# includeonio.h>

main( )

{

struct book

{

char code; int piece; float price;

};

struct book b1,b2,b3;

main( )

{

clrscr( );

printf(“enter code,piece,price”); scanf(%c%d%f”,&b1.code,&b1.piece,&b1.price); printf(“enter code,piece,price”); scanf(%c%d%f”,&b2.code,&b2.piece,&b2.price); printf(“enter code,piece,price”); scanf(%c%d%f”,&b3.code,&b3.piece,&b3.price); printf(the details are”);

printf(“\n %c%d%f”,b1.code,b1.piece,b1.price); printf(“\n %c%d%f”,b2.code,b2.piece,b2.price); printf(“\n %c%d%f”,b3.code,b3.piece,b3.price); getch( );

}

68. Program to print a diagonal matrix.

main()

{

int a[4][4],i,j;

clrscr( );

for(i=0;i<4;i++) for(j=0;j<4;j++) if(i==j)

c[i][j]=7; 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();

}

69. Program to copy contents of one file into another.

main( )

{

FILE *fp1,*fp2; char ch; fp1=fopen(text1”,w”); printf(enter the text”); while((ch=getchar( ))!=EOF) putc(ch,fp1);

fclose(fp1);

fp1=fopen(“text1”,”r); fp2=fopen(text2”,w”); while((ch=getc(fp1))!=EOF) putc(ch,fp2);

fclose(fp2);

getch( );

}

70. Program to create a file of number and copy odd number into second file and even number into third file.

main( )

{

FILE *fp1,*fp2,*fp3; int i; fp1=fopen(DATA1,”w”); printf(“enter the number”); scanf(%d,&i); while(i!=eof( ))

{

putw(i,fp1);

} fcolse(fp1); fp1=fopen(“DATA1”,r”); fp2=fopen(DATA2,”w”);

fp3=fopen(DATA3,”w”); while((i=getw(fp1))!=EOF()) if(i%2= =0)

putw(i,fp3); else putw(i,fp2); fcolse(fp1); fcolse(fp2); fcolse(fp3); getch( );

}



71. Program a structure which stores information about hotels which stores information about name, grade, room change, no of rooms.

a)

a)

Print the hotels of given grade in order of roomchange.

b)

b)

Print the hotels with roomchange less than a given change.

main( )

{

struct hotel

{

char name[20]; char city[10]; char grade;

int rc,nr;

};

struct hotel ht[20],t;

int i,n,j,c; char gr; clrscr( );

printf(“enter no. of hotels\n); scanf(%d,&n); for(i=0;i;i++)

{

printf(“enter name of hotel \n”); scanf(%s”,&ht[i].name); printf(“enter name of city \n); scanf(%s”,&ht[i].cit y); printf(“enter the grade \n”); scanf(%s”.ht[i].grade); ht[i].grade=getche( ); printf(“enter room charge \n”); scanf(%d,&ht[i].rc); printf(“enter no of rooms \n); scanf(%d,&ht[i].nr);

} for(i=0;i;i++) for(j=0;j<n-i;j++)

{ t=ht[j]; ht[j]=ht[j+i]; ht[j+1]=t;

}

printf(“enter a grade to print the hotels \n”);

gr=getche();

clrscr();

printf(hotel name city grade roomcharge no of room);

for(i=0;i;i++)

if(gr==ht[i].grade)

printf(“%s %s %c %d %d”,ht[i]. name,ht[i].city,ht[i].grade,ht[i].rc,ht[i].nr);

getch();

printf(“enter a room charge to print hotels less than given charge \n);

scanf(%d,&c);

printf(hotel name city grade roomcharge no of rooms);

for(i=0;i;i++)

if(c<=ht[i].rc)

printf(“%s %s %c %d %d”,ht[i].name,ht[i].city,h[i].grade,ht[i].rc,ht[i].nr);

}

72. Program which does the below process after reading on odd no of integer. a) a) Print them in given order.

b) b) Replace second elements by product of first and last element c) c) Replace middle value by average of all elements.

d) d) Replace all –ve no’s by zero’s.

main( )

{

int a[10],i,n,sum=0;

clrscr( );

printf(“enter the array sixe );

scanf(%d,&n); printf(“enter the elements”); for(i=0;i;i++)

{

scanf(%d,&a[i]);

sum=sum+a[i];

}

printf(The given arrays is: ); for(i=0;i;i++) printf(“%d,a[i]); a[2]=a[1]*a[n-1];

printf(“\n the given areay after replacing 2nd element is”);

for(i=0;i;i++) printf(“%d,a[i]); a[(1+n/2)]=sum/n;

printf(“\n the given array after replacing middle element by average of all);

for(i=0;i;i++)

if(a[i]<0)

a[i]=0;

printf(“\n given array after replacing ve values by zero”);

for(i=0;i;i++) printf(“%d,a[i]); printf(“\n”); getch();

}

73. Program to sort the entered elements using selection sort technique.

main( )

{

int a[100],i,n,j,t,min,pos;

clrscr();

printf(“enter the array size);

scanf(%d,&n); printf(“enter the elements”); for(i=0;i;i++) scanf(%d,&a[i]); for(i=0;i;i++)

{

min=a[i];

pos=i;

for(j=0;j-1;j++)

if(min>a[j])

{ min=j; pos=j;

} t=a[i]; a[i]=a[pos]; a[pos]=t;

}

printf(the sorted elements are); for(i=0;i;i++) printf(“%2d”,a[i]);

getch( );

}

74. Program to find whether a number is divisible by ‘11’ or not without actual division.

#include<math.h>

main( )

{

int a,b,n,evensum=0,oddsum=0,div;

clrscr( );

printf(“enter a number”);

scanf(%d,&n);

a=n; b=n/10; while(a>0)

{ oddsum=oddsum+(a%10); a=a/10;

}

while(b>0)

{ evensum=evensum+(b%10); b=b/10;

}

div=abs(evensum-oddsum);

if(div%11==0)

printf(The number is divisible by 11);

else

printf(The number is not divisible by 11);

getch();

}

75. Program to find maximum and minimum of entered ’n number using arrays.

main( )

{

int i,n,a[10],min,max;

clrscr( );

printf( enter how many number”);

scanf(%d,&n); printf(“enter the elements”); for(i=0;i;i++) scanf(”%d”,&a[i]); min=a[0];

for(i=0;i;i++) if(min>a[i]) min=a[i];

printf(“minimum=%d”,min);

max=0; for(i=0;i;i++) if(max]); max=a[i];

printf(“\n maximum=%d”,max);

getch( );

}

76. Program to print the following series until there sum exceeds 2.6 term value exceeds 1.5 x+x2/2!+x3/3!+------------.

main( )

{

float x,sum=0,prod=1;

int i;

clrscr( );

printf(“enter x value”);

scanf(%f’,&x); i=1; while((sum<2.6)&&(prod<=1.5))

{

prod=prod*(x/i);

if(prod<=1.5) sum=sum+prod; if(sum>2.6)

{

sum=sum-prod;

break;

} printf(sum=;%f’,sum); i++;

}

getch( );

}

77. Program to print a frequency distribution table for a class of 20-students in the following format. The marks range form 1-25.

Class Intertval fFrequency

1-5 1-5

6-10

6-10

11-15

11-15

16-20

16-20

21-25

21-25

main( )

{

int a[20],i,n1=0,n2=0,n3=0,n4=0,n5=0;

clrscr();

printf(“enter the any 20 no of range(1-25));

for(i=1;i<=20;i++) scanf(%d,&a[i]); for(i=1;i<=20;i++) if((a[i]>=1)&&(a[i]<6)) n1++;

else if((a[i]>5)&&(a[i]<11)) n2++;

else

if((a[i]>10)&&(a[i]<16))

n3++; else if((a[i]>15)&&(a[i]<21)) n4++;

else

if((a[i]>20)&&(a[i]<26))

n5++;

printf(class interval frequency); printf(“\n 1-5 %d”,n1); printf(“\n 6-10 %d”,n2); printf(“\n 11-15 %d”,n3); printf(“\n 16-20 %d”,n4); printf(“\n 21-25 %d”,n5); getch();

}

78. Program to accept values into an array and print array in reverse and original format by using three different functions.

void read_array(int x[]); void print_array(int y[]); void rev_array(int z[]); main()

{

int a[5]; clrscr(); read_array(a); printf_array(a); rev_array(a); getch( );

}

void read_array(int x[])

{

int i;

for(i=0;i<=4;i++)

{

printf(“enter values for a[%d]:,i);

scanf(%d,&x[i]);

}

}

void print_array(int y[])

{

int i; for(i=0;i<=4;i++) printf(“%d”,y[i]);

}

void rev_array(int z[])

{

int i; for(i=4;i>=0;i--) printf(“\n%d,z[i]);

}

79. Program to accept values into single dimensional array and print the array in reverse by using pointers.

main( )

{

int a[5],*b,i; clrscr( ); b=&a[0];

for(i=0;i<=4;i++)

{

printf(“enter a value for a[%d];”.i);

scanf(%d,b);

b++;

} b=&a[4]; for(i=0;i<=4;i++)

{ printf(“\n%d”,*b); b-- ;

}

getch( );

}

80. Program to read a string and print the number of characters in each word of the string.

#includeing.h>

main( )

{

char s[100]; int i,l,nc=0; clrscr( );

printf(“enter the sting”);

gets(s); l=strlen(s); for(i=0;i<l;i++)

{

if(s[i]!=’ )

{ nc=0; while(s[i]!=’ ‘)

{

nc++; printf(“%c”,s[i]); i++;

if(s[i]=’\0’)

break;

}

printf(“\t\t %d”,nc);

printf(“\n”);

}

}

getch();

}

No comments:

Post a Comment

Refer this site 2 ur frndz