87
1. Write a program to verify that each word of a given paragraph is started with uppercase or not. Solution: #include<stdio.h> #include<ctype.h> int main() { char para[20][100]; char ch; int i=0,j=0,status=1; while((ch=getchar())!=EOF) { if(ch!='\n') { if(((para[i][j-1])==32)||(j==0)) { if(isalnum(ch)) { if((isupper(ch))==0) status=0; } } para[i][j++]=ch; } else { para[i++][j]='\0'; j=0; } } (status==1)?printf("\nEach word begins with Uppercase."):printf("\ nEach word doesn't begin with Uppercase."); return 0; } 2. Write a program that can run a *.exe file from the current directory.

Programing Solution

Embed Size (px)

Citation preview

Page 1: Programing Solution

1. Write a program to verify that each word of a given paragraph is started with uppercase or not.

Solution:

#include<stdio.h>#include<ctype.h>

int main(){

char para[20][100];char ch;int i=0,j=0,status=1;

while((ch=getchar())!=EOF){ if(ch!='\n') {

if(((para[i][j-1])==32)||(j==0)) {

if(isalnum(ch)) {

if((isupper(ch))==0) status=0;

}

} para[i][j++]=ch; } else { para[i++][j]='\0'; j=0; }

}

(status==1)?printf("\nEach word begins with Uppercase."):printf("\nEach word doesn't begin with Uppercase.");

return 0;}

2. Write a program that can run a *.exe file from the current directory.

Solution:#include <stdlib.h>#include <stdio.h>#include<process.h>

int main(void){

system("1.exe"); return 0;

Page 2: Programing Solution

}

3. Write a C program to illustrate the use of enumerated data type.

Solution:

#include<stdio.h>

int main(){enum sw{off,on};enum sw bulb1,bulb2;int cx;printf("\nEnter Bulb1's status:\n1.on(1)\n2.off(0)\n:?\b");scanf("%d",&cx);bulb1=cx;

if(bulb1==on)bulb2=off;elsebulb2=on;

if(bulb2==on)printf("\nBulb2 is switched on.");elseprintf("\nBulb2 is switched off.");

return 0;}

4. Define and use a structure as an argument of function as well as return type of a function.

Solution:

#include<stdio.h>struct H_W BMI(struct H_W);struct H_W{ int feet; int in; float weight; float mheight; float bmi;};

struct H_W BMI(struct H_W x){ x.mheight=((x.feet*12.0)+x.in)/39.3700787; x.mheight*= x.mheight; x.bmi = x.weight/ x.mheight; return (x);}

int main(){ struct H_W var; printf("Enter your weight:"); scanf("%f",&var.weight); printf("\nEnter your height(feet inches):");

Page 3: Programing Solution

scanf("%d %d",&var.feet,&var.in); var=BMI(var); printf("\nYour BMI:%.3f",var.bmi);return 0;}

5. Write an user-defined function that accept a string(char array) from the terminal(command prompt) and return its reverse string.

Solution:

#include<stdio.h>#include<string.h>char * strreve();void swapp(char *,char *);

void swapp(char *ch1,char *ch2){ char temp;

temp=*ch1; *ch1=*ch2; *ch2=temp;

}

char * strreve(){

int i=0,l; char *str; printf("\nEnter a string to be reversed:"); scanf("%s",str); l=strlen(str); while(i<l) swapp(&str[i++],&str[--l]);

return str;}

int main(){printf("\nReversed string:%s",strreve());

return 0;}

6. Write a simple encryption program that can replace each alphabet of a given paragraph with its ASCII value separate each character by '-' and space and other non-alphabetic characters remain unchanged.

Solution:

Page 4: Programing Solution

#include<stdio.h>#include<ctype.h>

int main(){

char para[20][100];int msd[20][100];char ch;int i=0,j=0,line=0;

while((ch=getchar())!=EOF){ if(ch!='\n') { para[i][j++]=ch; } else { para[i++][j]='\0'; line++; j=0; }

}

for(i=0;i<line;i++){j=0; while((para[i][j])!='\0') { if(isalpha(para[i][j])) { printf("%d",para[i][j]); if(para[i][j+1]!=32) printf("-"); } else printf("%c",para[i][j]); j++; }printf("\n");}

return 0;}

7. Write an interesting C program that is able to count total number of words, characters, punctuation marks and total number of lines of a given paragraph .

Solution:

#include<stdio.h>

Page 5: Programing Solution

#include<ctype.h>

int main(){

char para[20][100];char ch;int i=0,j=0,line=0,count=0,punc=0,xc=0;

while((ch=getchar())!=EOF){ if(ch!='\n') { xc++; if(ispunct(ch)) punc++; if(((para[i][j-1])==32)||(j==0)) {

if(isalnum(ch)) {

count++; }

} para[i][j++]=ch; } else { para[i++][j]='\0'; line++; j=0; }

}

for(i=0;i<line;i++)printf("\n%s",para[i]);

printf("\nlines=%d\nwords=%d\nNo. of characters:%d\npunctuation maks:%d",line,count,xc,punc);

return 0;}

8. Write a program to calculate the area of a triangle whose vertices's are given.

Solution:

#include<stdio.h>//#include<math.h>

int main(){

float x1,x2,x3,y1,y2,y3,area;

printf("\nEnter A(x1,y1),B(x2,y2) and C(x3,y3):");printf("\nx1:?\b");scanf("%f",&x1);

Page 6: Programing Solution

printf("\ny1:?\b");scanf("%f",&y1);printf("\nx2:?\b");scanf("%f",&x2);printf("\ny2:?\b");scanf("%f",&y2);printf("\nx3:?\b");scanf("%f",&x3);printf("\ny3:?\b");scanf("%f",&y3);

area=0.5*(x1*y2-x2*y1+x2*y3-x3*y2+x3*y1-x1*y3);

if(area<0)area*=-1;

printf("\nArea of triangle ABC:%.2f square unit.",area);return 0;}

9. Write a program to calculate the distance between two points. Where points are given in this format p(x1,y1) ,Q(x2,y2) and then you have to find PQ.

Solution:

#include<stdio.h>#include<math.h>

int main(){float x1,x2,y1,y2,dis;printf("Enter values P(x1,y1) and Q(x2,y2):");printf("\nx1:?\b");scanf("%f",&x1);printf("\ny1:?\b");scanf("%f",&y1);printf("\nx2:?\b");scanf("%f",&x2);printf("\ny2:?\b");scanf("%f",&y2);dis=sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1));printf("\nPQ=%.2f",dis);return 0;}

10. Write a menu driven program having three options (i)AND operation (ii)NOR operation (iii)XOR operation. User will prompt to choose one of these then find and display output on the basis of the input of that user.

Solution:

#include<stdio.h>

int main(){ int x,b1,b2,an; while(x!=4) { printf("\nMenu:\n1.AND\n2.NOR\n3.XOR\n4.Exit"); printf("\nEnter your choice:?\b"); scanf("%d",&x);switch(x)

Page 7: Programing Solution

{ case 1: printf("\nEnter two boolean values:"); scanf("%d %d",&b1,&b2); if(b1<2&&b2<2) { printf("\nanswer:%d",(b1&&b2));

} else printf("Invalid Input!"); break; case 2: printf("\nEnter two boolean values:"); scanf("%d %d",&b1,&b2); if(b1<2&&b2<2) { printf("\nanswer:%d",!(b1||b2));

} else printf("Invalid Input!"); break; case 3: printf("\nEnter two boolean values:"); scanf("%d %d",&b1,&b2); if(b1<2&&b2<2) { printf("\nanswer:%d",b1^b2);

} else printf("Invalid Input!"); break; case 4: break; default: printf("Invalid Input!"); break;} }return 0;}

11. Write a program that can read a paragraph then print it a format so that each word begins with uppercase and ends with uppercase.

Solution:

#include<stdio.h>#include<ctype.h>

int main(){

char para[200];char ch;int i=0,j=0,line=0,count=0,punc=0,xc=0;printf("\nEnter a paragraph:");while((ch=getchar())!=EOF){ para[i++]=ch;

Page 8: Programing Solution

}para[i]=EOF;

i=0;while(para[i]!=EOF){ if(isalpha(para[i])) { if((para[i+1]==32)||(para[i+1]=='\n')||(para[i-1]==32)||(para[i-1]=='\n')) printf("%c",toupper(para[i])); else printf("%c",para[i]); } else printf("%c",para[i]);i++;}

return 0;}

12. Write a C program that can read a set of floating point numbers and also can calculate their sum using only and only pointer.

Solution:

#include<stdio.h>#include<conio.h>

int main(){ float *pt,arr[20],sum=0.0; int i,a,n;

clrscr(); printf("\nEnter how many numbers to be feed:"); scanf("%d",&n); printf("\nEnter %d numbers:",n); for(i=1;i<=n;i++) scanf("%f",&arr[i]); pt=&arr[n]; for(i=1;i<=n;i++) sum+=*pt--;

printf("\nSum=%.2f",sum);getch();return 0;}

13. Write a C program that is designed to accept a floating point number as a string and convert it with its nearest integer and then print.

Solution:

#include<stdio.h>#include<string.h>

Page 9: Programing Solution

#include<ctype.h>#include<math.h>

int main(){ char str[100]; int i=0,j=0,con[100],rcon[100],p,pn,oi; float in=0; printf("Enter a Valid float number:"); scanf("%s",str);

while(str[i]!='\0') { if(isdigit(str[i])) con[j++]=str[i]-'0'; else if(str[i]=='.') { p=i; break; } else { printf("Invalid Input!"); return 0; }

i++; } j=0;pn=str[p+1]-'0';for(i=p-1;i>=0;i--){ rcon[j++]=con[i];

}

for(i=0;i<p;i++)in=in+(rcon[i]*pow(10.0,i));oi=(int) in;if(pn>=5)oi=oi+1;printf("\nNearest integer:%d",oi);return 0;}

14. Write a C program to calculate E of the equation E=mc2. User will give input as a string where values of m and c will be separated by “,”(comma) and input will be terminated by “?”.

Solution:

#include<stdio.h>#include<stdlib.h>

int main(){ double E=0.0,m,c; char xc[20],mm[10],cc[10];

Page 10: Programing Solution

int i=0,j=0;

printf("\nFeed input:");scanf("%s",xc);while(xc[i]!=','){ mm[i]=xc[i]; i++;}mm[i]='\0';i=i+1;while(xc[i]!='?'){ cc[j++]=xc[i]; i++;}cc[j]='\0';m=atof(mm);c=atof(cc);E=m*c*c;printf("\n\nE=%.3lf",E);return 0;}

15. Write a program to read a set of English words from a file named “words.svc” read them all and extra attach 's' with all words if any of them is not terminated with s.

Solution:

#include<stdio.h>#include<ctype.h>#include<string.h>

int main(){

char para[23],out[20][50];int i=0,j=0;FILE *fp,*fo;

fp=fopen("word.txt","r");

while((fscanf(fp,"%s",para))!=EOF){ i=strlen(para)-1;; if(para[i]!='s') strcat(para,"s"); strcpy(out[j++],para);}fclose(fp);fo=fopen("word.txt","w");for(i=0;i<j;i++)fprintf(fo,"%s \n",out[i]);fclose(fo);return 0;}

16. Write a program to verify a given Email ID(g mail) is valid or not . If it's correct then request user to set a password and and store this email ID and password in a file named “Gmail.ice”.

Page 11: Programing Solution

Solution:

#include<stdio.h>#include<string.h>

int main(){ char id[30],pa[15]; FILE *fp;

printf("\nEnter your Email ID:"); scanf("%s",id);

if(strstr(id,"@gmail.com")) { printf("\nEnter passdword:"); scanf("%s",pa);

fp=fopen("Gmail.ice","w"); fprintf(fp,"%s\n%s",id,pa); fclose(fp); printf("\nPassword saved."); } else printf("Not Valid!");

return 0;}

17. Write a program to read an integer array then reverse each integer of this array and store them in another array. Finally print these two arrays.

Solution:

#include<stdio.h>#include<string.h>#include<stdlib.h>

int main(){

int arr[30],rarr[30],i=0,n; char tem[20]; printf("\nEnter how many elements:"); scanf("%d ",&n);

for(i=0;i<n;i++) { scanf("%d ",&arr[i]); sprintf(tem,"%d",arr[i]); strrev(tem); rarr[i]=atoi(tem);

}printf("\narray reverse array"); for(i=0;i<n;i++)printf("\n%3d %3d",arr[i],rarr[i]);return 0;

Page 12: Programing Solution

}

18. Write a program that can show the current date time according to the machine. And If user press any key other than space or return(Enter key) then print “*” n times where n equal to the readings of second of the current time.

Solution:

#include <stdio.h>#include <time.h>#include<string.h>#include<conio.h>

int now_sec(char *);int now_sec(char *sd){int s=0;char rr[5];

rr[0]=sd[17];rr[1]=sd[18];rr[2]='\0';s=atoi(rr);return (s);}

int main(void){ time_t t; char *now,ch; int n,i; clrscr();

do {

t = time(NULL); now= ctime(&t); n=now_sec(now); printf("\nCurrent datetime is %s\n", ctime(&t)); printf("\nPress any key to continue...\n"); ch=getch(); if(ch==32||ch==13) break; else { for(i=0;i<n;i++) printf("*"); } }while((ch!=32)||(ch!=13));

return 0;}

19. Write a C program to calculate the Sum of odd numbers of a given range.

Solution:

#include<stdio.h>#include<string.h>

Page 13: Programing Solution

int main(){ long i=0,lb,ub,sum=0; printf("\nEnter lower bound & Upper bound:"); scanf("%ld %ld",&lb,&ub);

for(i=lb;i<=ub;i++) { if(i%2)

{ sum+=i;}

}printf("\nSum of odd numbers(%d-%d):%d",lb,ub,sum);return 0;}

20. Write a user-defined function that receive a string and print it in lowercase but if there any character what is already in lowercase then replace it with its ASCII value.

Solution:

#include<stdio.h>#include<ctype.h>#include<string.h>

void low(char *);

void low(char *s){ int i=0;

while(s[i]!='\0') { if(isalpha(s[i])) { if(islower(s[i])) { printf("%d",s[i]); } else { tolower(s[i]); printf("%c",s[i]); } } i++; }

}

int main(){

char para[20];

printf("\nEnter a string:");gets(para);

Page 14: Programing Solution

low(para);return 0;}

21. Write a program that accept input as string and replace all the consonants with 1 and all the vowels with 0.Finally print it.

Solution:

#include<stdio.h>#include<string.h>#include<ctype.h>

int iscons(char );int iscons(char c){ switch(c) { case 'a':

case 'e':case 'i':case 'o':case 'u':case 'A':case 'E':case 'I':case 'O':case 'U':return 0;break;default:return 1;break;

}

}

int main(){ char str[100]; int i=0,j=0,k[100]; printf("Enter a string:"); gets(str); printf("\n"); while(str[i]!='\0') { if(isalpha(str[i])) {

printf("%d",iscons(str[i])); }

else printf("%c",str[i]);

i++; }

return 0;}

Page 15: Programing Solution

22. Write a program that request user to feed two integer type number as input and calculate the their sum and count how much carry operations took place there at the time of summing.

Solution:

#include<stdio.h>#include<stdlib.h>#include<string.h>int main(){ int x,y,u=0,carry=0,i,smal,aa,bb,bca=0; unsigned int Ia,Ib; char a[13],b[13],*big,*sma;printf("\nEnter two integers(Enter both 0 to terminate):");do{ scanf("%d %d",&Ia,&Ib); sprintf(a,"%d",Ia); sprintf(b,"%d",Ib); aa=strlen(a); bb=strlen(b); x=atoi(a); y=atoi(b); if(x==0&&y==0) break; if(aa>bb) { smal=bb; big=a; sma=b; } else { smal=aa; big=b; sma=a; } for(i=0;i<=strlen(sma);i++) { x=a[i]-48; y=b[i]-48; if(x+y>9) {carry=carry+1; bca=1;

} else bca=0;

} if((big[i]-48)+bca>9) carry=carry+1; if(carry==0)printf("No carry operation.\n"); else if(carry==1)printf("%d carry operation.\n",carry); elseprintf("%d carry operations.\n",carry);carry=0;}while(x!=0&&y!=0);return 0;}

Page 16: Programing Solution

23. Write a program to read a paragraph and then ask user to press a character as input and finally count how many words contain that character.

Solution:

#include<stdio.h>#include<ctype.h>#include<string.h>#include<conio.h>

int main(){

char para[20][100];int msd[20][100];char ch;int i=0,j=0,word=0,count=0;clrscr();printf("Your paragraph:");while((ch=getchar())!=EOF){ if(ch!='\n'&&ch!=32) { para[i][j++]=ch; } else { para[i++][j]='\0'; word++; j=0; }

}printf("\nGive a chartecter to count:");ch=getchar();for(i=0;i<word;i++){

if(strchr(para[i],ch)) {count++; printf("%s\n",para[i]); }}printf("\nTotal %d words contain '%c'.",count,ch);getch();return 0;}

24. Write a program to add two matrices.

Solution:

#include<stdio.h>

int main(){int i,j,r,c,mat1[10][10],mat2[10][10],ans[10][10];

printf("\nEnter how many rows:");scanf("%d",&r);

Page 17: Programing Solution

printf("\nEnter how many columns:");scanf("%d",&c);printf("\nEnter elements of matrix 1:");for(i=0;i<r;i++) for(j=0;j<c;j++) { scanf("%d",&mat1[i][j]); } printf("\nEnter elements of matrix 1:");for(i=0;i<r;i++) for(j=0;j<c;j++) { scanf("%d",&mat2[i][j]); }

printf("\nYour answer matrix :\n");for(i=0;i<r;i++){ for(j=0;j<c;j++) { ans[i][j]=mat1[i][j]+mat2[i][j]; printf("%d ",ans[i][j]); } printf("\n");}

return 0;}

25. Write a program to subtract one matrix from another.

Solution:

#include<stdio.h>

int main(){int i,j,r,c,mat1[10][10],mat2[10][10],ans[10][10];

printf("\nEnter how many rows:");scanf("%d",&r);printf("\nEnter how many columns:");scanf("%d",&c);printf("\nEnter elements of matrix 1:");for(i=0;i<r;i++) for(j=0;j<c;j++) { scanf("%d",&mat1[i][j]); } printf("\nEnter elements of matrix 1:");for(i=0;i<r;i++) for(j=0;j<c;j++) { scanf("%d",&mat2[i][j]); }

printf("\nYour answer matrix :\n");for(i=0;i<r;i++){ for(j=0;j<c;j++) {

Page 18: Programing Solution

ans[i][j]=mat1[i][j]-mat2[i][j]; printf("%d ",ans[i][j]); } printf("\n");}

return 0;}

26. Write a program that can read an array of string and define a function(user defined) that can determine the string having smallest length.

Solution:

#include<stdio.h>#include<ctype.h>

int len(char *);

int len(char *s){ int c=0,i=0;

while(s[i]!='\0') { i++; }return (i);}

int main(){

char para[20][100];

char ch;int i=0,j=0,line=0,sm=100,li;

while((ch=getchar())!=EOF){ if(ch!='\n') { para[i][j++]=ch; } else { para[i++][j]='\0'; line++; j=0; }

}

for(i=0;i<line;i++){ if(sm>len(para[i])) { sm=len(para[i]); li=i; }}

Page 19: Programing Solution

printf("\nSmallest length(%d) string:%s",sm,para[li]);

return 0;}

27. Write a program to calculate average of a given array of floats using only pointer.

Solution:

#include<stdio.h>#include<conio.h>

int main(){ float *pt,arr[20],sum=0.0,av; int i,a,n;

clrscr(); printf("\nEnter how many numbers to be feed:"); scanf("%d",&n); printf("\nEnter %d numbers:",n); for(i=1;i<=n;i++) scanf("%f",&arr[i]); pt=&arr[n]; for(i=1;i<=n;i++) sum+=*pt--; av=sum/(float) n; printf("\nAvarage=%.2f",av);getch();return 0;}

28. Write a program to illustrate the use of union.

Solution:

#include<stdio.h>union uni{ float h; int age;};struct st{ float h; int age;};

int main(){

union uni u; struct st s;printf("\nSize of Union:%d",sizeof(u));printf("\nSize of Struct:%d",sizeof(s));

return 0;}

Page 20: Programing Solution

29. Write a program that can determine whether a given number is positive or not using macro.

Solution:

#include<stdio.h>#define TEST(i) (i>0)?1:0

int main(){ int num; printf("\nenter a number:"); scanf("%d",&num);

if(TEST(num)==1) printf("\nnumber positive"); else printf("\nnumber not positive");return 0;}

30. Write a program that can convert an array of float into its equivalent integer array.

Solution:

#include<stdio.h>

int rounds(float);int rounds(float f){ int r;

r=(int) f;

if((f-r)>=0.5) return (r+1); else return (r);

}

int main(){ float FA[20]; int IA[20],i=0,j;

printf("\nEnter how many elements :");scanf("%d",&j);printf("\nEnter elements :");for(i=0;i<j;i++){ scanf("%f",&FA[i]); IA[i]=rounds(FA[i]);

}printf("\nEquivalent Integer Array:");for(i=0;i<j;i++)

Page 21: Programing Solution

printf("%d ",IA[i]);return 0;}

31. Write a C program that can determine whether a given number is binary or not.

Solution:

#include<stdio.h>

int main(){ int bin,xc;printf("\nEnter a number:");scanf("%d",&bin); while(bin) { xc=bin%10; if(xc>=2) {

printf("\nNot a Binary number!"); break; } bin/=10; }

if(bin==0) printf("\nBinary number!");

return 0;}

32. Write a program to find the numbers greater than 5 and less than 500 divisible by 2 but not divisible by 4 or 6 and calculate their sum also.

Solution:

#include<stdio.h>#include<string.h>

int main(){

long i=0,lb=6,ub=499,sum=0,l=0;

for(i=lb;i<=ub;i++) { if(i%2==0)

{ if((i%4!=0)&&(i%6!=0)) { printf("%d ",i); sum+=i; } if(l%10==0) printf("\n");

Page 22: Programing Solution

l++;}

}

return 0;}

33. Write a program to read “This is my first program” and print “This Is My First Program” using only getchar(),putchar() and while loop.

Solution:

#include<stdio.h>#include<ctype.h>

int main(){

char para[200],ch;int i=0;

while((ch=getchar())!=EOF){ para[i++]=ch;}

para[i]=EOF;i=0;while(para[i]!=EOF){ if(isalpha(para[i])) { if((para[i-1]==32)||(i==0)) printf("%c",toupper(para[i])); else printf("%c",para[i]); } else printf("%c",para[i]);i++;

}

return 0;}

34. Write a user-defined function named str_adder that accepts two string type arguments and concatenate these two string and display it.

Solution:

#include<stdio.h>#include<conio.h>void str_adder(char a[],char b[]);

void str_adder(char a[],char b[]){char ss[30];

Page 23: Programing Solution

int al,bl,i=0,j=0;

while(a[i]!='\0') { ss[j++]=a[i++]; } i=0; while(b[i]!='\0') { ss[j++]=b[i++]; } ss[j]='\0'; printf("concatented string:%s",ss);}

int main(){ char ch1[10],ch2[10]; clrscr(); printf("\nenter string1:"); scanf("%s",ch1); printf("\nenter string2:"); scanf("%s",ch2); str_adder(ch1,ch2); getch();return 0;}

35. Write a program to calculate average of 10 numbers within a given range.

Solution:

#include<stdio.h>#include<string.h>

int main(){

long i=0,lb,ub,sum=0; float av; printf("\nEnter lower bound & Upper bound:"); scanf("%ld %ld",&lb,&ub); if((ub-lb)==10) {

for(i=lb;i<=ub;i++) {

sum+=i; } av=sum/10; printf("\nAverage:%f",av); } elseprintf("\nGiven range doesn't contain 10 numbers");return 0;}

36. Write a program to calculate y where y=1+2/3+3/5+4/7+........

Page 24: Programing Solution

Solution:

#include<stdio.h>#include<conio.h>

int main(){int i,n;

float sum=0.0,j;clrscr();printf("\nEnter n:");scanf("%d",&n);j=1.0;for(i=1;i<=n;i++) { sum+=(i/(float)j); j+=2.0; }printf("\nSum=%f",sum);getch();return 0;}

37. write a program that can calculate sum of first n prime numbers where n<500;

Solution:

#include<stdio.h>#include<math.h>

int prime(int );

int prime(int p){ int i;

for(i=2;i<=(int) sqrt(p);i++) if(p%i==0) return (0);

printf("%d\t",p);return (1);}int main(){ int i=2,count=0;

while(count<=500){ count+= prime(i++);}return 0;}

38. Using only conditional statement write a program find whether a number is negative positive or zero.

Page 25: Programing Solution

Solution:

#include<stdio.h>

int main(){

int nu;

printf("\nEnter a number:");scanf("%d",&nu);(nu==0)?printf("\nZero"):(nu<0)?printf("\nNegative"):printf("\nPositive");return 0;}

39. Using only pointer write a program to find the largest number from a given set of integer values.

Solution:

#include<stdio.h>#include<conio.h>

int main(){ int *p,arr[20],max; int i,a,n;

clrscr(); printf("\nEnter how many numbers to be feed:"); scanf("%d",&n); printf("\nEnter %d numbers:",n); for(i=1;i<=n;i++) scanf("%d",&arr[i]); p=&arr[n]; max=arr[1]; for(i=1;i<=n;i++) { if(*p>=max) { max=*p; } *p=*p+2;

} printf("\nMaximum=%d",max);getch();return 0;}

40. Using only pointer write a program to calculate the average of total of two integer arrays.

Solution:

#include<stdio.h>#include<conio.h>

int main()

Page 26: Programing Solution

{ int *p,*q,arr[20],arr2[20],sum=0; int i,a,n,m; float av;

clrscr(); printf("\nEnter how many numbers in first array:"); scanf("%d",&n); printf("\nEnter %d numbers:",n); for(i=1;i<=n;i++) scanf("%d",&arr[i]); p=&arr[n]; printf("\nEnter how many numbers in 2nd array:"); scanf("%d",&m); printf("\nEnter %d numbers:",n); for(i=1;i<=m;i++) scanf("%d",&arr2[i]); q=&arr[n];

for(i=1;i<=n;i++) { sum+=*p; *p=*p+2;

} for(i=1;i<=m;i++) { sum+=*q; *q=*q+2;

} av=sum/(float) (m+n); printf("\nMaximum=%f",av);getch();return 0;}

41. Write a program to calculate and print Fibonacci numbers.

Solution:

#include<stdio.h>#include<conio.h>void main(){ clrscr(); long int fibo,a=1,b=0,num=1,n; int i=1; textcolor(GREEN); cprintf("How many :\n"); scanf("%ld",&n); cprintf("\tfirst %ld fibonacci numbers are:\n",n); printf("\t---------------------------------\n"); while(num<=n) { fibo=a+b; printf("%2d-th| fibonacci is |%ld\t\t",i,fibo); a=b; b=fibo; i++; printf("\n\n"); num++; } cprintf("\t----------------------------------\n");

Page 27: Programing Solution

getch(); }

42. Write a program to calculate the standard deviation of the elements of an array. Using two function st_dev and meanf() to calculate standard deviation and mean respectively.

Solution:

#include<stdio.h>#include<conio.h>#include<math.h>#include<dos.h>void meanf();void st_devf();int i,num;float x[30],value,total,mean;

void main(){ clrscr(); meanf(); st_devf();}void meanf(){ textcolor(GREEN); cprintf("how many numbers :"); scanf("%d",&num); /* Reading values into array */ cprintf("Enter %d real numbers:\n",num); for(i=0;i<num;i++) { scanf("%f",&value); x[i]=value; } /* Computation of Total and Mean */ total=0.0; for(i=0;i<num;i++) total=total+x[i]; mean=total/num; /* Printing entered values and mean */ printf("\n"); for(i=0;i<num;i++) printf("x[%2d]=%4.2f\n",i+1,x[i]); textcolor(WHITE); cprintf("\nTotal=%f\n",total); cprintf("Mean=%4.2f",mean); sleep(2); } void st_devf() { float y,std; for(i=0;i<num;i++) {y=0.0; y=y+((x[i]-mean)*(x[i]-mean)); } std=sqrt(y/num);

printf("\nStandard deviation=%f",std); getch();

}

Page 28: Programing Solution

43. Write a program to calculate factorial for n number of integers.

Solution:

#include<stdio.h>#include<conio.h>

long double factorial(int );

void main(){ int n;long double fact; clrscr(); printf("Enter the value of n:?\b"); scanf("%d",&n); fact=factorial(n); printf("factorial of %d is %Lf",n,fact); getch();

}

long double factorial(int n){ long double fact; if(n==1)

return (1); else fact=n*factorial(n-1); return (fact); }

44. Write a program to read the following string “ISLAMIC UNIVERSITY” and print the string in alphabetic order.

Solution:

#include<stdio.h>#include<conio.h>#include<math.h>#include<string.h>#include<ctype.h>#include<stdlib.h>#include<dos.h>

void main(){ int i,j,n;int value[500],k;char string[60]="ISLAMICUNIVERSITY";for(i=0;i<26;i++)value[i]=string[i];

clrscr();

printf("\t\tString in alphabetic order\n\n\n");sleep(1);for(j=0;j<=26;j++)

{

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

Page 29: Programing Solution

{ if(value[i]>=value[i+1]) { k=value[i]; value[i]=value[i+1]; value[i+1]=k; } }

} for(i=0;i<=26;i++) printf("%c",value[i]);

getch();}

45. Write a program to read the fooling string “INFORMATION AND COMMUNICATION ENGINEERING”,from the terminal and display it in following formats:(i)INFORMATION AND COMMUNICATION ENGINEERING (ii)Information and Communication Engineering and (iii)ICE

Solution:

#include<stdio.h>#include<conio.h>#include<string.h>void main(){ char a[80]; clrscr();

printf("Input the string:\n");

gets(a); /*read string*/

puts("\n\n-----------------------------------------------------\n\n");

printf("i)%s",strupr(a));

strlwr(a);

printf("\n\nii)%s",strlwr(a));

strupr(a);

printf("\n\niii)%.1s%.1s%.1s",a,&a[16],&a[30]);

getch();

}46. Write a program to print the 50th triangular number.

Solution:

#include<stdio.h>#include<conio.h>void main()

Page 30: Programing Solution

{ int i,j,n; clrscr(); for(i=1,n=1;n<=50;i++) { for(j=0;j<i;j++) { printf("%3d",n);

n++; } printf("\n\n"); } printf("\n50th triangular number\n"); getch(); }

47. Write a program to print the Floyd's triangle .

Solution:

#include<stdio.h>#include<conio.h>#include<math.h>#include<string.h>#include<ctype.h>#include<stdlib.h>#include<dos.h>

void main(){ int i,j,n,k=1;

clrscr();printf("Tpye how many row do you want:?\b");scanf("%d",&n);

printf("\t\tThe Floyed's triangle:\n\n\n");sleep(1);

for(i=1;i<=n;i++){for(j=1;j<=i;j++)printf("%4d",k++);printf("\n\n");}

getch();}

48. Write a C program that will read the value of x and evaluate the following function:y=1 For x=1,y=0 for x=0 and y=-1 using (i)Else if Statement (ii)conditional operator and (iii)case statement.

Solution:

(i)

#include<stdio.h>#include<conio.h>#include<ctype.h>

Page 31: Programing Solution

void main()

{ int x,y; char ch,che; clrscr(); A: printf("\n\nInput the value of x:?\b");

scanf("%d",&x);

if(x==1) y=1; else if(x==0) y=0; else if(x==-1) y=-1; else { printf("\n\ny is undefined for this value of x\n\nTry again?\n\ny(yes) and n(no)"); ch=getch(); if(toupper(ch)=='Y') goto A; else goto B; }

printf("\nx=%d\n\ny=%d",x,y); printf("\n\npress q or Q for quit..."); che=getch(); if(toupper(che)!='Q') goto A; else goto B;

B: getch();

}

(ii)

#include<stdio.h>#include<conio.h>#include<ctype.h>

void main()

{ int x,y; char ch,che; clrscr(); A: printf("\n\nInput the value of x:?\b");

scanf("%d",&x); switch(x) { case 1: y=1; break; case 0: y=0; break;

Page 32: Programing Solution

case -1: y=-1; break; default: { printf("\n\ny is undefined for this value of x\n\nTry again?\n\ny(yes) and n(no)"); ch=getch(); if(toupper(ch)=='Y') goto A; } }

printf("\nx=%d\n\ny=%d",x,y); printf("\n\npress q or Q for quit..."); che=getche(); if(toupper(che)!='Q') goto A; else goto B;

B: getch();

}

(iii)

#include<stdio.h>#include<conio.h>#include<ctype.h>

void main()

{ int x,y; char ch,che; clrscr(); A: printf("\n\nInput the value of x:?\b");

scanf("%d",&x); y=(x!=0)?((x==1)?1:-1):0; printf("\n\nx=%d\n\ny=%d",x,y); getch(); }

49. Write a C program that will read a line of English text and then print out the corresponding text in Uppercase.

Solution:

#include<stdio.h>

#include<string.h>

void main(){ char line[80];

printf("Enter a line of english text:"); printf("\n\n"); gets(line);

Page 33: Programing Solution

printf("\n\n"); printf("Text in uppercase letter:"); printf("\n\n"); strupr(line); printf("%s",strupr(line)); }

50. Write a C program that can calculate the value sin(x) using a user defined function sinx ranges from 0 to 90 degree steps of 15 degree.

Solution:

#include<stdio.h>#include<conio.h>#include<math.h>

float sinx(int);

int i=0;

float x;

double y;

void main()

{clrscr();printf("The value for various magnitude of x:");printf("\ndegree | sin(x) \n\n");while(i<=360) sinx(i);

getch();}

float sinx(int)

{

x=((i*3.1416)/180);

y=sin(x);

printf("%3d | %10.3lf",i,y);

printf("\n");

i=i+15;

return (y); }

51. Write a program to find the numbers and their sum greater than

Page 34: Programing Solution

100 and less than 200 and divisible by 7.

Solution:

/*Programe to find numbers 100<num<200 and divisible by 7*/

#include<stdio.h>#include<conio.h>#include<math.h>void main(){ int i,sum=0;clrscr(); printf("\nThe integers of greater than 100 and less than 200\nAnd divisible by 7"); for(i=101;i<200;i++) { if(i%7==0) {printf("\n\n\t%3d",i); sum=sum+i; }

} printf("\n\n-------------"); printf("\n\nSum =%7d",sum); getch(); }

52. Write a program a program using conditional operator to determine whether a number is even or odd and print the message “Number is Even” or “Number is Odd”.

Solution:

/*Even-Odd By using Conditional oparator*/

#include<stdio.h>#include<conio.h>

void main(){ long int number;

printf("Enter a number to determine whether it is even or odd\n\n"); scanf("%ld",&number); (number%2==0)?printf("\nNUMBER IS EVEN\n"):printf("\nNUMBER IS ODD\n"); getch(); }

53. Write a program to determine the real and imaginary roots of a quadratic equation.

Solution:

#include<stdio.h>#include<conio.h>#include<math.h>#include<string.h>#include<ctype.h>#include<stdlib.h>

Page 35: Programing Solution

void main(){ float a,b,c,root1,root2,det,D,p,q;

clrscr();

printf("Enter a,b and c\n:-");scanf("%f %f %f",&a,&b,&c);printf("\nGeneralise form of the equation is\n\n:- %2.2fx^2 (+) %2.2fx = %2.2f",a,b,c);

det=b*b-4.0*a*c;

/*Case for imaginary root*/

if(det<0){ printf("\n\nRoots are imaginary\n"); D=4*a*c-b*b; p=-b/2.0; q=sqrt(D)/2.0; printf("\nRoot1= %2.2f + %2.2fi",p,q); printf("\n\nRoot2= %2.2f - %2.2fi",p,q); }

else/* Case for real */

{ printf("\n\nRoots are real\n");root1=(-b+sqrt(det))/(2.0*a);

root2=(-b-sqrt(det))/(2.0*a);printf("\nRoot1= %3.2f\n\nRoot2= %3.2f",root1,root2);}

getch();

}

54. Write a program to compute the sum of digits of a given integer.

Solution:

#include<stdio.h>#include<conio.h>

void main(){long int num; int a,sum=0;clrscr(); printf("Enter an integer:\n"); scanf("%ld",&num);

while(num>0) { a=num%10; num=num/10; sum+=a;

} printf("Sum of the disits in this integer is=%d",sum); getch();

Page 36: Programing Solution

}

55. Write a program to convert a given decimal number to its equivalent binary number.

Solution:

#include<stdio.h>#include<conio.h>void main(){ long int num,con[50],c,i=0; clrscr(); printf("Input number:\n"); scanf("%ld",&num); while(num!=0) {

con[i++]=num%2; num/=2; } printf("Binary equivalent:"); for(c=i-1;c>=0;c--) { printf("%ld",con[c]); } getch(); }

56. Write a program to convert a given decimal number to its equivalent octal number.

Solution:

#include<stdio.h>#include<conio.h>void main(){ static char disits[16]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};int num[50],d,a,i=0;long int conv;clrscr();printf("Enter the decimal number to be converted:\n");scanf("%ld",&conv);

while(conv!=0){num[i++]=conv%8;conv=conv/8;}printf("The octal equivalent converted number=");for(a=i-1;a>=0;a--){d=num[a];printf("%c",disits[d]);}getch();}

57. Write a program to convert a given decimal number to its equivalent number of any given base.

Page 37: Programing Solution

Solution:

#include<stdio.h>#include<conio.h>void main(){ static char disits[16]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};int num[50],d,a,base,i=0;long int conv;clrscr();printf("Enter the decimal number to be converted:\n");scanf("%ld",&conv);printf("on which base you want to convert?\n");scanf("%d",&base);while(conv!=0){ num[i++]=conv%base;conv=conv/base;}printf("The converted number=");for(a=i-1;a>=0;a--){ d=num[a];printf("%c",disits[d]);}getch();}

58. Write a program to convert a given Hexadecimal number to its equivalent decimal number.

Solution:

#include<stdio.h>#include<math.h>int main (){long int n;printf("print an hexadecimal number:");scanf("%x",&n);printf("\n\nEquivalent decimal number is=%d",n);

return 0;}

59. Write a program to convert a given Hexadecimal number to its equivalent binary number.

Solution:

#include<stdio.h>

void main(){int i;char s[30];

printf("HEXADECIMAL TO BINARY\n");printf("Enter A Hexadecimal Number:");scanf("%s",s);

Page 38: Programing Solution

printf("Binary=");for(i=0;s[i]!=NULL;i++){switch(s[i]){case '0':printf("0000");break;case '1':printf("0001");break;case '2':printf("0010");break;case '3':printf("0011");break;case '4':printf("0100");break;case '5':printf("0101");break;case '6':printf("0110");break;case '7':printf("0111");break;case '8':printf("1000");break;case '9':printf("1001");break;case 'a':case 'A':printf("1010");break;case 'b':case 'B':printf("1011");break;case 'c':case 'C':printf("1100");break;case 'd':case 'D':printf("1101");break;case 'e':case 'E':printf("1110");break;case 'f':case 'F':printf("1111");break;default:printf("Number Is Not Hexadecimal");break;}

Page 39: Programing Solution

}

}60. Write a program to convert a given octal number to its equivalent

number of base 5.

Solution:

#include<stdio.h>#include<stdlib.h>int main(){ long int num,n,con[50],c,i=0,d; char xc[20]; printf("Input an octal number:\n"); scanf("%o",&n); sprintf(xc,"%ld",n); num=atol(xc); while(num!=0) {

con[i++]=num%5; num/=5; } printf("\nThe equivalent base 5 number:"); for(c=i-1;c>=0;c--) { printf("%ld",con[c]); } return 0; }

61. Write a program which will take an integer and print a message whether it is leap year or not.

Solution:

#include<stdio.h>void main(){ int year;

printf("Enter year to Test leap year or not :\n");scanf("%d",&year);if((year%100&&year%4==0)||(year%400==0))printf("Leap year");elseprintf("Is Not leap year");

}

62. Write a temperature conversion program,that will convert temperatures among Celsius,Fahrenheit,Rome r and Kelvin scale.

Solution:

//*A temparature conversion programe *//#include<stdio.h>#include<conio.h>#include<dos.h>void main()

Page 40: Programing Solution

{ double fahrenheit,celsius,kelvin; int num,v; clrscr(); printf("1.Fahrenheit to Celsius\n\n2.Celsius to Fahrenheit\n\n3.Celsius to kelvin\n\n4.Kelvin to Celsius\n\n5.Fahrenheit to Kelvin\n\n6.Kelvin to Fahrenheit\n\n7.Celsius to all scale\n\n8.Kelvin to all scale\n\n9.fahrenheit to all scale\n\n"); printf("\nEnter 1,2,3,4,5,6,7,8 or 9 to choose conversion type:\n\n"); sleep(2); scanf("%d",&num); v=num/1; switch(v) { case 1: printf("You have chosen Fahrenheit to Celsius\n\nEnter Fahrenheit temparature\n\n: "); scanf("%lf",&fahrenheit); celsius=double((5/9)*(fahrenheit-32)); printf("Celsius Temparature=%lf\n\n",celsius); break; case 2: printf("You have chosen Celsius to Fahrenheit\n\nEnter Celsius temparature\n\n: "); scanf("%lf",&celsius); fahrenheit=double(((9/5)*celsius)+32); printf("Fahrenheit Temparature=%lf\n\n",fahrenheit); break; case 3: printf("You have chosen Celsius to kelvin \n\nEnter Celsius temparature\n\n: "); scanf("%lf",&celsius); kelvin=double(celsius+273); printf("Kelvin Temparature=%lf\n\n",kelvin); break; case 4: printf("You have chosen Kelvin to celsius\n\nEnter Kelvin Temparature\n\n: "); scanf("%lf",&kelvin); celsius=double(kelvin-273); printf("Celsius Temparature=%lf\n\n",celsius); break; case 5: printf("You have chosen Fahrenheit to kelvin\n\nEnter Fahrenheit temparature\n\n: "); scanf("%lf",&fahrenheit); kelvin=double(((5/9)*(fahrenheit-32))+273); printf("Kelvin Temparature=%lf\n\n",kelvin); break; case 6: printf("You have chosen Kelvin to Fahrenheit\n\nEnter kelvin temparature\n\n"); scanf("%lf",&kelvin); fahrenheit=double((9/5)*(kelvin-273)+32); printf("Fahrenheit Temparature=%lf\n\n",fahrenheit); case 7: printf("You have chosen Celsius to others scale\n\nEnter Celsius temparature\n\n: "); scanf("%lf",&celsius); kelvin=double(celsius+273); printf("Kelvin Temparature=%lf\n\n",kelvin); fahrenheit=double(((9/5)*celsius)+32); printf("Fahrenheit Temparature=%lf\n\n",fahrenheit); break; case 8:

Page 41: Programing Solution

printf("You have chosen Kelvin to others scale\n\nEnter Kelvin temparature\n\n: "); scanf("%lf",&kelvin); celsius=double(kelvin-273); printf("Celsius Temparature=%lf\n\n",celsius); fahrenheit=double((9/5)*(kelvin-273)+32); printf("Fahrenheit Temparature=%lf\n\n",fahrenheit); break; case 9: printf("You have chosen Fahrenheit to others scale\n\nEnter fahrenheit temparature\n\n: "); scanf("%lf",&fahrenheit); kelvin=double(((5/9)*(fahrenheit-32))+273); printf("Kelvin Temparature=%lf\n\n",kelvin); celsius=double((5/9)*(fahrenheit-32)); printf("Celsius Temparature=%lf\n\n",celsius); break; default: sleep(1); printf("\n\nInvalid Input\n\n"); break; } getch(); }

63. Write a program to print digits in reverse order of an integer.

Solution:

#include<stdio.h>

void main(){ long int a,num;

printf("Enter An integer to make reverse\n\n:"); scanf("%ld",&num); printf("Reverse order is :"); while(num>0) { a=num%10; printf("%ld",a); num/=10; }

}

64. Write a program to count the digits of an integer.

Solution:

#include<stdio.h>

void main(){long int num;long int count=0;

printf("Enter an integer:\n"); scanf("%ld",&num);

while(num>0) { num%10;

Page 42: Programing Solution

num=num/10; count++;

} printf("Total disits in this integer=%ld",count);

}

65. Write a program to calculate the product of two matrix.

Solution:

#include<stdio.h>#include<conio.h>#include<math.h>#include<string.h>#include<ctype.h>#include<stdlib.h>#include<dos.h>

void main()

{ int i,j,k,fr,fc,sr,sc,ar,ac,fmat[10][10],smat[10][10],amat[10][10];

clrscr();

printf("Enter how many row in first Matrix:\n\n=>?\b"); scanf("%d",&fr);

printf("\nEnter how many column in first Matrix:\n\n=>?\b"); scanf("%d",&fc);

printf("\nEnter how many row in second Matrix:\n\n=>?\b"); scanf("%d",&sr);

printf("\nEnter how many column in second Matrix:\n\n=>?\b"); scanf("%d",&sc);

printf("\nprothom Matrixer datagulo dao(row by row):");

for(i=0;i<fr;i++) { for(j=0;j<fc;j++) scanf("%d",&fmat[i][j]);

}

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

{for(j=0;j<fc;j++) printf("%3d",fmat[i][j]); printf("\n\n");

}

printf("\nDitio Matrixer datagulo dao(row by row):");

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

{for(j=0;j<sc;j++) scanf("%d",&smat[i][j]);

}

Page 43: Programing Solution

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

{for(j=0;j<sc;j++) printf("%3d",smat[i][j]);

printf("\n\n"); }

if(fc==sr) {printf("\n\nProduct of these two matrix is possible\n\n");

for(i=0;i<fr;i++) {for(j=0;j<sc;j++) {amat[i][j]=0; for(k=0;k<sr;k++) amat[i][j]=amat[i][j]+fmat[i][k]*smat[k][j]; printf("%3d",amat[i][j]); }

printf("\n\n");

}

} else printf("\n\nMultiplication is not possible");}

printf("\ni=%d",i);*/

getch();}

66. Write a program to generate 20x20 multiplication table. The table will be stored in an array. Finally display the array.

Solution:

#include<stdio.h>#define columns 20#define rows 20

void main(){ int i,j,row,column,product[20][20];

printf("\t\tMULTIPLICATION TABLE\n\n");

for(j=1;j<=columns;j++)printf("%4d",j);printf("\n\n");printf("----------------------------------------------------------------------------------\n");for(i=0;i<rows;i++){ row=i+1;

for(j=1;j<=columns;j++) { column=j; product[i][j]=row*column; printf("%4d",product[i][j]);

Page 44: Programing Solution

} printf("\n");

}

}

67. Write a program to calculate “2 to the power n” and “2 to the power -n”.The results will be stored in different arrays and finally display the results as table.

Solution:

#include<stdio.h>#include<conio.h>#include<math.h>void main(){ double pos[50],neg[50]; int n,i; clrscr(); printf("Enter the value of n:?\b"); scanf("%d",&n); for(i=0;i<=n;i++) pos[i]=pow(2,i); for(i=1;i<=n;i++) neg[i]=pow(2,-i); printf("\t2 to power \t\t 2 to power -n\n"); for(i=0;i<=n;i++) printf("\t %lf \t\t %lf\n\n",pos[i],neg[i]); getch(); }

68. Write a program to read from keyboard,print into screen also stored in a file(named top ten) the information of 10 cricketers. Using a structure called cricket that will describe the following information:player’s name,team-name,number of Half centuries and batting average.

Solution:

#include<stdio.h>#include<conio.h>void main(){ int v,marks,i,m; clrscr(); printf("\nEnter your Obtained Marks\n:"); scanf("%d",&marks); v=marks/5; switch(v) { case 20: case 19: case 18: case 17: case 16: printf("Grade=A+\nMarks>79;"); break; case 15: printf("Grade=A\n74<Marks<80"); break;

Page 45: Programing Solution

case 14: printf("Grade=A-\n69<Marks<75;"); break; case 13: printf("Grade=B+\n64<Marks<70;"); break; case 12: printf("Grade=B\n59>Marks<65"); break; case 11: case 10: case 9: printf("Grade=C\n44<Marks<60"); break; default: printf("Grade=F\nmarks<45"); break; } getch();

}

69. Define a structure that can describe a hotel. It should have numbers that include the name,address grade,average room charge,and number of rooms. Write a function to perform the following operations:

(i)To print out hotels of a given grade in order of charges.(ii)To print out hotels with room charges less than a given value.

Solution:

#include<stdio.h>#include<ctype.h>#define MAX 5

struct hotel{ char name[25]; char grd; float avg_rc; char add[100];}H[MAX];

void hotel_grd(char );void hotel_ch(float );

void hotel_ch(float c){int i; printf("\nRecord of hotels of room charge less than %f.",c); for(i=0;i<MAX;i++) { if(H[i].avg_rc<c) { printf("\nRecord no:%d",i+1); printf("\nName of the hotel:%s",H[i].name); printf("\nHotel's Grade:%c",H[i].grd); printf("\nAverage room charge:%f",H[i].avg_rc); printf("\nAddress:%s",H[i].add);

Page 46: Programing Solution

}

}}

void hotel_grd(char ch){ int i; printf("\nRecord of hotels of grade %c.",ch); for(i=0;i<MAX;i++) { if(H[i].grd==ch) { printf("\nRecord no:%d",i+1); printf("\nName of the hotel:%s",H[i].name); printf("\nHotel's Grade:%c",H[i].grd); printf("\nAverage room charge:%f",H[i].avg_rc); printf("\nAddress:%s",H[i].add); }

}

}

int main(){char c; int i,op; float ch;

printf("\nEnter record of %d hotels.",MAX); for(i=0;i<MAX;i++) { printf("\nRecord no:%d",i+1); printf("\nEnter Name of the hotel:"); scanf("%s",H[i].name); printf("\nEnter Average room charge:"); scanf("%f",&H[i].avg_rc); printf("\nEnter Address:"); scanf("%s",H[i].add); printf("\nEnter Grade(A,B,C so on.):"); H[i].grd=getchar(); }

printf("\n:::::::::::::::::::::::::::::::::::::::::::::::::::::\n"); printf("\nYou may do:\n1.Search hotels with room charge less than a given value."); printf("\n2.Search hotels of a given grade.\n"); printf("Enter your choice: "); scanf("%d",&op);

if(op==1){ printf("\nEnter desired grade:"); scanf("%c",&c); hotel_grd(c);}else if(op==2){ printf("\nEnter desired grade:");

Page 47: Programing Solution

scanf("%f",&ch); hotel_ch(ch);}elseprintf("\ninvalid input.");

return 0;}

70. Write a program that will add two tables of number.

Solution:

#include<stdio.h>#include<conio.h>#include<ctype.h>

void main(){ label: double t1[50][50],t2[50][50],sum[50][50]; int i,j,col,row; char ch; clrscr(); printf("Enter how many rows and columns in each table:\n"); scanf("%d %d",&row,&col); printf("Input first table's data row by row:\n?\b"); for(i=0;i<row;i++) { for(j=0;j<col;j++) scanf("%lf",&t1[i][j]);

} clrscr(); printf("\nInput second table's data row by row:\n?\b"); for(i=0;i<row;i++) { for(j=0;j<col;j++) scanf("%lf",&t2[i][j]);

} clrscr(); printf("-------------------------------------------------------\n"); printf("\t\t\tTable one \n"); printf("-------------------------------------------------------\n"); textcolor(GREEN); for(i=0;i<row;i++) { for(j=0;j<col;j++) cprintf("|%7.2lf",t1[i][j]); printf("\n\n"); } printf("-------------------------------------------------------\n"); printf("\t\t\tTable two \n"); printf("-------------------------------------------------------\n"); for(i=0;i<row;i++) { for(j=0;j<col;j++) cprintf("|%7.2lf",t2[i][j]); printf("\n\n"); } printf("--------------------------------------------------------\n"); printf("\t\tSum of two tables\n"); printf("-------------------------------------------------------\n"); textcolor(RED); for(i=0;i<row;i++)

Page 48: Programing Solution

{ for(j=0;j<col;j++) { sum[i][j]=t1[i][j]+t2[i][j]; cprintf("|%7.2lf",sum[i][j]); } printf("\n\n"); } printf("---------------------------------------------------------\n"); textcolor(BLUE+BLINK); cprintf("Enter q or Q to quit...."); textcolor(WHITE); ch=getche(); if(toupper(ch)!='Q') goto label;getch();}

71. Write a program to sort an integer array.

Solution:

#include<stdio.h>#include<conio.h>void sort(int a[],int n);void main(){int i,n,a[50];clrscr();printf("How many elements in the array?");scanf("%d",&n);printf("Enter %d elements in aray\n ",n);for(i=0;i<n;i++)scanf("%d",&a[i]);sort(a,n);}

void sort(int a[],int n)

{int i,j,tem;

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

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

if(a[j-1]>=a[j])

{

tem=a[j-1];

a[j-1]=a[j];

a[j]=tem;

}

printf("After acesding number\n");

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

printf("%3d",a[i]);

Page 49: Programing Solution

getch();

}

72. Using structure type variable ,write a program to input the records of 100 students and display their record will contain student's name,address and registration number.

Solution:

#include<stdio.h>#include<conio.h>#define MAX 100struct student{ char name[20]; char add[100]; int reg;};

int main(){ struct student S[MAX]; int i; clrscr(); printf("\nEnter record of %d students.",MAX); for(i=0;i<MAX;i++) { printf("\nRecord no:%d",i+1); printf("\nEnter Name:"); scanf("%s",S[i].name); printf("\nEnter Reg No:"); scanf("%d",&S[i].reg); printf("\nEnter Address:"); scanf("%s",S[i].add); } clrscr(); printf("\n::::::::::::::::::::::::::::::::::::::::::::::::::::\n"); for(i=0;i<MAX;i++) { printf("\nRecord no:%d",i+1); printf("\nName:%s",S[i].name); printf("\nReg No:%d",S[i].reg); printf("\nAddress:%s",S[i].add); } getch();return 0;}

73. Given a set of numbers find the greatest common divisor using function.

Solution:

#include<stdio.h>#include<stdlib.h>int gcd(int ,int);

int gcd(int a,int b){ int temp;

Page 50: Programing Solution

while(b!=0){ temp=(a%b); a=b; b=temp;}

return a;}

int main(){ int p[10],i,n,temp,g;

printf("\nHow many numbers:");scanf("%d",&n);for(i=0;i<n;i++)scanf("%d",&p[i]);i=0;temp=p[0];while(i<n){ g=gcd(temp,p[i+1]); temp=g; i++;}

printf("\n\nGcd=%d",g);return 0;}

74. Write a program to generate the first n Fibonacci numbers.

Solution:

#include<stdio.h>#include<conio.h>void main(){ clrscr(); long int fibo,F1=1,b=0,num=1,n; int i=1; textcolor(GREEN); cprintf("How many many fibonacci number do you want to generate:?\b"); scanf("%ld",&n); cprintf("first %ld fibonacci numbers are:",n); printf("\n\t---------------------------------\n"); while(num<=n) { fibo=F1+b; printf("%2d-th| fibonacci is |%ld\t\t",i,fibo); F1=b; b=fibo; i++; printf("\n\n"); num++; } printf("\t----------------------------------\n"); getch(); }

Page 51: Programing Solution

75. Using for loop write a program to display the following triangle :12 34 5 67 8 9 1011 12 13 14 15

Solution:

#include<stdio.h>

int main(){ int i,j,a;

for(i=1,a=1;i<=5;i++) { for(j=0;j<i;j++) {

printf("%-4d",a); a++;

}

printf("\n\n\n");}

return 0;}

76. Write a program to display the following triangle : 1

2 3 4 5 6

7 8 9 10 11 12 13 14 15

Solution:

#include<stdio.h>#include<conio.h>

int main(){ int i,j,c,n,k;

clrscr(); printf("\nEnter how many rows:"); scanf("%d",&n); k=1; for(i=1;i<=n;i++) { for(j=0;j<(n-i);j++) printf(" "); for(c=1;c<=i;c++) printf("%-4d",k++); printf("\n\n");

}getch();

Page 52: Programing Solution

return 0;}

77. write a program to display the following triangle : 1 0 1 1 0 1 0 1 0 1 1 0 1 0 1

Solution:

#include<stdio.h>#include<conio.h>#define row 5#define col 9

int main(){ int i,j,k,l=1; clrscr();for(i=1;i<=row;i++){ if(i%2==0) l=0; else l=1;for(j=0;j<row-i;j++)printf(" ");for(k=1;k<=i;k++){ if(l>0) {printf(" 1 "); l=0; } else {printf(" 0 "); l=1; }

}printf("\n\n");}getch();return 0;}

78. write a program to display the following triangle :15 14 13 12 1110 9 8 76 5 43 21

Solution:

#include<stdio.h>#include<conio.h>#define maxrow 5

void gtab(int u)

Page 53: Programing Solution

{ int i=0;for(i=0;i<u;i++)printf(" ");}int main(){ int r,c,m,num=15,sp=3; clrscr();for(r=maxrow;r>=1;r--){

for(m=0;m<=maxrow-r;m++)printf(" ");for(c=r;c>=1;c--){printf("%-4d",num--);}printf("\n\n");gtab(sp);sp=sp+3;}getch();return 0;}

79. Using for loop write a program to display the following triangle :12 33 4 54 5 6 75 6 7 8 9

Solution: #include<stdio.h>

int main()

{ int i,j,d=0,n;printf("How many line you want:?\b\n\n");

scanf("%d",&n);

printf("\t\tThe triangle with %d row:\n\n",n);

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

{ for(j=i;j<=(i+d);j++)

{ printf("%4d",j);

} d++; printf("\n\n");

}

return 0;}

Page 54: Programing Solution

80. Write a program to display the Pascal's Triangle.

Solution:

#include<stdio.h>main(){int b,p,q,r,x;printf("Enter the no: of rows : ");scanf("%d",&r);printf("\n pascals triangle :\n\n");b=1;q=0;while(q<r){for(p=30-3*q;p>0;p--)printf(" ");for(x=0;x<=q;x++){if(x==0||q==0)b=1;elseb=(b*(q-x+1)/x);printf("%6d",b);}printf("\n");q++;}}

81. Write a program to display the binomial Coefficient Table.

Solution:

#include<stdio.h>#include<conio.h>#define max 10

int main(){ int n,r,binom; clrscr(); printf("\n\t\t\tBinomial Coefficient table\n\n"); printf("n\\r");for(n=0;n<=10;++n)printf(" %4d",n);printf("\n___________________________________________________________\n");n=0;

do{printf(" %2d",n);r=0;binom=1;while(r<=n) {if(n==0||r==0)printf(" %4d",binom);else

Page 55: Programing Solution

{binom=binom*(n-r+1)/r;printf(" %4d",binom);}r++; }printf("\n\n");n+=1;}while(n<=max);

getch();return 0;}

82. Write a program to evaluate the following investment equation V=P(1+r)^n and prints the tables which would give the value of V for various combination of the values of P,r and n.

P:1000,2000,3000..................,10000r: 0.10,0.11,0.12,.....................,0.20n:1,2,3......................................10

solution:

#include<stdio.h>#include<conio.h>#include<math.h>void main(){ long int P=1000; int n=1,i; float r=0.10,x; double V ; clrscr(); printf("\t\tTable for V=P(1+r)^n investment Equation\n\n"); printf("\t\tP|\tr|\t n|\t V\n"); printf("\t\t------------------------------------\n"); for(P=1000,r=0.10,n=1;P<=10000;P=P+1000,r=r+0.01,n++) {V=P*pow((1+r),n); printf("\t\t%ld|\t%0.2f|\t%2d|\t%10.2lf|\n",P,r,n,V); printf("\t\t------------------------------------\n"); }

getch(); }

83. Write a program to evaluate equation distance=ut+(at^2)/2 by varying time.

Solution:

#include<stdio.h>#include<conio.h>#include<math.h>#define MAX 20#define u 10.5#define a 12.3

void main()

{ float distance; int t;

Page 56: Programing Solution

printf("\t Time\t\tDistance\n\n"); for(t=0;t<=MAX;t++) { distance=(u*t)+((1/2)*a*pow(t,2)); printf("\t %2d\t\t%3.2f\n\n",t,distance); }

getch(); }

84. Write a program to illustrate “Continue” statement.

Solution:

#include<stdio.h>#include<conio.h>int main(){ int num[10],i,sum; clrscr(); printf("Enter 5 values:"); for(i=0;i<5;i++) scanf("%d",&num[i]); clrscr(); for(i=0;i<5;i++) printf("%3d",num[i]); sum=0; for(i=0;i<5;i++){ if(num[i]<0) continue; sum=sum+num[i];

} printf("\n\nSum of given positive numbers is %d",sum); getche(); return 1; }

85. Write a password program,where the program will ask for

password . The program then read a file named “user.pwd”(manually created) and will compare the password with the file content. Three attempts are allowed,then the program will exit.

Solution:

#include<stdio.h>#include<conio.h>#include<string.h>#include<ctype.h>void main(){ FILE *fp; char ch[10],c[200],str[10],d; int i=0,j=0; printf("Enter a file name(user.pwd):\n"); scanf("%s",ch); fp=fopen(ch,"w"); printf("Give a passward...\n...=>"); { fscanf(stdin,"%s",c); fprintf(fp,"%s",c);

Page 57: Programing Solution

} fclose(fp); printf("\nEnter passward:"); scanf("%s",str); fp=fopen(ch,"r"); while((d=getc(fp))!=EOF) strchr(str,d)?i++:j++; fclose(fp); if(i==strlen(str)) printf("\nPassward mach"); else printf("\nPassward mismach"); getch(); }

86. Write a menu driven program to illustrate the following four types of functions. a. Functions with no arguments and return values. b. Functions with arguments but no return values. c. Functions with arguments and return values.

d. Functions with no argument but return values.

Solution:

#include<stdio.h>#include<conio.h>#include<math.h>#include<string.h>#include<ctype.h>#include<stdlib.h>#include<dos.h>

void nargnret(void);void argnret(int,int);int argret(int,int);float nargret(void);

void main()

{ char ch; int x,y,z; float w; clrscr(); printf("\nSelect one category from the menu shown below:"); printf("\n\na.Function with no arguments and no return value\n\nb.Function with arguments but no return values\n\nc.Function with arguments and return values\n\nd.Function with no arguments but return values\n\n"); printf("You have selected:"); ch=getch(); putchar(ch); printf("\n\nPlease Wait...."); sleep(2); clrscr(); switch(ch) { case 'a': printf("\n\n***Illustration of Function with no arguments and no return value***\n\n"); nargnret(); printf("\n\nAlready Come back to main... "); break; case 'b':

Page 58: Programing Solution

printf("\n\n***Illustration of Function with arguments but no return values***\n\n"); printf("\nEnter x and y as arguments of called function:"); scanf("%d %d",&x,&y); argnret(x,y); printf("\n\nAlready Come back to main... "); break; case 'c': printf("\n\n***Illustration of Function with arguments and return values***"); printf("\n\nEnter x and y as arguments of called function:"); scanf("%d %d",&x,&y); z=argret(x,y); printf("\n\nAlready Come back to main... "); printf("\n\nCalled function returned the multiplication result of given arguments is %d",z); break; case 'd': printf("\n\n***Illustration of Function with no arguments but return values***"); w=nargret(); printf("\n\nAlready Come back to main... "); printf("\n\nCalled function returns a float: %2.3f",w); break;

default: printf("\n\nyour choice is not in the menu list"); break; }

getch();}

void nargnret()

{ printf("\n\nCalled fanction has been started from here....."); printf("\n\nIts a Example of function with no arguments and no return value;"); printf("\n\nCalled function ends here");

}void argnret(int x,int y)

{ int z; z=x+y; printf("\n\nCalled fanction has been started from here....."); printf("\n\nIts a Example of function with arguments but no return values;"); printf("\n\nsum of x and y is %d",z); printf("\n\nCalled fanction ends here.");

}

int argret(int x,int y)

{ int z; z=x+y; printf("\n\nCalled fanction has been started from here....."); printf("\n\nIts a Example of function with arguments and return values;"); printf("\n\nsum of x and y is %d",z); printf("\n\nCalled fanction ends here..press enter..."); getch(); return (x*y); }

Page 59: Programing Solution

float nargret()

{ float me=100.50; printf("\n\nCalled fanction starts from here"); printf("\n\nIts a Example of function with no arguments but return value;"); printf("\n\nCalled function ends here..press enter..."); getch(); return (me);}

87. Write a function “exchange” to interchange the values of two variables x,y. Illustrating the use of this function in a calling function.

Solution:

#include<stdio.h>#include<conio.h>void exchange(int*,int*);void main(){ int x,y; printf("Enter two numbers to be exchanged:?\b"); scanf("%d %d",&x,&y); printf("\n\nBefore Exchange:\n\n x=%d and\n\n y=%d",x,y); exchange(&x,&y); printf("\n\nAfter Exchange:\n\n x=%d and\n\n y=%d",x,y); getch(); } void exchange(int *a,int *b) { int t;

t= *a; *a=*b; *b=t;

}

88. using switch statement write a program to determine the grade of a student where the grades are designed as follows :

Marks>79:Grade=A+ 74<Marks<80:Grade=A 69<Marks<75:Grade=A-

64<Marks<70:Grade=B+ 59<Marks<65:Grade=B 44<Marks<60:Grade=C Marks<40:Grade=F

Solution:

#include<stdio.h>#include<conio.h>void main(){ int v,marks,i,m; clrscr(); printf("\nEnter your Obtained Marks\n:");

Page 60: Programing Solution

scanf("%d",&marks); v=marks/5; switch(v) { case 20: case 19: case 18: case 17: case 16: printf("Grade=A+\nMarks>79;"); break; case 15: printf("Grade=A\n74<Marks<80"); break; case 14: printf("Grade=A-\n69<Marks<75;"); break; case 13: printf("Grade=B+\n64<Marks<70;"); break; case 12: printf("Grade=B\n59>Marks<65"); break; case 11: case 10: case 9: printf("Grade=C\n44<Marks<60"); break; default: printf("Grade=F\nmarks<45"); break; } getch();

}

89. Write a function space(x) that can be used to provide a space of x positions between two output numbers.or, Use recursive calls to evaluate the function f(x)=x-x^3/3!+x^5/5!-x^7/7!

Solution:

#include<stdio.h>#include<conio.h>void space();void main()

void space(){ int a=123; int i=1,x; int b=890; printf("How many space do you want to provide\n\n:"); scanf("%d",&x); printf("%d",a); while(i<=x) { printf(" "); i++; } printf("%d",b);getch();

Page 61: Programing Solution

}

90. Write a function using pointers to add two matrices and return the resultant matrix to the calling function.

Solution:

#include<stdio.h> #include<conio.h>  int  **sum_mat(int  x1[][5],int x2[][5],int x,int y);  int **sum_mat(int  x1[][5],int x2[][5],int x,int y) {  int **s,i,j;    for(i=0;i<x;i++)   for(j=0;j<y;j++)    s[i][j]=x1[i][j]+x2[i][j];   return (s); }   int main() {  int i,j,x,y;  int a[5][5],b[5][5],**get;;  clrscr();   printf("\nEnter no of row:");  scanf("%d",&x);  printf("\nEnter no of col:");  scanf("%d",&y);    printf("\nEnter elements of mat1:");  for(i=0;i<x;i++)   for(j=0;j<y;j++)    scanf("%d",&a[i][j]);     printf("\nEnter elements of mat2:");    for(i=0;i<x;i++)   for(j=0;j<y;j++)    scanf("%d",&b[i][j]);   get=sum_mat(a,b,x,y);    printf("\nReceived matrix(mat1+mat2):\n\n");  for(i=0;i<x;i++)   {   for(j=0;j<y;j++)    printf("%4d",get[i][j]);    printf("\n\n");    } getch(); return 0; }

91. Write a program illustrating the use of pointer with two dimensional arrays.

Solution:

#include <stdio.h>#define NUM_STUDENTS1 25

Page 62: Programing Solution

#define NUM_TESTS1 10#define NUM_STUDENTS2 15#define NUM_TESTS2 8

int get_highest(int *a, int cols, int row, int col);

int main(){ int grades1[NUM_STUDENTS1][NUM_TESTS1] = { {55, 60, 65}, {85, 90, 95} }; int num_students1 = 2; int num_tests1 = 3; int high_test1;

int grades2[NUM_STUDENTS2][NUM_TESTS2] = { {55, 60}, {85, 90}, {75, 70} }; int num_students2 = 3; int num_tests2 = 2; int high_test2; high_test1 = get_highest( (int *)grades1, NUM_TESTS1, num_students1, num_tests1); printf("The highest score in the first class is %d.\n", high_test1);

high_test2 = get_highest( (int *)grades2, NUM_TESTS2, num_students2, num_tests2); printf("The highest score in the second class is %d.\n", high_test2);

return 0;}

int get_highest(int *a, int cols, int row, int col)

{ int i, j; int highest = *a;

for( i = 0; i < row; i++) for( j = 0; j < col; j++) if ( *(a + i*cols + j) > highest) highest = *(a + i*cols + j); return highest;}

92. Write a program illustrating the use of pointer with functions.

Solution:

#include<stdio.h>#include<conio.h>#include<string.h>

int sum(int*,int*);

void main(){ int *num[40],s,n,i;

clrscr();printf("Enter how many numbers do you want to Add:?\b");

Page 63: Programing Solution

scanf("%d",&n);printf("\n\nEnter %d numbers:",n);for(i=0;i<n;i++)scanf("%d",&num[i]);s=sum(&n,&num);printf("\t\tsum=%d\n\n\n",s);

getch();

}

int sum(int*n,int *num)

{ int s=0,i;

for(i=0;i<*n;i++)s=s+num[i];return(s);}

93. Write a program to read a text file “Data.txt” containing integers,then the program will search for even and odd numbers from the integers. Finally the program will write even and odd numbers in two different files “Even.txt” and “Odd.txt” respectively.

Solution:

#include<stdio.h>#include<conio.h>

void main(){ FILE *f1,*f2,*f3; int num,i; clrscr(); printf("Give input for the data file\npress -1 to stop feeding:\n"); f1=fopen("DATA.txt","w"); while(num!=-1) { scanf("%d",&num); putw(num,f1); } fclose(f1); f1=fopen("DATA.txt","r"); f2=fopen("ODD.txt","w"); f3=fopen("EVEN.txt","w"); while((num=getw(f1))!=EOF) { if(num%2==0) putw(num,f3); else putw(num,f2); } fclose(f1); fclose(f2); fclose(f3);

f2=fopen("ODD.txt","r"); f3=fopen("EVEN.txt","r");

printf("\n\nThe file ODD.txt contains:\n"); while((num=getw(f2))!=EOF)

Page 64: Programing Solution

printf("%4d",num);

printf("\n\nThe file EVEN.txt contains:\n"); while((num=getw(f3))!=EOF) printf("%4d",num); fclose(f2); fclose(f3);

getch(); }

94. Two files “Data1” and “Data2” contain sorted lists of integers. write a program to produce a file “Data” which holds a single sorted,merged list of these two lists. Use command line arguments to specify the file names.

Solution:

#include<stdio.h>#include<conio.h>

void main(int argc,char *argv[]){ FILE *f1,*f2,*f3; char *fn1,*fn2,*fn3; int num,i=0,final[100],x; clrscr();

fn1=argv[1]; fn2=argv[2]; fn3=argv[3];

f1=fopen(fn1,"r"); f2=fopen(fn2,"r"); f3=fopen(fn3,"w");

while((num=getw(f1))!=EOF) //file 1 theke nibo final[i++]=num; while((num=getw(f2))!=EOF) //file 2 theke nibo final[i++]=num; x=i;

for(i=0;i<x;i++) putw(final[i],f3); fclose(f1); fclose(f2); fclose(f3); printf("\nYour file is created!"); getch(); }

95. Write an age calculation program,which will take birth date as its command line argument,and then it calculates age with respect to current date.

Solution:

Page 65: Programing Solution

#include<stdio.h>#include<conio.h>#include<math.h>#include<dos.h>#include<ctype.h>#include<time.h>#include<stdlib.h>#include<string.h>

int leap(int y );void main(){ int i=0,cum,cuy,cud,Yb,Mb,Db,Ya,Ma,Da,m; char mon[5],yer[6],da[5]; char d[35]; struct tm *ptr;clrscr(); time_t lt;

lt=time(NULL); ptr=localtime(&lt);

strcpy(d,asctime(ptr)); for(i=4;i<=6;i++) mon[i-4]=d[i];

mon[i-4]='\0'; if(strcmp(mon,"Jan")==0)

cum=1; else if(strcmp(mon,"Feb")==0)

cum=2; else if(strcmp(mon,"Mar")==0)

cum=3; else if(strcmp(mon,"Apr")==0)

cum=4; else if(strcmp(mon,"May")==0)

cum=5; else if(strcmp(mon,"Jun")==0)

cum=6; else if(strcmp(mon,"Jul")==0)

cum=7; else if(strcmp(mon,"Aug")==0)

cum=8; else if(strcmp(mon,"Sep")==0)

cum=9; else if(strcmp(mon,"Oct")==0)

cum=10; else if(strcmp(mon,"Nov")==0)

cum=11; else

cum=12;

for(i=20;i<=24;i++) yer[i-20]=d[i]; cuy=atoi(yer); for(i=8;i<=9;i++) da[i-8]=d[i]; da[i-8]='\0'; cud=atoi(da);

textcolor(YELLOW); cprintf(":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::"); printf("\n\n");

Page 66: Programing Solution

textcolor(RED); cprintf("++++++++++++++++++++DATA INPUT SECTION+++++++++++++++++++"); printf("\n\n"); textcolor(BLUE); cprintf("Enter birth(year-month-day)separate them by single space :"); scanf("%d %d %d",&Yb,&Mb,&Db); printf("\n\n"); cprintf("Your Birthday is:%d-%d-%d",Db,Mb,Yb); printf("\n\n"); cprintf("OK..."); sleep(1); printf("\n\n"); textcolor(YELLOW); cprintf("Current Date is "); cprintf(asctime(ptr));

printf("\n\n");

switch(Mb) { case 1: m=31; break; case 2: m=leap(Yb)+28; break; case 3: m=31; break; case 4: m=30; break; case 5: m=31; break; case 6: m=30; break; case 7: m=31; break; case 8: m=31; break; case 9: m=30; break; case 10: m=31; break; case 11: m=30; break; case 12: m=31; break; }/*Calculation Of Day */if(cud>=Db) { Da=(cud-Db)+1; /*Extra 1 is birthday*/

Page 67: Programing Solution

Mb=Mb; }else { Da=((cud+m)-Db+1); Mb=Mb+1; }/*Calculation Of Month */if(cum>=Mb) { Ma=(cum-Mb); Yb=Yb; }else { Ma=(cum+12)-Mb; Yb=Yb+1; } /*Calculation Of Year */ Ya=(cuy-Yb); textcolor(YELLOW); cprintf("::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::"); textcolor(RED); printf("\n\n"); cprintf("++++++++++++++++++++OUTPUT SECTION+++++++++++++++++"); printf("\n\n"); textcolor(WHITE); cprintf("Today Your Age is:%d years %d month %d days",Ya,Ma,Da); sleep(6); printf("\n\n"); textcolor(YELLOW); cprintf("::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::"); textcolor(RED); printf("\n\n"); cprintf("++++++++++++PROGRAMMER'S INFORMATION SECTION++++++++++++++++"); printf("\n\n"); textcolor(RED); cprintf("This Program's Source code is written by....."); sleep(3); printf("\n\n"); textcolor(RED+128); cprintf("MD: ANISUZZAMAN"); printf("\n\n"); sleep(3); textcolor(YELLOW); cprintf("DEPARTMENT OF ICE,ISLAMIC UNIVERSITY"); printf("\n\n"); if(m==29) cprintf("Yahoo...Your Birth Year is a Leap Year"); printf("\n\n"); textcolor(RED+BLINK); cprintf("**************************** BYE******************************"); printf("\n\n"); textcolor(YELLOW); cprintf("::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::");

getch();}/*leap year ber kora*/int leap(int year){ int y,a; y=year;if((y%100&&y%4)==0||y%400==0)a=1;

Page 68: Programing Solution

elsea=0;return(a);}

96. Write a command line age calculation program,which will take birth date as its command line argument,and then it calculates age with respect to current date and displays the current date.

Solution:

#include<stdio.h>#include<conio.h>#include<math.h>#include<dos.h>#include<ctype.h>#include<time.h>#include<stdlib.h>#include<string.h>

int leap(int y );void main(int argc, char *argv[]){ int i=0,cum,cuy,cud,Yb,Mb,Db,Ya,Ma,Da,m; char mon[5],yer[6],da[5]; char d[35]; struct tm *ptr; if(argc<4) {

printf("You have to enter year, month, and day atleast.");return;

}clrscr(); time_t lt;

lt=time(NULL); ptr=localtime(&lt);

strcpy(d,asctime(ptr)); for(i=4;i<=6;i++) mon[i-4]=d[i];

mon[i-4]='\0'; if(strcmp(mon,"Jan")==0)

cum=1; else if(strcmp(mon,"Feb")==0)

cum=2; else if(strcmp(mon,"Mar")==0)

cum=3; else if(strcmp(mon,"Apr")==0)

cum=4; else if(strcmp(mon,"May")==0)

cum=5; else if(strcmp(mon,"Jun")==0)

cum=6; else if(strcmp(mon,"Jul")==0)

cum=7; else if(strcmp(mon,"Aug")==0)

cum=8; else if(strcmp(mon,"Sep")==0)

cum=9;

Page 69: Programing Solution

else if(strcmp(mon,"Oct")==0) cum=10;

else if(strcmp(mon,"Nov")==0) cum=11;

else cum=12;

for(i=20;i<=24;i++) yer[i-20]=d[i]; cuy=atoi(yer); for(i=8;i<=9;i++) da[i-8]=d[i]; da[i-8]='\0'; cud=atoi(da);

textcolor(YELLOW); cprintf(":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::"); printf("\n\n"); textcolor(RED); cprintf("++++++++++++++++++++DATA INPUT SECTION+++++++++++++++++++"); printf("\n\n"); textcolor(BLUE); cprintf("Enter birth(year-month-day)separate them by single space :");// scanf("%d %d %d",&Yb,&Mb,&Db);Yb=atoi(argv[1]);Mb=atoi(argv[2]);Db=atoi(argv[3]); printf("\n\n"); cprintf("Your Birthday is:%d-%d-%d",Db,Mb,Yb); printf("\n\n"); cprintf("OK..."); sleep(1); printf("\n\n"); textcolor(YELLOW); cprintf("Current Date is "); cprintf(asctime(ptr));

printf("\n\n");

switch(Mb) { case 1: m=31; break; case 2: m=leap(Yb)+28; break; case 3: m=31; break; case 4: m=30; break; case 5: m=31; break; case 6: m=30; break; case 7: m=31;

Page 70: Programing Solution

break; case 8: m=31; break; case 9: m=30; break; case 10: m=31; break; case 11: m=30; break; case 12: m=31; break; }/*Calculation Of Day */if(cud>=Db) { Da=(cud-Db)+1; /*Extra 1 is birthday*/ Mb=Mb; }else { Da=((cud+m)-Db+1); Mb=Mb+1; }/*Calculation Of Month */if(cum>=Mb) { Ma=(cum-Mb); Yb=Yb; }else { Ma=(cum+12)-Mb; Yb=Yb+1; } /*Calculation Of Year */ Ya=(cuy-Yb); textcolor(YELLOW); cprintf("::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::"); textcolor(RED); printf("\n\n"); cprintf("++++++++++++++++++++OUTPUT SECTION+++++++++++++++++"); printf("\n\n"); textcolor(WHITE); cprintf("Today Your Age is:%d years %d month %d days",Ya,Ma,Da); sleep(6); printf("\n\n"); textcolor(YELLOW); cprintf("::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::"); textcolor(RED); printf("\n\n"); cprintf("++++++++++++PROGRAMMER'S INFORMATION SECTION++++++++++++++++"); printf("\n\n"); textcolor(RED); cprintf("This Program's Source code is written by....."); sleep(3); printf("\n\n"); textcolor(RED+128); cprintf("MD: ANISUZZAMAN"); printf("\n\n"); sleep(3);

Page 71: Programing Solution

textcolor(YELLOW); cprintf("DEPARTMENT OF ICE,ISLAMIC UNIVERSITY"); printf("\n\n"); if(m==29) cprintf("Yahoo...Your Birth Year is a Leap Year"); printf("\n\n"); textcolor(RED+BLINK); cprintf("**************************** BYE******************************"); printf("\n\n"); textcolor(YELLOW); cprintf("::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::");

getch();}/*leap year ber kora*/int leap(int year){ int y,a; y=year;if((y%100&&y%4)==0||y%400==0)a=1;elsea=0;return(a);}

97. Write a nested macro that gives the minimum of three values.

Solution:#include<stdio.h>#include<conio.h>#define MIN(a,b) (((a)<b))?(a):(b))main(){int x,y,z,sma;clrscr();printf("Enter 3 values");scanf("%d%d%d",&x,&y,&z);sma= MIN(x, MIN(y,z)); /*use of nested macro*/printf("\n minimum number is %d",sma);getch();}

98. Write a program to illustrate malloc,realloc and free function.

Solution:#include<stdio.h>

int main(){

int *ptr_one;

ptr_one = (int *)malloc(sizeof(int));

if (ptr_one == 0){

printf("ERROR: Out of memory\n");return 1;

}

*ptr_one = 25;printf("%d\n", *ptr_one);

Page 72: Programing Solution

free(ptr_one);

return 0;}

99. Write a command line program to add three float where three float will be given by user.

Solution:#include<dos.h>#include<ctype.h>#include<time.h>#include<stdlib.h>#include<string.h>

void main(int argc, char *argv[]){ float a,b,c,sum;

if(argc<4) {

printf("You have to enter 3 values");return;

}a=atof(argv[1]);b=atof(argv[2]);c=atof(argv[3]);sum=a+b+c;printf("\nsum=%f",sum);getch();}

100. Write a simple program that can count how many consonants and vowels in your fullname.

Solution:

#include<stdio.h>#include<string.h>#include<ctype.h>

int iscons(char );int iscons(char c){ switch(c) { case 'a':

case 'e':case 'i':case 'o':case 'u':case 'A':case 'E':case 'I':case 'O':case 'U':return 0;break;

Page 73: Programing Solution

default:return 1;break;

}

}

int main(){ char str[100]; int i=0,j=0,k[100],c=0,v=0; printf("Enter a string:"); gets(str); printf("\n"); while(str[i]!='\0') { if(isalpha(str[i])) if((iscons(str[i]))==1)

c++; i++;

} v=strlen(str)-c;printf("\nNumber of consonant:%d\nAnd vowel:%d",c,v);return 0;}

Page 74: Programing Solution

Tarek Hasan-Al-MahmudLecturer

Dept. of ICE