0% found this document useful (0 votes)
12 views

MSC Complete C File

The document discusses several C programming questions and programs related to arithmetic operations, functions, integrals, and date calculations. Multiple programs are provided to demonstrate different concepts like Simpson's rule, trapezoidal rule, prime number checks, and payroll calculations through functions.

Uploaded by

gsbhootstrap
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)
12 views

MSC Complete C File

The document discusses several C programming questions and programs related to arithmetic operations, functions, integrals, and date calculations. Multiple programs are provided to demonstrate different concepts like Simpson's rule, trapezoidal rule, prime number checks, and payroll calculations through functions.

Uploaded by

gsbhootstrap
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/ 63

Session 1

Program 1
QUESTION 1:
#include<stdio.h>
int main(void)
{
printf("This is easy!!\n");
return 0;
}

OUTPUT:

QUESTION 2:
#include<stdio.h>
#include<conio.h>
int main()
{
printf("This is easy!!");
return 0;
}

OUTPUT:
QUESTION 3

#include<stdio.h>
#include<conio.h>
int main()
{
printf("This is very easy!!");
return 0;
}
OUTPUT:

Program 2
QUESTION 1 :
#include<stdio.h>
#include<conio.h>
int main()
{
int x=5,y=7,z;
printf("x=%d,y=%d\n",x,y);
z=x-y;
printf("z=x-y=%d\n",z);
z=x*y;
printf("z=x*y=%d\n",z);
z=x/y;
printf("z=x/y=%d\n",z);
z=y/x;
printf("z=y/x=%d\n",z);
z=x%y;
printf("z=x%%y=%d\n",z);
z=y%x,
printf("z=y%%x=%d\n",z);
return 0;
}
OUTPUT:

QUESTION 2(1):
#include<stdio.h>
#include<conio.h>
int main()
{
int x=12,y=13,z;
printf("x=%d,y=%d\n",x,y);
z=x-y;
printf("z=x-y=%d\n",z);
z=x*y;
printf("z=x*y=%d\n",z);
z=x/y;
printf("z=x/y=%d\n",z);
z=y/x;
printf("z=y/x=%d\n",z);
z=x%y;
printf("z=x%%y=%d\n",z);
z=y%x,
printf("z=y%%x=%d\n",z);
return 0;
}
OUTPUT:

QUESTION 2(2):
#include<stdio.h>
#include<conio.h>

int main()
{
int x=11,y=14,z;
printf("x=%d,y=%d\n",x,y);
z=x-y;
printf("z=x-y=%d\n",z);
z=x*y;
printf("z=x*y=%d\n",z);
z=x/y;
printf("z=x/y=%d\n",z);
z=y/x;
printf("z=y/x=%d\n",z);
z=x%y;
printf("z=x%%y=%d\n",z);
z=y%x,
printf("z=y%%x=%d\n",z);
return 0;
}

QUESTION 3(1):
#include<stdio.h>
#include<conio.h>
int main()
{
int x=1.2,y=4.1,z;
printf("x=%d,y=%d\n",x,y);
z=x-y;
printf("z=x-y=%d\n",z);
z=x*y;
printf("z=x*y=%d\n",z);
z=x/y;
printf("z=x/y=%d\n",z);
z=y/x;
printf("z=y/x=%d\n",z);
z=x%y;
printf("z=x%%y=%d\n",z);
z=y%x,
printf("z=y%%x=%d\n",z);
return 0;
}
OUTPUT:

QUESTION 3(2):
#include<stdio.h>
#include<conio.h>
int main()
{
int x=0.2,y=5,z;
printf("x=%d,y=%d\n",x,y);
z=x-y;
printf("z=x-y=%d\n",z);
z=x*y;
printf("z=x*y=%d\n",z);
z=x/y;
printf("z=x/y=%d\n",z);
z=y/x;
printf("z=y/x=%d\n",z);
z=x%y;
printf("z=x%%y=%d\n",z);
z=y%x,
printf("z=y%%x=%d\n",z);
return 0;
}

Program 3
QUESTION 1:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
float f(float x)
{
return(1/(1+x*x));
}
int main()
{
float integral=0,h=1/4.0;
integral=(h/3.0)*(f(0)+4*(f(h)+f(3*h))+2*f(2*h)+f(4*h));
printf("The inegral is %f\n",integral);
return 0;
}
Session 2
Program 1
#include<stdio.h>
#include<conio.h>
int main()
{
int x,y,z;
x=2+3-4+5-(6-7);
y=2*33+4*(5-6);
z=2*3*4/(15%13);
printf("x=%d,y=%d,z=%d\n",x,y,z);
x=2*3*4/(15%13);
y=2*3*(4/15%13);
z=2+33%5/4;
printf("x=%d,y=%d,z=%d\n",x,y,z);
x=2+33%-5/4;
y=2-33%-5/-4;
z=-2*-3/-4%-5;
printf("x=%d,y=%d,z=%d\n",x,y,z);
x=50%(5*(16%12*(17/3)));
y=-2*-3%-4/-5-6+-7;
z=8/4/2*2*4*8%13%7%3;
printf("x=%d,y=%d,z=%d\n",x,y,z);
getch();
}

PROGRAM 2
#include<stdio.h>
#include<conio.h>
int main()
{
int x=3,y=5,z=7,w;
w=x% y+y% x-z% x-x%z;
printf("%d\n",w);
w=x/z+y/z+(z+y)/z;
printf("%d\n",w);
w=x/z*y/z+x*y/z;
printf("%d\n",w);
w=x%y%z+z%y%(y%x);
printf("%d\n",w);
w=z/y/y/x+z/y/(y/x);
printf("%d\n",w);
getch();
}

PROGRAM 3
#include<stdio.h>
#include<conio.h>
int main()
{
int a,b,ans1,ans2;
a=-10;
b=5;
printf("a=%d\nb=%d\n",a,b);
ans1=a%b;
ans2=a-(a/b)*b;
printf("The difference is %d\n",ans1-ans2);
a=-10;
b=-5;
printf("a=%d\nb=%d\n",a,b);
ans1=a%b;
ans2=a-(a/b)*b;
printf("The difference is %d\n",ans1-ans2);
a=10;
b=-5;
printf("a=%d\nb=%d\n",a,b);
ans1=a%b;
ans2=a-(a/b)*b;
printf("The difference is %d\n",ans1-ans2);
a=10;
b=5;
printf("a=%d\nb=%d\n",a,b);
ans1=a%b;
ans2=a-(a/b)*b;
printf("The difference is %d\n",ans1-ans2);
getch();
}

OUTPUT:

PROGRAM 4
#include<stdio.h>
#include<conio.h>
int main()
{
int a,b,ans1,ans2;
printf("Enter the value of a\n");
scanf("%d",&a);
printf("Enter the value of b\n");
scanf("%d",&b);
printf("a=%d\nb=%d\n",a,b);
ans1=a%b;
ans2=a-(a/b)*b;
printf("The difference is%d\n",ans1-ans2);
return 0;
}

OUTPUT:
PROGRAM 5
#include<stdio.h>
#include<conio.h>
int main()
{
float bp,da,hra,grpay;
printf("\nEnter the Basic Pay of ABSAR ANSARI:");
scanf("%f",&bp);

da=0.46*bp;
hra=0.30*bp;
grpay=bp+da+hra;
printf("\nBasic Pay of ABSAR ANSARI=%f",bp);
printf("\nDearness Allowance=%f",da);
printf("\House Rent Allowance=%f",hra);
printf("\nGross Pay of ABSAR ANSARI is%f",grpay);

printf("\n\n\n\n\nPress any Key to exit...");


getch();
}

OUTPUT:
Session 3
Program 1
#include<stdio.h>
#include<conio.h>
#include<limits.h>
void main()
{
unsigned long x,y;
printf("Enter the value of x...\n");
scanf("%lu", &x);
printf("Enter the value of y..\n");
scanf("%lu", &y);
if(x>ULONG_MAX-y)
printf("The sum of x and y exceeds\ the limits of ULONG_MAX\n");
else
printf("The sum of x and y is %lu\n", x+y);
getch();
}

OUTPUT:
Program 2
#include<stdio.h>
int main()
{
int pre ,cur, next,n,i,l,d;
pre=0,cur=1;
printf("\n Enter the choice \n\t1.print fibonacci series \n\t2.To find nth fibonacci no\n");
scanf("%d",&d);
switch(d)
{
case 1 :printf("\n Enter the no of terms\t");
scanf("%d",&n);
printf("\n Fibonacci series with %d terms :\n",n);
for(i=1;i<=n;i++)
{
printf("%d\t",pre);
next=pre+cur;
pre=cur;
cur=next;
}
break;
case 2: printf("\n Enter the nth term\t");
scanf("%d",&n);
if(n<=2)
{ if(n==1)
printf("%d\t",pre);
else
printf("%d\t",cur);
}
else
{
for(i=1;i<n;i++)
{
next = pre+cur;
pre=cur;
cur=next;
}
}
printf("%d\t",pre);
break;
default : printf("Invoalid entry");
}
printf("\n\n");
return(0);
}

OUTPUT:

Program 3
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
int number, factor;
printf("Enter a number, I\'ll tell you if it\'s a prime:");
scanf("%d", &number);
if(number==2 || number==3)
{
printf("%d is a prime..\n", number);
exit(0);
}
else if (number %2==0)
{
printf("%d is composite..\n", number);
exit(0);
}
for(factor=3;; factor+=2)
if(number % factor==0)
{
printf("%d is composite...\n",number);
break;
}
else if (factor*factor> number)
{
printf("%d is a prime...\n", number);
break;
}
getch();
}

OUTPUT:
Session 4
Program
#include<stdio.h>
#include <conio.h>
#include<math.h>
int main()
{
int day1,mon1,year1,day2,mon2,year2;
int ref,dd1,dd2,i;
printf("\nEnter the first day ,month,year\n");
scanf("%d%d%d",&day1,&mon1,&year1);
printf("\n Enter the second day,month,year\n");
scanf("%d%d%d",&day2,&mon2,&year2);
ref=year1;
if(year2<year1)
ref=year2;
dd1=0;
dd1=func1(mon1);
for(i=ref;i<year1;i++)
{ if(i%4==0)
dd1+=1;
}
dd1=dd1+day1+(year1-ref)*365;
printf("\n No of days of first date from the jan 1 %d=%d",ref,dd1);
/* count for additional days due to leap year*/
dd2=0;
for(i=ref; i<year2;i++)
{ if(i%4==0)
dd2+=1;
}
dd2=func1(mon2)+day2+dd2+((year2-ref)*365);
printf("\n no of days of second date from jan 1 %d=%d",ref,dd2);
printf("\ntherefore,difference between two dates is %d",abs(dd2-dd1));
getch();
return(0);
}
int func1(int x)
{int y=0;
switch(x)
{ case 1 :y=0;
break;
case 2 :y=31;
break;
case 3 : y=59;
break;
case 4 : y=90;
break;
case 5: y= 120;
break;
case 6: y=151;
break;
case 7: y=181;
break;
case 8: y= 212;
break;
case 9: y=243;
break;
case 10: y=273;
break;
case 11: y=304;
break;
case 12: y=334 ;
break;
default : printf("Error encountered");
}
return(y);
}

OUTPUT:
Session 5
Program
#include<stdio.h>
#include <conio.h>
#include<stdio.h>
int main()
{
float array_of_coefficient[20],sum_of_polynomial=0,xvalue;
int i,n;
printf("\nHighest degree of polynomial'a to solve the value should be less than 20");
scanf("%d",&n);
printf("\n Each coefficients of polynomial one by one\n");
for(i=n;i>=0;i--)
{
scanf("%f",&array_of_coefficient[i]);
}
printf("\n Enter the value of x of polynomial:");
scanf("%f",&xvalue);
for(i=n;i>0;i--)
{
sum_of_polynomial=(sum_of_polynomial+array_of_coefficient[i])*xvalue;
}
sum_of_polynomial=sum_of_polynomial+array_of_coefficient[0];
printf("Evaluated value of whole expression is =%f",sum_of_polynomial);
return(0);
}

OUTPUT:

Session 6
Program 1: Simpson for (sqrt sin(x))
#include<stdio.h>
#include<conio.h>
#include<math.h>
float f();
int main()
{
float a,b,h,x,sum,sum1,sum2,simp;
int n,i,n1,n2;
FILE*fp;
fp=fopen("result","w");
printf("Input limits a&b and half the no.of");
printf("subintervals n\n");
scanf("%f %f %d",&a,&b,&n);
fprintf(fp,"The limits are a=%f,b=%f\n",a,b);
h=(b-a)/(2.0*n);
sum=f(a)+f(b);
sum1=0.0;
n1=2*n-1;
for(i=1;i<=n1;i=i+2)
{
x=a+i*h;
sum1=sum1+f(x);
}
n2=2*n-2;
sum2=0.0;
for(i=2;i<=n2;i=i+2)
{
x=a+i*h;
sum2=sum2+f(x);
}
simp=h*(sum+4.0*sum1+2.0*sum2)/3.0;
fprintf(fp,"Value of integral with");
fprintf(fp,"%d Subintervals = %14.6e\n",2*n,simp);
printf("\nPLEASE SEE FILE 'result' FOR RESULTS\n\n");
return 0;

}
float f(x)
float x;
{
float fun;
fun=(sqrt(sin(x))));
return(fun);
}
OUTPUT:

Program 2:Trapezoidal for (sqrt sin(x))


#include<stdio.h>
#include<conio.h>
#include<math.h>
float f();
main()
{
float a,b,h,x,sum,trap;
int n,i,m;
FILE*fp;
fp=fopen("result","w");
printf("Input limits a&b and no.of subintervals n\n");
scanf("%f %f %d",&a,&b,&n);
fprintf(fp,"The limits are a=%f,b=%f\n",a,b);
h=(b-a)/n;
sum=0.0;
m=n-1;
for(i=1;i<=m;i++)
{
x=a+i*h;
sum=sum+f(x);
}
trap=h*(f(a)+2.0*sum+f(b))/2.0;
fprintf(fp,"Value of integral with %d Subintervals = %14.6e\n",n,trap);
printf("\nPLEASE SEE FILE 'result' FOR RESULTS\n\n");
return 0;

float f(x)
float x;
{
float fun;
fun=(sqrt(sin(x)));
return(fun);
}

OUTPUT:

Simpson rule
#include<stdio.h>
#include<conio.h>
#include<math.h>
float f();
int main()
{
float a,b,h,x,sum,sum1,sum2,simp;
int n,i,n1,n2;
FILE*fp;
fp=fopen("result","w");
printf("Input limits a&b and half the no.of");
printf("subintervals n\n");
scanf("%f %f %d",&a,&b,&n);
fprintf(fp,"The limits are a=%f,b=%f\n",a,b);
h=(b-a)/(2.0*n);
sum=f(a)+f(b);
sum1=0.0;
n1=2*n-1;
for(i=1;i<=n1;i=i+2)
{
x=a+i*h;
sum1=sum1+f(x);
}
n2=2*n-2;
sum2=0.0;
for(i=2;i<=n2;i=i+2)
{
x=a+i*h;
sum2=sum2+f(x);
}
simp=h*(sum+4.0*sum1+2.0*sum2)/3.0;
fprintf(fp,"Value of integral with");
fprintf(fp,"%d Subintervals = %14.6e\n",2*n,simp);
printf("\nPLEASE SEE FILE 'result' FOR RESULTS\n\n");
return 0;

}
float f(x)
float x;
{
float fun;
fun=1/(sqrt (1-(x*x)));
return(fun);
}

OUTPUT:

Trapezoidal rule
#include<stdio.h>
#include<conio.h>
#include<math.h>
float f();
main()
{
float a,b,h,x,sum,trap;
int n,i,m;
FILE*fp;
fp=fopen("result","w");
printf("Input limits a&b and no.of subintervals n\n");
scanf("%f %f %d",&a,&b,&n);
fprintf(fp,"The limits are a=%f,b=%f\n",a,b);
h=(b-a)/n;
sum=0.0;
m=n-1;
for(i=1;i<=m;i=i++)
{
x=a+i*h;
sum=sum+f(x);
}
trap=h*(f(a)+2.0*sum+f(b))/2.0;
fprintf(fp,"Value of integral with %d Subintervals = %14.6e\n",n,trap);
return 0;

}
//********************************************************/

float f(x)
float x;
{
float fun;
fun=1/(sqrt(1-(x*x)));
return(fun);
}

OUTPUT:
Session 7
Program 1(a)
/*PROGRAM BISECTION.*/
#include<stdio.h>
#include<conio.h>
#include<math.h>
float f();
int main()
{
float a,b,x,eps,fa,fx,ff,s;
int il=1;
int i,n;
FILE*fp;
fp=fopen("result","w");
printf("please input end points of interval(a,b),\n");
printf("in which the root lies\n");
printf("n: number of iterations\n");
printf("eps:error tolerance\n");
scanf("%f %f %d %E",&a,&b,&n,&eps);
fprintf(fp,"A=%f,B=%f,N=%d,",a,b,n);
fprintf(fp,"EPS=%e\n",eps);
/*Compute the bisection point of a and b*/
x=(a+b)/2.0;
for(i=1;i<=n;i++)
{
fa=f(a);
fx=f(x);
if(fabs(fx)<=eps)
{
il=0;
break;
}
ff=fa*fx;
if(ff<0.0)
x=(a+x)/2.0;
else
{
a=x;
x=(x+b)/2.0;
}
}
if(il==1)
printf("No.of iterations not sufficient\n");
else
{
fprintf(fp,"ITERATIONS=%d,",i);
fprintf(fp,"ROOT=%10.7f,F(X)=%E\n",x,fx);
printf("\nPLEASE SEE FILE'result'FOR RESULTS\n\n");
fclose(fp);
return 0;
}
}
float f(x)
float x;
{
float fun;
fun=cos(x)-x*exp(x);
return(fun);
}

OUTPUT:
Program 1(b)
#include<stdio.h>
#include<math.h>
#include<conio.h>
#include<stdlib.h>
int main()
{ doublefx(double x); //decleration of function
doublex,a,b,fa,fb,c,fc,err;
intn,i;
clrscr();
printf("\n BISECTION METHOD FIND ROOT OF AN EQUATION");
printf("\n f(x)=x*log10-1.2");
printf("\n Enter an interval[a,b] in which root is to be found");
printf("\na=");
scanf("%lf",&a);
printf("b=");
scanf("%lf",&b);
printf("\n Enter the value of permissible error=");
scanf("%lf",err);
n=(log10(abs(b-a))-log10(err))/log10(2);
n=n+1;
i=0;
getch();
printf("\n Number of iterations for this error bound are =%d",n);
printf("\n press any key for step by step display of intrervals");
printf("\n RESULT OF BISECTION METHOD \n");
while(n-->0)
{
fa= fx(a);
fb=fx(b);
c=(a+b)/2;
fc=fx(c);
i++;
printf("\n %d a=%lf b=%lf c=%lf",i,a,b,c);
printf("\n f(a)=%lf f(b)=%lf f(c)=%lf",fa,fb,fc);
if((fc*fa)<0)
b=c;
if((fc*fb)<0)
a=c;
printf("\n interval :a=%lf b=%lf ",a,b);
getch();
}
x=a+(b-a)/2;
printf("\n The valueof root is=%20.15lf",x);
getch();
return(0);
}

doublefx(double x)
{
double f;
f=x*log10(x)-1.2;
return(f);
}

OUTPUT:
Program 2
/* REGULA-FALSI*/
#include<stdio.h>
#include<conio.h>
#include<math.h>
float f();
main()
{
float a,b,x,eps,fa,fb,fx;
int il=1;
int i,n;
FILE *fp;
fp=fopen("result","w");
printf("Input the end points of the interval(a,b)in");
printf("Which the root lies");
printf("\n:number of iterations\n");
printf("eps:error tolerance\n");
scanf("%f %f %d %E",&a,&b,&n,&eps);
fprintf(fp,"a=%f b=%f n=%d",a,b,n);
fprintf(fp,"eps=%e\n\n",eps);
/*compute the value of f(x)at a&b and calculate the new
approximation x and value of f(x) at x.*/
for(i=1;i<=n;i++)
{
fa=f(a);
fb=f(b);
x=a-(a-b)*fa/(fa-fb);
fx=f(x);
if(fabs(fx)<=eps)
/*Iteration is stopped when abs(f(x))is less than or equal to eps.
Alternate conditions can also be used.*/
{
il=1;
break;
}
if((fa*fx)<0.0)
b=x;
else
a=x;
}
if(il==0)
{
printf("\nITERATIONS ARE NOT SUFFICIENT");
return 0;
}
fprintf(fp,"Number of iterations=%d\n",i);
fprintf(fp,"ROOTS=%10.7f,f(x)=%e\n",x,fx);
printf("\nPLEASE SEE FILE 'result' FOR RESULTS\n\n");
return 0;
}
/**********************************************/
float f(x);
float x;
{
float fun;
fun=cos(x)-x*exp(x);
return(fun);
}
}

OUTPUT:
Session 8
/*RUNGE-KUTTA CLASSICAL FOURTH
ORDER METHOD*/
#include<stdio.h>
#include<conio.h>
#include<math.h>
float f();
int main()
{
float u[20],v[40],x0,y0,h,k1,k2,k3,k4;
float h1,v1,te,x1,u1;
int n,i,m,nn,ij;
FILE*fp;
fp=fopen("result","w");
printf("Input initial point x0,initial value y0,\n");
printf("number of intervals n and step size h\n");
scanf("%f %f %d %f",&x0,&y0,&n,&h);
fprintf(fp,"Initial point x0=%f,initial",x0);
fprintf(fp,"initial value y0=%f\n",y0);
fprintf(fp,"Number of intervals=%d,\n",n);
x1=x0;
for(m=1;m<=2;m++)
{
if(m==1)
{
nn=n;
u[0]=y0;
}
else
{nn=2*n;
h=h/2.0;
v[0]=y0;
}
for(i=1;i<=nn;i++)
{
if(m==1)
{
u1=u[i-1];
h1=h/2.0;
k1=h*f(x0,u1);
k2=h*f(x0+h1,u1+0.5*k1);
k3=h*f(x0+h1,u1+0.5*k2);
k4=h*f(x0+h,u1+k3);
u[i]=u1+(k1+2.0*(k2+k3)+k4)/6.0;
x0=x0+h;
}
else
{
v1=v[i-1];
h1=h/2.0;
k1=h*f(x1,v1);
k2=h*f(x1+h1,v1+0.5*k1);
k3=h*f(x1+h1,v1+0.5*k2);
k4=h*f(x1+h,v1+k3);
v[i] = v1+(k1+2.0*(k2+k3)+k4)/6.0;
x1=x1+h;
}
}
}
te=16.0*(v[nn]-u[n])/15.0;
fprintf(fp,"Step=%4.2f\n",2.0*h);
fprintf(fp,"Solution at nodal points\n");
for(i=1;i<=n;i++)
fprintf(fp,"%11.7f",u[i]);
fprintf(fp,"\n");
fprintf(fp,"Step=%4.2f\n",h);
fprintf(fp,"Solution at nodal points\n");
for(i=1;i<=2*n;i++)
{
if(i==n+1)
fprintf(fp,"\n");
fprintf(fp,"Estimate of truncation error at");
fprintf(fp,"xf=%12.5e\n",te);
printf("\nPLEASE SEE FILE 'result'FOR RESULT\n\n");
return 0;
}
//***********************************************/
float f(x,y)
float x,y;
{
float fun;
fun=-2.0*x*y*y;
return(fun);
}
//****************************************************/

OUTPUT:
Session 9.
Program 1
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define amdollar 50
#define amyen 40
#define ameuro 80

int main()
{
int d,u,y;
int ch1,ch;
int r;
d=u=y=r=0;
printf("main menu\n");
printf("1.rupee to other\n");
printf("2.other to rupee\n");
printf("enter choice\n");
scanf("%d",&ch1);
switch(ch1)
{
case 1:
{
printf("main menu\n");
printf("1.rupee to dollar\n");
printf("2.rupee to euro\n");
printf("3.rupee to yen\n");
printf("enter choice\n");
scanf("%d",&ch);
switch(ch)
{
case 1:{
printf("enter rupees\n");
scanf("%d",&r);
d=r/amdollar;
printf("\ndollars=");
printf("\n%d",d);
break;
}
case 2:{
printf("enter rupees\n");
scanf("%d",&r);
u=r/ameuro;
printf("\neuro=");
printf("\n%d",u);
break;
}
case 3:{
printf("enter rupees\n");
scanf("%d",&r);
y=r/amyen;
printf("\nyen=");
printf("\n%d",y);
break;
}
default:break;
}
break;
}
case 2:{
{
printf("main menu\n");
printf("1.dollar to rupee\n");
printf("2.euro to rupee\n");
printf("3.yen to rupee\n");
printf("enter choice\n");
scanf("%d",&ch);
switch(ch)
{
case 1:{
printf("enter dollars\n");
scanf("%d",&d);
r=d*amdollar;
printf("\nrupees=");
printf("%d",r);
break;
}
case 2:{
printf("enter euro\n");
scanf("%d",&u);
r=u*ameuro;
printf("\nrupees=");
printf("%d",r);
break;
}
case 3:{
printf("enter yen\n");
scanf("%d",&y);
r=y*amyen;
printf("\nrupees=");
printf("%d",r);
break;
}
default:break;
}
break;
}
}
default:
printf("wrong choice\n");
getch();
}
}
Session 11
Program 1
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
void main()
{
float *x,*y,n,i,j,x1=0,x12=0,y1=0,x1y1=0,a1,a0,e;
puts(“_**************************************************_”);
printf("How many values You are Entering");
scanf("%f",&n);
puts(“_****************************************************_”);
x=(float*)malloc(n*4);
y=(float*)malloc(n*4);
puts();
puts("Enter corresponding Elements X &Y");
for(i=0;j<n;i++);
{
scanf("%f%f", &x[i],&y[i]);
x1+=x[i];
y1+=y[i];
x1y1+=(x[i]*y[i]);
x12+=(x[i]*x[i]);
a1=(n*x1y1-x1*y1)/(n*x12-x1*x1);
a0=y1/n-a1*x1/n;
e=y[0]-a0-a1*x[0];
a0+=e;
puts(“_****************************************************_”);
printf("\nY=%f+%fX",a0,a1);
puts(“_***************************************************_”);
}
}

OUTPUT:
Session 12
#include<stdio.h>
#include<math.h>
main()
{
float a[15][15],ai[15][15],b[15],x[15];
float aa[15][30],big,ab,t,p,sum;
int n,m,m2,i,j,lj,k,kpl,jj,lk,li,l3;
FILE *fp;
fp=fopen("result","w");
printf("Input order of matrix:\n");
scanf("%d", &n);
printf("Input augmented matrix row-wise\n");
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
scanf("%f", &a[i][j]);
scanf("%f",&b[i]);
}
fprintf(fp,"Order of the system=%d\n",n);
fprintf(fp,"Elements of the augmented matrix:\n");
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
fprintf(fp,"%8.4f",a[i][j]);
fprintf(fp,"%8.4f\n",b[i]);
}
m=n+n;
m2=n+1;
/*Gnerate the augmented matrix aa.*/
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
aa[i][j]=a[i][j];
}
for(i=1;i<=n;i++)
{
for(j=m2;j<=m;j++)
aa[i][j]=0.0;
}
for(i=1;i<=n;i++)
{
j=i+n;
aa[i][j]=1.0;
}
/* GEnerate elemnts of b matrix.*/
for(lj=1;lj<=n;lj++)
{
/*Search otr the largest pivot.*/
k=lj;
if(k<n)
{
jj=k;
big=fabs(aa[k][k]);
kpl=k+1;
for(i=kpl;i<=n;i++)
{
ab=fabs(aa[i][k]);
if((big-ab)<0.0)
{
big=ab;
jj=i;
}
}
/* Interchange of rows if required.*/
if((jj-k) !=0)
{
for(j=k;j<=m;j++)
{
t=aa[jj][j];
aa[jj][j]=aa[k][j];
aa[k][j]=t;
}
}
}
p=aa[lj][lj];
for(i=lj;i<=m;i++)
aa[lj][i]=aa[lj][i]/p;
for(lk=1;lk<=n;lk++)
{
t=aa[lk][lj];
for(li=lj;li<=m;li++)
{
if((lk-lj)!=0)
aa[lk][li]=aa[lk][li]-aa[lj][li]*t;
}
}
}
for(i=1;i<=n;i++)
{
for(j=m2;j<=m;j++)
{
l3=j-n;
a[i][13]=aa[i][j];
}
}
fprintf(fp,"\n Inverse Matrix\n");
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
fprintf(fp,"%11.5f",ai[i][j]);
fprintf(fp,"\n");
}
for(i= 1;i<=n;i++)
{
sum=0.0;
for(k=1;k<=n;k++)
sum=sum=ai[i][k]*b[k];
x[i]=sum;
}
fprintf(fp,"\n SOLUTION VECTOR\n");
for(i=1;i<=n;i++)
fprintf(fp,"%11.5f",x[i]);
fprintf(fp,"\n");
printf("\nPLEASE SEE FILE 'result'FOR RESULTS\n\n");
fclose(fp);
return 0;
}

OUTPUT:
Session 13
#include<stdio.h>
#include<math.h>
#include<conio.h>
#define maximum 10
#define minvalue 0.0005
int main()
{
float augmentedmatrix[maximum][2*maximum];
/* 2D array declared to store augmented matrix */
float temporary, r ;

int i, j, k, dimension, temp;


/* declaring counter variables for loops */
printf("\nINVERSE OF NON-SINGULAR MATRIX BY GAUSS-JORDAN ELIMINATION METHOD");
printf("\n Enter the dimension of the matrix to be provided as input : \n");
scanf("%d",&dimension);
/*storing augmented matrix as a matrix of dimension(dimension)x(2*dimension) in 2D array */
printf("\n Enter a non-singular %dx%d matrix : \n",dimension,dimension);
for(i=0; i<dimension; i++)
for(j=0; j<dimension; j++)
scanf(" %f",&augmentedmatrix[i][j]) ;
/* augmenting with identity matrix of similar dimensions */
for(i=0;i<dimension; i++)
for(j=dimension; j<2*dimension; j++)
if(i==j%dimension)
augmentedmatrix[i][j]=1;
else
augmentedmatrix[i][j]=0;
/* using gauss-jordan elimination */
for(j=0; j<dimension; j++)
{
temp=j;
/* finding maximum jthcolumn element in last (dimension-j) rows */
for(i=j+1; i<dimension; i++)
if(augmentedmatrix[i][j]>augmentedmatrix[temp][j])
temp=i;
if(fabs(augmentedmatrix[temp][j])<minvalue)
{
printf("\n Elements are too small to deal with !!!");
goto end;
}
/* swapping row which has maximum jthcolumn element */
if(temp!=j)
for(k=0; k<2*dimension; k++)
{
temporary=augmentedmatrix[j][k] ;
augmentedmatrix[j][k]=augmentedmatrix[temp][k] ;
augmentedmatrix[temp][k]=temporary ;
}
/* performing row operations to form required identity matrix out of the input matrix */
for(i=0; i<dimension; i++)
if(i!=j)
{
r=augmentedmatrix[i][j];
for(k=0; k<2*dimension; k++)
augmentedmatrix[i][k]-=(augmentedmatrix[j][k]/augmentedmatrix[j][j])*r ;
}
else
{
r=augmentedmatrix[i][j];
for(k=0; k<2*dimension; k++)
augmentedmatrix[i][k]/=r ;
}
}
/* Display augmented matrix */
printf("\n After Gauss-Jordan elimination, augmented matrix is : \n\n") ;
for(i=0; i<dimension; i++)
{
for(j=0; j<2*dimension; j++)
printf(" % 4.2f",augmentedmatrix[i][j]) ;
printf("\n");
}
/* displaying inverse of the non-singular matrix */
printf("\n\n\n The inverse of the entered non-singular matrix is : \n\n");
for(i=0; i<dimension; i++)
{
for(j=dimension; j<2*dimension; j++)
printf(" % .5f",augmentedmatrix[i][j]);
printf("\n");
}
end: getch() ;
}

OUTPUT:
Session 14
#include <stdio.h>
#include <conio.h>
typedef struct
{
int num,den;
}
RATIONAL;
RATIONAL add(RATIONAL,RATIONAL);
RATIONAL mult(RATIONAL,RATIONAL);
RATIONAL reduce(RATIONAL);
int equal(RATIONAL,RATIONAL);

int main()
{
RATIONAL A,B,C,D;
int f;
printf("\nEnter the rational number A with numerator and denominator");
scanf("%d%d",&A.num,&A.den);
printf("\nEnter the Rational number B with numerator and denominator");
scanf("%d%d",&B.num,B.den);
C=add(A,B);
printf("\nThe sum of two rational numbers is %d%d",C.num,C.den);
D=mult(A,B);
printf("\nThe product of two rational numbers is %d%d", D.num,D.den);
f=equal(A,B);
if(f==0)
printf("\nGiven two numbers are equal.");
else
printf("\nGiven two numbers are unequal.");
getch();
}
RATIONAL add(RATIONAL A,RATIONAL B)
{
RATIONAL C;
C.num=A.num*B.den+A.den*B.num;
C.den=A.den*B.den;
C=reduce(C);
return(C);
}
RATIONAL mult(RATIONAL A,RATIONAL B)
{
RATIONAL C;
C.num=A.num*B.num;
C.den=A.den*B.den;
C=reduce(C);
return(C);
}

int equal(RATIONAL A,RATIONAL B)


{
A=reduce(A);
B=reduce(B);
if(A.num==B.num&&A.den==B.den)
return(0);
else
return(1);
}
RATIONAL reduce(RATIONAL A)
{
int a,b,c;
if(A.num=A.den)
{
A.num=1;
A.den=1;
return(A);
}
if(A.num>A.den)
{
b=A.num;
a=A.den;
}
if(A.den>A.num)
{
b=A.den;
a=A.num;
}
while(a>0)
{
c=b%a;
b=a;
a=c;
}
A.num=A.num/b;
A.den=A.den/b;
return(A);
}
Session 18
Program 1
#include<stdio.h>
#include<string.h>
#define max 100
int top,stack[max];
void push(char x)
{
//Push (inserting Element in stack) operation
if(top==max-1)
{
printf("Stack overflow");
}
else
{
stack[++top]=x;
}
}
void pop()
{
//Pop(removing element from stack)
printf("%c",stack[top--]);
}
int main()
{
char str[]="I Love Programming\n";
int len=strlen(str),i;
for (i=0;i<len;i++)
push(str[i]);
for(i=0;i<len;i++)
pop();
return 0;
}
Program 2
#include<stdio.h>
#include<conio.h>
using namespace std;
struct node
{
char data;
node *next;
}*p=NULL, *top=NULL, *save+NULL, *ptr;
void push(char x)
{
p=new node;
p->data=x;
p->next=NULL;
if(top==NULL)
{
top=p;
}
else
{
save=top;
top=p;
p->next=save;
}
}
char pop()
{
if(top==NULL)
{
printf("underflow!!");
}
else
{
ptr=top;
top=top->next;
return(ptr->data);
deleteptr;
}
}
int main()
{
int i;
char c[30],a,y,z;
printf("Enter the expression:\n");
fgets(c);
for(i=0;i<sterlen(c);i++)
{
if((c[i]=='(')||(c[i]=='{')||(c[i]=='['))
{
push(c[i]);
}
else
{
switch(c[i])
{
case ')':
a=pop();
if((a=='{')||(a=='['))
{
printf("invalid expr!!");
getch();
}
break;
case '}':
y=pop();
if((y=='{')||(y=='['))
{
printf("invalid expr!!");
getch();
}
break;
case ']':
z=pop();
if((z=='{')||(z=='['))
{
printf("invalid expr!!");
getch();
}
break;
}
}
}
if(top==NULL)
{
printf("balanced expr!!");
}
else
{
printf("string is not valid.!!");
}
getch();
}

OUTPUT:

Enter the expression:


[{{{}}}{{()[[]]}}]
Balanced expression!!
Session 19
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
#include<stdlib.h>
struct node
{
int data;
struct node *link;
}
struct node *cur,*first,*last;

void create();
void insert();
void delte();
void display();

void create()
{
printf("\ENTER THE FIRST ELEMENT:");
cur=(struct node *)malloc(sizeof(struct node));
scanf("%d",&cur->data);
cur->link=NULL;
first=cur;
last=cur;
}
void insert()
{
printf("\nENTER THE NEXT ELEMENT:");
cur=(struct node*)malloc(sizeof(struct node));
scanf("%d",&cur->data);
cur->link=NULL;
last->link=cur;
last=cur;
}
void delte()
{
if(first==NULL)
{
printf("\t\nQUEUE IS EMPTY\n");
}
else
{
cur=first;
first=first->link;
cur->link=NULL;
printf("\nDELETED ELEMENT IS: %d\n",cur->data);
free(cur);
}
}
void display()
{
cur=first;
printf("\n");
while(cur!=NULL)
{
printf("\t%d",cur->data);
cur=cur->link;
}
}
void displaytop()
{
cur=first;
printf("\n");
if(cur!=NULL)
{
printf("\t%d",cur->data);
}
}
void diplaybottom()
{
cur=last;
printf("\n");
if(cur!=NULL)
{
printf("\t%d",cur->data);
}
}

void main()
{
int ch;
clrscr();
while(1)
{
printf("\n\n 1.CREATE \n 2.INSERT \n 3.DELETE \n 4.TOP ELEMENT \n 5.BOTTOM ELEMENT \n
6.EXIT \n");
scanf("%d",&ch);
switch(ch)
{
case 1:
create();
display();
break;

case 2:
insert();
display();
break;

case 3:
delete();
display();
break;

case 4:
displaytop();
break;

case 5:
displaybottom();
break;
case 6:
exit(0);
}
}
}

OUTPUT:
Session 20
#include <stdio.h>
#include <stdlib.h>
struct node
{
int value;
node* left;
node* right;
};
struct node* root;
struct node* insert(struct node* r, int data);
void inOrder(struct node* r);
void preOrder(struct node* r);
void postOrder(struct node* r);
int main()
{
root = NULL;
int n, v;
printf("How many data's do you want to insert ?\n");
scanf("%d", &n);
for(int i=0; i<n; i++)
{
printf("Data %d: ", i+1);
scanf("%d", &v);
root = insert(root, v);
}
printf("Inorder Traversal: ");
inOrder(root);
printf("\n");
printf("Preorder Traversal: ");
preOrder(root);
printf("\n");
printf("Postorder Traversal: ");
postOrder(root);
printf("\n");
return 0;
}
struct node* insert(struct node* r, int data)
{
if(r==NULL)
{
r = (struct node*) malloc(sizeof(struct node));
r->value = data;
r->left = NULL;
r->right = NULL;
}
else if(data < r->value)
{
r->left = insert(r->left, data);
}
else
{
r->right = insert(r->right, data);
}
return r;
}
void inOrder(struct node* r)
{
if(r!=NULL)
{
inOrder(r->left);
printf("%d ", r->value);
inOrder(r->right);
}
}
void preOrder(struct node* r)
{
if(r!=NULL)
{
printf("%d ", r->value);
preOrder(r->left);
preOrder(r->right);
}
}
void postOrder(struct node* r)
{
if(r!=NULL)
{
postOrder(r->left);
postOrder(r->right);
printf("%d ", r->value);
}
}
OUTPUT:

You might also like