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.
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
1. Write a program to verify that each word of a given paragraph is started with uppercase or not.
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.
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 .
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)
{ 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);
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>
#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);
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];
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.
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”.
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.
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>
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.
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;}
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;
}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);
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]); }
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("%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.
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()
{ 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];
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");
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.
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();
}
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)
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
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.
{ 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;
{ 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;
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;
{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
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; }
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.
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);
57. Write a program to convert a given decimal number to its equivalent number of any given base.
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);
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.
{ 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:
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;
num=num/10; count++;
} printf("Total disits in this integer=%ld",count);
}
65. Write a program to calculate the product of two matrix.
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;
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.
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);
}
}}
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);
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++)
#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]);
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;};
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(); }
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();
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
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;}
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;
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.
} 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);
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.
{ 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':
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); }
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 :
#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; 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!
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();
}
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.
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)
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);
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.
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*/
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;
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.
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;
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);
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);
free(ptr_one);
return 0;}
99. Write a command line program to add three float where three float will be given by user.
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;}