C Programming Exam problems & Solution by sazzad hossain
1. Problems and solutions
1.1.1.1. ProgramProgramProgramProgram totototo findfindfindfind outoutoutout thethethethe temperaturetemperaturetemperaturetemperature inininin FahrenheitFahrenheitFahrenheitFahrenheit
#include <stdio.h>
void main()
{
float c, f;
printf("nEnter temperature in degree Centigrade: ");
scanf("%f", &c);
f = (1.8*c) + 32;
printf("n Temperature in degree Fahrenheit: %.2f", f);
getchar();
}
2.2.2.2.ProgramProgramProgramProgram totototo findfindfindfind outoutoutout thethethethe biggerbiggerbiggerbigger numbernumbernumbernumber betweenbetweenbetweenbetween thethethethe
twotwotwotwo numbersnumbersnumbersnumbers
#include<stdio.h>
#include<conio.h>
int main()
{
int a,b;
2. printf("Enter a numbern");
scanf("%d",&a);
printf("Enter a numbern");
scanf("%d",&b);
if(a>b)
{
printf("%d is greater than %dn",a,b);
}
else if(a,b)
{
printf("%d is less than %dn",a,b);
}
else
{
printf("%d is equal to %dn",a,b);
}
getchar();
return 0;
}
3.3.3.3.ProgramProgramProgramProgram totototo findfindfindfind outoutoutout eveneveneveneven andandandand oddoddoddodd number.number.number.number.
#include <stdio.h>
int main()
3. {
int a;
printf("Enter a: n");
scanf("%d", &a);
/* logic */
if (a % 2 == 0)
{printf("The given number is EVENn");
}
else
{printf("The given number is ODDn");
}
return 0;
}
4.4.4.4. ProgramProgramProgramProgram forforforfor squaresquaresquaresquare ofofofof aaaa numbernumbernumbernumber
#include<stdio.h>
#include<conio.h>
void main()
{
long int m,n;
printf("Please enter the number");
scanf("%ld" ,&n);
m=n;
n=n*n;
4. printf("Square of entered number %ld = %ld ",m,n);
}
5.5.5.5. ProgramProgramProgramProgram forforforfor summationsummationsummationsummation ofofofof aaaa seriesseriesseriesseries
NormalNormalNormalNormal series(1+3+series(1+3+series(1+3+series(1+3+…………..+n)..+n)..+n)..+n)
#include <stdio.h>
#include <conio.h>
void main()
{
int i, N, sum = 0;
clrscr();
printf("Enter The last number of the series n");
scanf ("%d", &N);
for (i=1; i <= N; i++)
{
sum = sum + i;
}
5. printf ("Sum of first %d natural numbers = %dn", N, sum);
}
WhenWhenWhenWhen differencedifferencedifferencedifference isisisis 2222
#include <stdio.h>
#include <conio.h>
void main()
{
int i, N, sum = 0;
clrscr();
printf("Enter The last number of the series n");
scanf ("%d", &N);
6. for (i=1; i <= N; i=i+2)
{
sum = sum + i;
}
printf ("Sum of the series is = %dn", sum);
}
SquareSquareSquareSquare series(1^2+3^2+series(1^2+3^2+series(1^2+3^2+series(1^2+3^2+…………..+n^2)..+n^2)..+n^2)..+n^2)
#include<stdio.h>
int main()
{
int n,i;
int sum=0;
printf("Enter the last number os the n i.e. max values of series: ");
scanf("%d",&n);
sum = (n * (n + 1) * (2 * n + 1 )) / 6;
printf("Sum of the series : ");
for(i =1;i<=n;i++){
if (i != n)
printf("%d^2 + ",i);
else
printf("%d^2 = %d ",i,sum);
7. }
return 0;
}
6.6.6.6. FactorialFactorialFactorialFactorial ofofofof aaaa numbernumbernumbernumber
#include <stdio.h>
int main()
{
int c, n, fact = 1;
printf("n Enter a number to calculate it's factorial: ");
scanf("%d", &n);
for (c = 1; c <= n; c++)
fact = fact * c;
printf("Factorial of %d = %d n", n, fact);
return 0;
}
7.7.7.7. ProgramProgramProgramProgram forforforfor standardstandardstandardstandard deviationdeviationdeviationdeviation ofofofof aaaa numbernumbernumbernumber
#include <stdio.h>
#include <math.h>
float standard_deviation(float data[], int n);
int main()
{
int n, i;
8. float data[100];
printf("Enter number of datas to be calculated to ndetermine the standard deviation( should be
less than 100): ");
scanf("%d",&n);
printf("Enter elements: ");
for(i=0; i<n; ++i)
scanf("%f",&data[i]);
printf("n");
printf("Standard Deviation = %.2f", standard_deviation(data,n));
return 0;
}
float standard_deviation(float data[], int n)
{
float mean=0.0, sum_deviation=0.0;
int i;
for(i=0; i<n;++i)
{
mean+=data[i];
}
mean=mean/n;
for(i=0; i<n;++i)
sum_deviation+=(data[i]-mean)*(data[i]-mean);
return sqrt(sum_deviation/n);
}
9. 8.8.8.8. WhetherWhetherWhetherWhether thethethethe yearyearyearyear isisisis leapleapleapleap yearyearyearyear orororor notnotnotnot
#include <stdio.h>
int main()
{
int year;
printf("n Enter a year to check whether it is a leap year or not: ");
scanf("%d", &year);
if ( year%4 == 0 )
printf("%d is a leap year.n", year);
else
printf("%d is not a leap year.n", year);
return 0;
}
9.9.9.9. WhetherWhetherWhetherWhether aaaa numbernumbernumbernumber isisisis aaaa primeprimeprimeprime numbernumbernumbernumber orororor notnotnotnot
#include<stdio.h>
int main()
{
int n, c = 2;
printf("n Enter a number to check if it is prime: ");
scanf("%d",&n);
for ( c = 2 ; c <= n - 1 ; c++ )
10. {
if ( n%c == 0 )
{
printf("%d is not prime.n", n);
break;
}
}
if ( c == n )
printf("%d is prime.n", n);
return 0;
}
10.10.10.10. TheTheTheThe biggestbiggestbiggestbiggest numbernumbernumbernumber amongamongamongamong threethreethreethree numbersnumbersnumbersnumbers
#include <stdio.h>
int main()
{int a, b, c;
printf("Enter a,b,c: n");
scanf("%d %d %d", &a, &b, &c);
if (a > b && a > c)
{printf("a is the biggest number");}
else if (b > a && b > c)
{printf("b is the biggest number ");}
11. else if (c > a && c > b)
{printf("c is the biggest number ");}
else {printf("all are equal or any two values are equal");}
return 0;
}
11.11.11.11. FibonacciFibonacciFibonacciFibonacci series.series.series.series.
#include<stdio.h>
main()
{
int n, first = 0, second = 1, next, c;
printf("n Enter the number of terms: ");
scanf("%d",&n);
printf("First %d terms of Fibonacci series are :-n",n);
for ( c = 0 ; c < n ; c++ )
{
if ( c <= 1 )
next = c;
else
{
next = first + second;
first = second;
second = next;
12. }
printf("%dn",next);
}
return 0;
}
12.12.12.12. WhetherWhetherWhetherWhether thethethethe twotwotwotwo stringstringstringstring areareareare equalequalequalequal
#include<stdio.h>
#include<string.h>
main()
{
char a[100], b[100];
printf("Enter the first string to be compared: ");
gets(a);
printf("nEnter the second string : ");
gets(b);
if( strcmp(a,b) == 0 )
printf("Entered strings are equal.n");
else
printf("Entered strings are not equal.n");
return 0;
}
13. 13.13.13.13. FindFindFindFind outoutoutout thethethethe lengthlengthlengthlength ofofofof aaaa stringstringstringstring
#include<stdio.h>
#include<string.h>
main()
{
char a[100];
int length;
printf("Enter a string to calculate it's length: ");
gets(a);
length = strlen(a);
printf("Length of entered string is = %dn",length);
return 0;
}
14.14.14.14. WhetherWhetherWhetherWhether aaaa particularparticularparticularparticular charactercharactercharactercharacter existsexistsexistsexists inininin aaaa stringstringstringstring
(frequency):(frequency):(frequency):(frequency):
#include <stdio.h>
int main(){
char c[1000],ch;
int i,count=0;
printf("Enter a string: ");
14. gets(c);
printf("Enter a characeter to find frequency: ");
scanf("%c",&ch);
for(i=0;c[i]!='0';++i)
{
if(ch==c[i])
++count;
}
printf("Frequency of %c = %d", ch, count);
return 0;
}
15.15.15.15. FindFindFindFind outoutoutout thethethethe maximummaximummaximummaximum andandandand minimumminimumminimumminimum numbernumbernumbernumber inininin anananan
arrayarrayarrayarray ::::
[maximum][maximum][maximum][maximum]
#include <stdio.h>
int main()
{
int array[100], maximum, size, c, location = 1;
printf("Enter the number of elements in arrayn");
scanf("%d", &size);
printf("Enter %d integersn", size);
for (c = 0; c < size; c++)
scanf("%d", &array[c]);
15. maximum = array[0];
for (c = 1; c < size; c++)
{
if (array[c] > maximum)
{
maximum = array[c];
location = c+1;
}
}
printf("Maximum element is present at location number %d and it's value is %d.n", location,
maximum);
return 0;
}
[Minimum][Minimum][Minimum][Minimum]
#include <stdio.h>
int main()
{
int array[100], minimum, size, c, location = 1;
printf("Enter the number of elements in arrayn");
scanf("%d",&size);
printf("Enter %d integersn", size);
for ( c = 0 ; c < size ; c++ )
scanf("%d", &array[c]);
minimum = array[0];
for ( c = 1 ; c < size ; c++ )
{
16. if ( array[c] < minimum )
{
minimum = array[c];
location = c+1;
}
}
printf("Minimum element is present at location number %d and it's value is %d.n", location,
minimum);
return 0;
}
16.16.16.16. FindFindFindFind outoutoutout powerpowerpowerpower valuevaluevaluevalue
#include <stdio.h>
int main()
{
int base, exp;
long int value=1;
printf("Enter base number and exponent respectively: ");
scanf("%d%d", &base, &exp);
while (exp!=0)
{
value*=base;
--exp;
17. }
printf("Answer = %d", value);
}
17.17.17.17. ProgramProgramProgramProgram forforforfor salarysalarysalarysalary sheetsheetsheetsheet
#include<stdio.h>
#include<conio.h>
struct employ
{
char name[20];
int id,bp;
float tot,hra,dr;
};
void main()
{
struct employ list[20],temp;
char name[30];
int n,i,j,m;
printf("Enter the number of employees: ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("nEnter the name of employee: ");
19. getchar();
}
18.18.18.18. ProgramProgramProgramProgram forforforfor findfindfindfind thethethethe valuevaluevaluevalue totototo thethethethe followingfollowingfollowingfollowing equationequationequationequation ()()()()
#include<stdio.h>
#include<math.h>
int main(){
float a,b,c;
float d,root1,root2;
printf("Enter a, b and c of quadratic equation: ");
scanf("%f%f%f",&a,&b,&c);
d = b * b - 4 * a * c;
if(d < 0){
printf("Roots are complex number.n");
printf("Roots of quadratic equation are: ");
21. 19.19.19.19. ConversionConversionConversionConversion ofofofof decimaldecimaldecimaldecimal numbernumbernumbernumber totototo binarybinarybinarybinary numbernumbernumbernumber
#include <stdio.h>
int main()
{
int n, c, k;
printf("Enter an integer in decimal number systemn");
scanf("%d", &n);
printf("%d in binary number system is:n", n);
for (c = 31; c >= 0; c--)
{
k = n >> c;
if (k & 1)
printf("1");
else
printf("0");
}
printf("n");
return 0;
}
20.20.20.20. ProgramProgramProgramProgram forforforfor studentsstudentsstudentsstudents gradinggradinggradinggrading usingusingusingusing structurestructurestructurestructure
#include<stdio.h>
24. getch();
}
21.21.21.21. TakeTakeTakeTake somesomesomesome inputinputinputinput andandandand findfindfindfind sumsumsumsum andandandand averageaverageaverageaverage
#include<stdio.h>
#include<conio.h>
void main()
{
int i,sum=0,n,x;
printf("nHow many numbers you want to input? : ",n);
scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf("nEnter numbers to add: ",x);
scanf("%d",&x);
sum=sum+x;
}
printf("nSum is: %d",sum);
printf("nAverage is: %d",sum/n);
}
22.22.22.22. WriteWriteWriteWrite aaaa programprogramprogramprogram totototo sortsortsortsort somesomesomesome numbersnumbersnumbersnumbers inininin ascendingascendingascendingascending
orderorderorderorder
25. #include <stdio.h>
int main()
{
int n, array[1000], c, d, t;
printf("Enter number of elementsn");
scanf("%d", &n);
printf("Enter %d integersn", n);
for (c = 0; c < n; c++) {
scanf("%d", &array[c]);
}
for (c = 1 ; c <= n - 1; c++) {
d = c;
while ( d > 0 && array[d] < array[d-1]) {
t = array[d];
array[d] = array[d-1];
array[d-1] = t;
d--;
}
}
printf("Sorted list in ascending order:n");
for (c = 0; c <= n - 1; c++) {
printf("%dn", array[c]);
}
return 0;
}
26. 23.23.23.23. WriteWriteWriteWrite aaaa programprogramprogramprogram totototo findfindfindfind totototo reversereversereversereverse stringstringstringstring
#include<stdio.h>
#include<string.h>
main()
{
char arr[100];
printf("n Enter a string to reverse: ");
gets(arr);
strrev(arr);
printf("Reverse of entered string is n%sn",arr);
return 0;
}
24.24.24.24. TakeTakeTakeTake somesomesomesome inputinputinputinput inininin aaaa tabletabletabletable andandandand displaydisplaydisplaydisplay themthemthemthem (two(two(two(two
dimensionaldimensionaldimensionaldimensional array)array)array)array) andandandand add/multiplyadd/multiplyadd/multiplyadd/multiply themthemthemthem
#include<stdio.h>
int main()
{
int matrix1[12][12], matrix2[12][12], sum[12][12],multi[12][12], i, j, m,n,p,q;
printf("Enter the order of first matrix: ");
scanf("%d%d",&m,&n);
printf("Enter the order of second matrix: ");
27. scanf("%d%d",&p,&q);
if(m!=p && n!=q){
printf("Order of matrix did not matched!!");
}
printf("Enter first matrix: n");
for(i = 0 ; i < m; i++){
for(j = 0; j < n; j++)
scanf("%d", &matrix1[i][j]);
}
printf("Enter second matrix: n");
for(i = 0 ; i < p; i++){
for(j = 0; j < q; j++)
scanf("%d", &matrix2[i][j]);
}
for(i = 0 ; i < m; i++){
for(j = 0; j < n; j++)
sum[i][j] = matrix1[i][j] + matrix2[i][j];
}
printf("The sum of the matrix is :n");
for(i = 0 ; i < m; i++){
for(j = 0; j < n; j++){
printf("%d", sum[i][j]);
printf("t");
}
28. printf("n");
}
for(i = 0 ; i < m; i++){
for(j = 0; j < n; j++)
multi[i][j] = matrix1[i][j] * matrix2[i][j];
}
printf("The multiplication of the matrix is :n");
for(i = 0 ; i < m; i++){
for(j = 0; j < n; j++){
printf("%d", multi[i][j]);
printf("t");
}
printf("n");
}
return 0;
}
25.25.25.25. TakeTakeTakeTake inputinputinputinput andandandand findfindfindfind sumsumsumsum usingusingusingusing functionfunctionfunctionfunction
#include<stdio.h>
#include<conio.h>
void sum();
void main()
29. {
sum();
getchar();
}
void sum()
{
int n1,n2,s;
printf("nEnter two numbersn ");
scanf("%d%d",&n1,&n2);
s=n1+n2;
printf("n the sum is %d",s);
}
26.26.26.26. ProgramProgramProgramProgram forforforfor stringstringstringstring compare,compare,compare,compare, copycopycopycopy andandandand additionadditionadditionaddition
[Compare][Compare][Compare][Compare]
#include<stdio.h>
#include<string.h>
main()
{
char a[100], b[100];
printf("Enter the first stringn");
gets(a);
30. printf("Enter the second stringn");
gets(b);
if( strcmp(a,b) == 0 )
printf("Entered strings are equal.n");
else
printf("Entered strings are not equal.n");
return 0;
}
[Copy][Copy][Copy][Copy]
#include<stdio.h>
void copy_string(char*, char*);
main()
{
char source[100], target[100];
printf("Enter source stringn");
gets(source);
copy_string(target, source);
printf("Target string is "%s"n", target);
return 0;
}
void copy_string(char *target, char *source)
{
while(*source)
31. {
*target = *source;
source++;
target++;
}
*target = '0';
}
[Addition][Addition][Addition][Addition]
#include<stdio.h>
#include<conio.h>
#include<string.h>
main()
{
char a[100], b[100];
printf("Enter the first stringn");
gets(a);
printf("Enter the second stringn");
gets(b);
strcat(a,b);
printf("String obtained on addition is %sn",a);
getchar();
return 0;
}