Mech nacp lab

19
Shri Rawatpura Sarkar Institute of Technology-II New Raipur (C.G.) Experiment No. 1 Aim:-Write a C-Program to calculate the area & Perimeter of the rectangle and the area & circumference of the circle. The length and breadth of a rectangle and radius of a circle are input through keyboard. /* Program for finding area & circumference */ #include<stdio.h> #include<conio.h> #define pi 3.14 int main() { float r,l,b,area,perimeter,circum,areac; printf("enter the radius of circle\tlength breadth of rectangle\n"); scanf("%f %f %f",&r,&l,&b); circum=2*pi*r; areac=pi*r*r; area=l*b; perimeter=2*l+2*b; printf("Area and circumference of circle is =%f\t%f\n\n\n",areac,circum); printf("Area and Perimeter of rectangle is =%f\t%f",area,perimeter); getch(); } 1 Department of Mechanical Engineering – 4 th Sem. Prepare by Ms Swagita Dwivedi

Transcript of Mech nacp lab

Page 1: Mech nacp lab

Shri Rawatpura Sarkar Institute of Technology-II New Raipur (C.G.)

Experiment No. 1

Aim:-Write a C-Program to calculate the area & Perimeter of the rectangle and the area & circumference of the circle. The length and breadth of a rectangle and radius of a circle are input through keyboard.

/* Program for finding area & circumference */

#include<stdio.h>

#include<conio.h>

#define pi 3.14

int main()

{

float r,l,b,area,perimeter,circum,areac;

printf("enter the radius of circle\tlength breadth of rectangle\n");

scanf("%f %f %f",&r,&l,&b);

circum=2*pi*r;

areac=pi*r*r;

area=l*b;

perimeter=2*l+2*b;

printf("Area and circumference of circle is =%f\t%f\n\n\n",areac,circum);

printf("Area and Perimeter of rectangle is =%f\t%f",area,perimeter);

getch();

}

1 Department of Mechanical Engineering – 4th Sem. Prepare by Ms Swagita Dwivedi

Page 2: Mech nacp lab

Shri Rawatpura Sarkar Institute of Technology-II New Raipur (C.G.)

/* Output */

enter the radius of circle length breadth of rectangle

5

3

4

Area and circumference of circle is =78.500000 31.400000

Area and Perimeter of rectangle is =12.000000 14.000000

2 Department of Mechanical Engineering – 4th Sem. Prepare by Ms Swagita Dwivedi

Page 3: Mech nacp lab

Shri Rawatpura Sarkar Institute of Technology-II New Raipur (C.G.)

Experiment No. 2

Aim:-Write a C-Program to determine whether the character entered through a keyboard is a capital letter ,a small case letter, a digit or a special symbol.

/* Program to whether the character entered through a keyboard is a capital letter ,a small case letter, a digit or a special symbol.*/

#include<stdio.h>

#include<conio.h>

int main()

{

char ch;

printf("Enter any character\n");

scanf("%c",&ch);

if(ch>=65 && ch<=91)

printf("Entered character is capital case letter");

else if(ch>=97 && ch<=123)

printf("Entered character is small case letter");

else if(ch>=48 && ch<=57)

printf("Entered character is Digit");

else

printf("Entered character is special symbol");

getch();

}

/* Output */

3 Department of Mechanical Engineering – 4th Sem. Prepare by Ms Swagita Dwivedi

Page 4: Mech nacp lab

Shri Rawatpura Sarkar Institute of Technology-II New Raipur (C.G.)

Enter any characterp

Entered character is small case letter

Experiment No. 3Aim:-Write a program which has the following options:

a) Factorial of a number

b) Prime or not

c) Odd or Even

/* Menu Driven Program*/

#include<stdio.h>

#include<conio.h>

int main()

{

int ch,fact=1,num,i;

printf("Enter any number\n\n");

scanf("%d",&num);

printf("\nEnter your chice from 1 to 3\n\n");

printf("Factorial of a number.............1\n\n");

printf("Prime or not......................2\n\n");

printf("Odd or even.......................3\n\n");

scanf("%d",&ch);

switch(ch)

{

case 1:

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

fact= fact*i;

printf("factorial of given number is = %d",fact);

break;

4 Department of Mechanical Engineering – 4th Sem. Prepare by Ms Swagita Dwivedi

Page 5: Mech nacp lab

Shri Rawatpura Sarkar Institute of Technology-II New Raipur (C.G.)

case 2:

for(i=2;i<num;i++)

{

if(num%i==0)

{

printf("Entered number is not prime\

n");

break;

}

}

if(num==i)

printf("number is prime\n");

break;

case 3:

if(num%2==0)

printf("Enter number is even");

else

printf("Enter number is Odd");

break;

default:

printf("Out of range");

break;

getch();

}

}

/* Output */

Enter any number

5

5 Department of Mechanical Engineering – 4th Sem. Prepare by Ms Swagita Dwivedi

Page 6: Mech nacp lab

Shri Rawatpura Sarkar Institute of Technology-II New Raipur (C.G.)

Enter your chice from 1 to 3

Factorial of a number.............1

Prime or not......................2

Odd or even.......................3

1

factorial of given number is = 120

Enter any number

5

Enter your chice from 1 to 3

Factorial of a number.............1

Prime or not......................2

Odd or even.......................3

2

number is prime

Enter any number

5

Enter your chice from 1 to 3

Factorial of a number.............1

Prime or not......................2

6 Department of Mechanical Engineering – 4th Sem. Prepare by Ms Swagita Dwivedi

Page 7: Mech nacp lab

Shri Rawatpura Sarkar Institute of Technology-II New Raipur (C.G.)

Odd or even.......................3

3

Enter number is Odd

Experiment No. 4

Aim:-Write a C-Program to sort an array using bubble sort technique.

/*Program to sort an array using bubble sort*/

#include<stdio.h>

#include<conio.h>

int main()

{

int i,j,num,a[10],temp;

printf("enter total number to store in array\n");

scanf("%d",&num);

printf("enter nummbers\n");

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

scanf("%d",&a[i]);

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

{

for(j=0;j<num-i;j++)

{

if(a[j]>a[j+1])

{

temp=a[j];

a[j]=a[j+1];

a[j+1]=temp;

}

}

}

printf("stored array using bubble sort\n ");

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

7 Department of Mechanical Engineering – 4th Sem. Prepare by Ms Swagita Dwivedi

Page 8: Mech nacp lab

Shri Rawatpura Sarkar Institute of Technology-II New Raipur (C.G.)

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

getch();

}

/* Output */

enter total number to store in array

7

enter nummbers

12

15

6

8

3

9

5

stored array using bubble sort

3 5 6 8 9 12 15

8 Department of Mechanical Engineering – 4th Sem. Prepare by Ms Swagita Dwivedi

Page 9: Mech nacp lab

Shri Rawatpura Sarkar Institute of Technology-II New Raipur (C.G.)

Experiment No. 5

Aim:-Write a program to find the roots of an equation using Newton Raphson Method.

/* Program for Newton Raphson Method*/

#include<stdio.h>

#include<conio.h>

#include<math.h>

float f(float x)

{

return x*log10(x) - 1.2;

}

float df (float x)

{

return log10(x) + 0.43429;

}

int main()

{

int itr, maxmitr;

float h, x0, x1, allerr;

printf("\nEnter x0, allowed error and maximum iterations\n");

scanf("%f %f %d", &x0, &allerr, &maxmitr);

for (itr=1; itr<=maxmitr; itr++)

{

h=f(x0)/df(x0);

x1=x0-h;

printf(" At Iteration no. %3d, x = %9.6f\n", itr, x1);

if (fabs(h) < allerr)

9 Department of Mechanical Engineering – 4th Sem. Prepare by Ms Swagita Dwivedi

Page 10: Mech nacp lab

Shri Rawatpura Sarkar Institute of Technology-II New Raipur (C.G.)

{

printf("After %3d iterations, root = %8.6f\n", itr, x1);

return 0;

}

x0=x1;

}

printf(" The required solution does not converge or iterations are insufficient\

n");

getch();

}

/* Output */

Enter x0, allowed error and maximum iterations

2

4

4

At Iteration no. 1, x = 2.813170

After 1 iterations, root = 2.813170

10 Department of Mechanical Engineering – 4th Sem. Prepare by Ms Swagita Dwivedi

Page 11: Mech nacp lab

Shri Rawatpura Sarkar Institute of Technology-II New Raipur (C.G.)

Experiment No. 6

Aim:-Write a program to find the solution of differential equation by Runge Kutta Equation.

#include<stdio.h>

#include<conio.h>

#include<math.h>

float f(float x,float y);

int main()

{

float x0,y0,m1,m2,m3,m4,m,y,x,h,xn;

printf("Enter x0,y0,xn,h:");

scanf("%f %f %f %f",&x0,&y0,&xn,&h);

x=x0;

y=y0;

printf("\n\nX\t\tY\n");

while(x<xn)

{

m1=f(x0,y0);

m2=f((x0+h/2.0),(y0+m1*h/2.0));

m3=f((x0+h/2.0),(y0+m2*h/2.0));

m4=f((x0+h),(y0+m3*h));

m=((m1+2*m2+2*m3+m4)/6);

y=y+m*h;

x=x+h;

printf("%f\t%f\n",x,y);

11 Department of Mechanical Engineering – 4th Sem. Prepare by Ms Swagita Dwivedi

Page 12: Mech nacp lab

Shri Rawatpura Sarkar Institute of Technology-II New Raipur (C.G.)

}

}

float f(float x,float y)

{

float m;

m=(x-y)/(x+y);

return m;

getch();

}

/* Output */

Enter x0,y0,xn,h:3

4

5

6

X Y

9.000000 4.909602

12 Department of Mechanical Engineering – 4th Sem. Prepare by Ms Swagita Dwivedi

Page 13: Mech nacp lab

Shri Rawatpura Sarkar Institute of Technology-II New Raipur (C.G.)

Experiment No. 7

Aim:-Write a program to practice one of the numerical Integration Method.

/* SIMPSON'S 1/3 RULE Integration method*/#include<stdio.h>

#include<conio.h>

#include<math.h>

void main()

{

float x[10],y[10],sum=0,h,temp;

int i,n,j,k=0;

float fact(int);

clrscr();

printf("\nhow many record you will be enter: ");

scanf("%d",&n);

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

{

printf("\n\nenter the value of x%d: ",i);

scanf("%f",&x[i]);

printf("\n\nenter the value of f(x%d): ",i);

scanf("%f",&y[i]);

}

h=x[1]-x[0];

n=n-1;

sum = sum + y[0];

13 Department of Mechanical Engineering – 4th Sem. Prepare by Ms Swagita Dwivedi

Page 14: Mech nacp lab

Shri Rawatpura Sarkar Institute of Technology-II New Raipur (C.G.)

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

{

if(k==0)

{

sum = sum + 4 * y[i];

k=1;

}

else

{

sum = sum + 2 * y[i];

k=0;

}

}

sum = sum + y[i];

sum = sum * (h/3);

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

getch();

}

/* Output */

how many record you will be enter: 5

enter the value of x0: 0

enter the value of f(x0): 1

enter the value of x1: 0.25

14 Department of Mechanical Engineering – 4th Sem. Prepare by Ms Swagita Dwivedi

Page 15: Mech nacp lab

Shri Rawatpura Sarkar Institute of Technology-II New Raipur (C.G.)

enter the value of f(x1): 0.8

enter the value of x2: 0.5

enter the value of f(x2): 0.6667

enter the value of x3: 0.75

enter the value of f(x3): 0.5714

enter the value of x4: 1

enter the value of f(x4): 0.5

I = 0.693250

15 Department of Mechanical Engineering – 4th Sem. Prepare by Ms Swagita Dwivedi

Page 16: Mech nacp lab

Shri Rawatpura Sarkar Institute of Technology-II New Raipur (C.G.)

16 Department of Mechanical Engineering – 4th Sem. Prepare by Ms Swagita Dwivedi