0% found this document useful (0 votes)
142 views19 pages

Programming (C) Question Solve (2014-2009) RUET

This document contains 15 C programming questions and their solutions from previous years' CSE exams from 2014 to 2012. The questions cover topics like calculating volume of hollow cylinders, matrix multiplication and inversion, Newton-Raphson method for solving equations, finding largest number in an array, and transposing matrices. Each question includes the full source code to solve the given problem.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
142 views19 pages

Programming (C) Question Solve (2014-2009) RUET

This document contains 15 C programming questions and their solutions from previous years' CSE exams from 2014 to 2012. The questions cover topics like calculating volume of hollow cylinders, matrix multiplication and inversion, Newton-Raphson method for solving equations, finding largest number in an array, and transposing matrices. Each question includes the full source code to solve the given problem.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

CSE Previous Year Question

Solves (2014-2009)
BY Al-Amin CE’13

MAY 18, 2016


SHUVO-PC
1.///A hollow cylinder sample having inner diameter,r1= 3cm, outer
diameter,r2= 5cm and height of 6cm.
///A program to calculate the hollow volume and solid volume of the
soil mass which can be subjected to hollow cylindrical test.’09
#include<stdio.h>
#define pi 3.14159
int main()
{
float hv, sv, r1=3, r2=5, h=6;
hv=pi*r1*r1*h;
sv=pi*(r2-r1)*(r2-r1)*h;

printf("Hollow volume = %.4fcu cm\n", hv);


printf("Solid volume = %.4fcu cm\n", sv);
return 0;
}
2.// matrix A=kB ‘09
#include<stdio.h>
#include<math.h>
int main()
{
FILE *f1;
f1=fopen("09_input.txt","r");
int i, j, k, A[2][2], B[2][2], C[2][2], b[2][2], D[2][2], sum;
//printf("Enter matrix A of order 2*2\n");
for(i=0;i<2;i++)
for(j=0;j<2;j++)
fscanf(f1,"%d",&A[i][j]);

//printf("Enter matrix B of order 2*2\n");


for(i=0;i<2;i++)
for(j=0;j<2;j++)
fscanf(f1,"%d",&B[i][j]);

printf("Entered matrix A is...\n\n");


for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
printf("%d\t", A[i][j]);
printf("\n\n");
}

printf("Entered matrix B is...\n\n");


for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
printf("%d\t", B[i][j]);
printf("\n\n");
}

///inverse of B matrix
sum=B[0][0]*B[1][1]-B[1][0]*B[0][1]; ///determinant of B matrix

///adjoint matrix of B
for(i=0;i<2;i++)
for(j=0;j<2;j++)
b[i][j]=pow(-1,i+j)*B[(i+1)%2][(j+1)%2];

///last process with transpose


for(i=0;i<2;i++)
for(j=0;j<2;j++)
C[i][j]=b[j][i]/sum;

///multiplication of A and inverse of B(=C)


for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
D[i][j]=0;
for(k=0;k<2;k++)
D[i][j]=D[i][j]+A[i][k]*C[k][j];
}
}

printf("The matrix k is...\n");


for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
printf("%d\t", D[i][j]);
printf("\n\n");
}
fclose(f1);
return 0;
}

3.//Print matrix A+2AB ‘10


#include<stdio.h>
int main()
{
int i, j, k, A[3][3], B[3][3], C[3][3];
printf("Enter matrix A of order 3*3\n");
for(i=0;i<3;i++)
for(j=0;j<3;j++)
scanf("%d",&A[i][j]);
printf("Enter matrix B of order 3*3\n");
for(i=0;i<3;i++)
for(j=0;j<3;j++)
scanf("%d",&B[i][j]);

printf("Entered matrix A is...\n\n");


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

printf("Entered matrix B is...\n\n");


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

for(i=0;i<3;i++)
for(j=0;j<3;j++)
{
C[i][j]=0;
for(k=0;k<3;k++)
C[i][j]=C[i][j]+A[i][k]*B[k][j];
}

printf("The matrix is...\n");


for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
printf("%d\t", A[i][j]+3*C[i][j]);
printf("\n\n");
}
return 0;
}

4.//print input matrix’10


#include<stdio.h>
int main()
{
int i, j, A[4][4];
printf("Enter matrix A of order 4*4\n");
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
scanf("%d",&A[i][j]);
}

printf("Entered matrix A is...\n");


for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
printf("%d\t", A[i][j]);
printf("\n\n");
}

return 0;
}

5.///A program to find the real root of the equation sin(x)-x+1=0


using Newton-Raphson method.’10
#include<stdio.h>
#include<math.h>

float fx(float m);


float fx1(float n);

int main()
{
float i, x0,x1;
printf("Enter the value of x0\n");
scanf("%f",&x0);

for(i=0;i<1000;i++)
{
x1=x0-fx(x0)/fx1(x0);
x0=x1;
}

printf("Answer = %f\n", x0);

return 0;
}

float fx(float m)
{
float p;
p=sin(m)-m+1;
return p;
}

float fx1(float n)
{
float q;
q=cos(n)-1;
return q;
}

6.//1+2+4+6+.........+n ‘10
#include<stdio.h>
int main()
{
int i, sum=1, n=20;
for(i=1;i<=n;i++)
{
if(i%2==0)
sum=sum+i;
}
printf("\nsum = %d\n", sum);
return 0;
}

7./// Find the shear, moment, shearing stress, flexural stress at


every L/10 distance from the left end and find the maximum moment.’10
/// p
/// w ->X |
/// /\ /\ /\ /\ |/\ /\ | b
/// _____________________________ |///////|
/// /\ OO |///////|
/// /// /// |///////| h
/// |<-----------L--------------->| |///////|
/// |<---------a------->| |///////|
/// | X-X
/// R1 ->X R2
#include<stdio.h>
int main()
{
float V, M, C, Q, I, b, h, Ss, Sf, L, w, p, a, R1, R2;
printf("Enter span length L\n");
scanf("%f", &L);
printf("Enter the value of point load p\n");
scanf("%f", &p);
printf("Enter the distance of point load p from left end\n");
scanf("%f", &a);
printf("Enter distributed load w\n");
scanf("%f", &w);
printf("Enter cross section width b\n");
scanf("%f", &b);
printf("Enter cross section height h\n");
scanf("%f", &h);

C=h/2;
I=b*h*h*h/12;
Q=b*(h/2)*(h/4);
R2=(p*a+w*x*x/2)/L;
R1=p+w*x-R2;

for(float x=0;x<=L;x+=(L/10))
{
V=-R2+p+w*x;
M=p*x+w*x*x/2;
Sf=(M*C)/I;
Ss=(V*Q)/(I*b);

printf("\nAt %.3f distance\n", x);


printf("Shear %.3f\nMoment %.3f\nShearing stress
%.3f\nFlexural stress %.3f\n\n", V, M, Ss, Sf);
}

return 0;
}

8.//1^2/1!+2^2/2!+3^2/3!+.................+n^2/n! ’11 ‘13


#include<stdio.h>
int main()
{
float i, j, n, sum=0, fact;
printf("please enter the value of n\n");
again:
scanf("%f", &n);

if(n<=0)
{
printf("\nInvalid input.\n\nPlease input
correctly.........\n");
goto again;
}
else
{
for(i=1;i<=n;i++)
{
fact=1;
for(j=1;j<=i;j++)
fact=fact*j;
sum=sum+(i*i/fact);
}
}
printf("\nsum = %f\n", sum);
return 0;
}
9.//Average best class test using function’11
#include<stdio.h>
float serial(float a, float b, float c, float d)
{
float avg;
if(a<=b && a<=c && a<=d)
avg=(b+c+d)/3;
else if(b<=c && b<=d && b<=a)
avg=(c+d+a)/3;
else if(c<=d && c<=a && c<=b)
avg=(d+a+b)/3;
else
avg=(a+b+c)/3;

return avg;
}

int main()
{
float a, b, c, d, p;
printf("please input the marks of four class tests\n");
scanf("%f%f%f%f", &a, &b, &c, &d);
p=serial(a, b, c, d);
printf("\naverage mark = %.2f\n", p);
return 0;
}

10.///A program to find the real root of the equation e^(-x)-x=0 using
Iteration method.(iteration method) ’11
#include<stdio.h>
#include<math.h>
int main()
{
float i, x0,x1;
printf("Enter the value of x0\n");
scanf("%f",&x0);

for(i=0;i<1000;i++)
{
x1=exp(-x0);
x0=x1;
}

printf("Answer = %f\n", x0);

return 0;
}

11.//matrix A + AB ‘11
#include<stdio.h>
int main()
{
int i, j, k, A[3][3], B[3][3], C[3][3];
printf("Enter matrix A of order 3*3\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
scanf("%d",&A[i][j]);
}
printf("Enter matrix B of order 3*3\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
scanf("%d",&B[i][j]);
}

printf("Entered matrix A is...\n\n");


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

printf("Entered matrix B is...\n\n");


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

for(i=0;i<3;i++)
for(j=0;j<3;j++)
{
C[i][j]=0;
for(k=0;k<3;k++)
C[i][j]=C[i][j]+A[i][k]*B[k][j];
}

printf("The matrix is...\n");


for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
printf("%d\t",A[i][j]+C[i][j]);
printf("\n\n\n");
}
return 0;
}

12./Reverse order ‘11


#include<stdio.h>
int main()
{
int i,n;
printf("Enter the number of integer\n");
scanf("%d", &n);

int a[n];
printf("Input the numbers:\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);

printf("The reverse order is...\n");


for(i=n-1;i>=0;i--)
printf("%d\t",a[i]);

return(0);
}

13.//largest value in between n numbers’12


#include<stdio.h>
int main()
{
int a,i,tem,n,k=-1;
printf("enter how many numbers:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter number %d:",i+1);
scanf("%d",&a);
if(k<a)
{
k=a;
}
}
printf("the max number is: %d\n", k);
return 0;
}
14.///A program to find the real root of the equation x^10-1=0 using
Newton-Raphson method.’12
#include<stdio.h>
#include<math.h>
float fx(float m);
float fx1(float n);

int main()
{
float i, x0,x1;
printf("Enter the value of x0\n");
scanf("%f",&x0);

for(i=0;i<1000;i++)
{
x1=x0-fx(x0)/fx1(x0);
x0=x1;
}

printf("Answer = %f\n", x0);

return 0;
}

float fx(float m)
{
float p;
p=pow(m,10)-1;
return p;
}

float fx1(float n)
{
float q;
q=10*pow(n,9);
return q;
}

15.//print input matrix &its transpose’13’14


#include<stdio.h>
int main()
{
int i, j, m, n;
printf("enter the order m & n\n");
scanf("%d%d", &m, &n);
int A[m][n];
printf("Enter matrix A of order %d*%d\n", m, n);
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
scanf("%d",&A[i][j]);
}
printf("Entered matrix A is...\n\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
printf("%d\t",A[i][j]);
printf("\n\n");
}

printf("The transpose of matrix A is...\n\n");


for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
printf("%d\t",A[j][i]);
printf("\n\n\n");
}
return 0;
}

16.///A program to find the real root of the equation x+log(x)=2 using
Newton-Raphson method.’13
#include<stdio.h>
#include<math.h>

float fx(float m);


float fx1(float n);

int main()
{
float i, x0,x1;
printf("Enter the value of x0\n");
scanf("%f",&x0);

for(i=0;i<1000;i++)
{
x1=x0-fx(x0)/fx1(x0);
x0=x1;
}

printf("Answer = %f\n", x0);

return 0;
}

float fx(float m)
{
float p;
p=m+log(m)-2;
return p;
}

float fx1(float n)
{
float q;
q=1+1/n;
return q;
}

17.//Check if input number is odd or even? ‘13


#include<stdio.h>
int main()
{
int n;
printf("Enter a number\n");
scanf("%d", &n);

if(n%2==0)
printf("The number is even.\n");
else
printf("The number is odd.\n");

return 0;
}

18.// Print number of palindrome’13


#include<stdio.h>
int main()
{
int n, p, i, j, s, sum, cnt=0;
scanf("%d", &n);
while(n!=0)
{
cnt++;
n/=10;
}
p=cnt;
printf("Digit length = %d\n", p);

int a[p];
for(i=p-1;i>=0;i--)
{
a[i]=n%10;
n/=10;
}
for(i=0, j=p-1;i<j;i++, j--)
{
if(a[i]==a[j])
s=1;
else
s=0;
}

if(s==1)
printf("\nPalindrome\n");
else
printf("\nNot Palindrome\n");

return 0;
}

19.//(1/2)*(3/4)+(2/3)*(4/5)+.............(n/(n+1))*((n+2)/(n+3))’13
#include<stdio.h>
int main()
{
float i,n,sum=0;
printf("enter the value of n\n");
scanf("%f",&n);

for(i=1;i<=n;i++)
sum=sum+(i/(i+1))*((i+2)/(i+3));

printf("\nsum = %f\n",sum);

return 0;
}
20.//Average of n numbers and deviation from average ‘14
#include<stdio.h>
#include<math.h>
int main()
{
int n, i;
float sum=0, avg;
printf("How many numbers\n");
start:
scanf("%d", &n);
if(n<=0)
{
printf("\nInvalid input. Please input positive value\n");
goto start;
}
else
{
int a[n];
for(i=0;i<n;i++)
{
printf("Enter number:%d\n", i+1);
scanf("%d", &a[i]);
sum=sum+a[i];
}

avg=sum/n;
printf("\nAverage = %.2f\n", avg);

for(i=0;i<n;i++)
printf("\nDeviation of %d from average is = %.2f", a[i],
fabs(avg-a[i]));
}

return 0;
}

21.///A program to find the real root of the equation e^(x)=cot(x)


using Iteration method.’14
#include<stdio.h>
#include<math.h>
float fn(float x);
int main()
{
float i, a, b, x1, x2, x;
again:
printf("Guess the value of a and b\n");
scanf("%f%f", &a, &b);
if(fn(a)*fn(b)<0 && a<b)
{
for(i=1;i<=1000;i++)
{
x=a-fn(a)*(b-a)/(fn(b)-fn(a));
if(fn(x)<0)
a=x;
else if(fn(x)>0)
b=x;
else
break;
}
}
else
{
printf("Your guess is not correct.\n");
goto again;
}
printf("Answer = %f\n", x);
return 0;
}

float fn(float x)
{
float p;
p=exp(x)*tan(x);
return p;
}

22.//Half pyramid of asterics ‘14


#include<stdio.h>
int main()
{
int i, j, r;
printf("Enter the row of pyramid\n");
scanf("%d", &r);

for(i=1;i<=r;i++)
{
for(j=1;j<=2*i-1;j++)
printf("*");
printf("\n");
}

printf("\n\nI don't know which is correct pyramid...up or down.


Choose any of them.\n\n\n");

for(i=1;i<=r;i++)
{
for(j=1;j<=i;j++)
printf("*");
printf("\n");
}

return 0;
}

23.//Matrix multiplication using function’14


#include<stdio.h>

int scan(int P[][2])


{
int i,j;
for(i=0;i<2;i++)
for(j=0;j<2;j++)
scanf("%d",&P[i][j]);

return 0;
}

int main()
{
int i, j, k, A[2][2], B[2][2], C[2][2];

printf("Enter matrix A of order 2*2\n");


scan(A);

printf("Enter matrix B of order 2*2\n");


scan(B);

printf("The matrix A is...\n\n");


for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
printf("%d\t", A[i][j]);
printf("\n\n\n");
}

printf("The matrix B is...\n\n");


for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
printf("%d\t", B[i][j]);
printf("\n\n\n");
}

for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
C[i][j]=0;
for(k=0;k<2;k++)
C[i][j]=C[i][j]+A[i][k]*B[k][j];
}
}

printf("The matrix is...\n\n");


for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
printf("%d\t",C[i][j]);
printf("\n\n\n");
}
return 0;
}

24.//Second largest& smallest number ‘14


#include<stdio.h>
int main()
{
int i, j, n, tem;
printf("How many numbers?\n");
scanf("%d",&n);

int a[n];
for(i=0;i<n;i++)
{
printf("Enter number: %d\n",i+1);
scanf("%d",&a[i]);
}

for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
if(a[i]<a[j])
{
tem=a[i];
a[i]=a[j];
a[j]=tem;
}
}
}

printf("\n2nd largest number = %d\n",a[1]);


printf("\n2nd smallest number = %d\n",a[n-2]);

return 0;

}
25.//Students of highest mark’14
#include<stdio.h>
int main()
{
int marks[10], i, cnt=0, k=0;
for(i=0;i<10;i++)
{
printf("Enter the marks of Roll: %d\n", 130001+i);
scanf("%d",&marks[i]);
if(k<=marks[i])
k=marks[i];
}
printf("\nHighest mark = %d\n", k);

printf("\nLists of Roll numbers who got highest mark:\n\n");


for(i=0;i<10;i++)
{
if(k==marks[i])
{
cnt++;
printf("%d\n",i+130001);
}
}
printf("\nTotal students counted who got highest mark = %d\n",
cnt);

return 0;
}

26.//1-(x^2)/2!+(x^4)/4!-..................’14
#include<stdio.h>
#include<cmath>
int main()
{
float x, n, i, j, sum=1,fact, f=-1;
printf("Enter the value of n and x\n");
scanf("%f%f", &n, &x);
for(i=2;i<=n;i+=2)
{
fact=1;
for(j=1;j<=i;j++)
fact=fact*j;
sum=sum+((pow(x,i)*f)/fact);
f*=-1;
}
printf("\nsum = %f\n", sum);
return 0;
}

You might also like