Cprogramming For 5th Sem Mech
Cprogramming For 5th Sem Mech
#include <stdio.h>
#include <conio.h>
void main()
{
float a, b, c, sum, avg;
printf("Enter value of three numbers\n ");
scanf("%f %f %f", &a, &b, &c);
sum = a+b+c;
avg = sum/3;
printf("Sum = %f\n", sum);
printf("Average = %f", avg);
getch();
}
Output:
Enter the value of three numbers
4
5
6
Sum=15.00000
Average=5.00000
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
printf("Enter the value of a,b,c\n");
scanf("%d%d%d",&a,&b,&c);
if(a>b&&a>c)
printf("largest number is %d",a);
if(b>a&&b>c)
printf("largest number is %d",b);
if(c>a&&c>b)
printf("largest number is %d",c);
getch();
}
output
Enter the value of a,b,c
8 9 22
largest number is 22
#include<conio.h>
void main()
{
int n,rem;
clrscr();
printf("enter the number\n");
scanf("%d",&n);
rem=n%2;
if(rem==0)
printf("The number is even\n");
else
printf("The number is odd\n");
getch();
}
Output 1:
enter the number
3
The number is odd
Output 2:
enter the number
8
The number is even
void main()
{
int n,i,rem;
clrscr();
printf("Enter the number\n");
scanf("%d",&n);
for(i=2;i<n;i++)
{
rem=n%i;
if(rem==0)
break;
}
if(rem==0)
printf("The number is not prime\n");
else
printf("The number is prime\n");
getch();
}
Output 1:
Enter the number
4
The number is not prime
Output 2:
Enter the number
5
The number is prime
Output 3:
Enter the number
9
The number is not prime
Output 1:
Enter the value of a,b,c
4 5 1
dis=9.000000
Real & Distinct roots are
root1=-0.250000
root2=-1.000000
Output 2:
int a,b,c,x;
float dis,root1,root2;
printf("Enter the value of a,b,c\n");
scanf("%d%d%d",&a,&b,&c);
dis=(b*b)-(4*a*c);
printf("dis=%f\n",dis);
if(dis>0)
x=1;
if(dis==0)
x=2;
if(dis<0)
x=3;
switch(x)
{
case 1:
printf("Real & Distinct roots are\n");
root1=(-b+sqrt(dis))/(2*a);
root2=(-b-sqrt(dis))/(2*a);
printf("root1=%f\n root2=%f", root1, root2);
break;
case 2:
printf("real and equal\n");
root1=root2=-b/(2*a);
printf("root1=%f\n root2=%f", root1, root2);
break;
case 3:
printf("roots are imaginary no real solution\n");
break;
}
getch();
}
scanf("%d",&num);
while(num>0)
{
rem=num%10;
sum=sum+rem;
num=num/10;
}
printf("sum of entered number is %d",sum);
getch();
}
Output 1:
Enter the number
456
sum of entered number is 15
output 2:
Enter the number
2123
sum of entered number is 8
/* to
Change
if(a[j]<a[j+1]) to
if(a[j]>a[j+1])
For substraction
Change
c[i][j]=a[i][j]+b[i][j];
To
c[i][j]=a[i][j] - b[i][j];
for(j=0;j<3;j++)
scanf("%d",&b[i][j]);
for(i=0;i<3;i++)
for(j=0;j<3;j++)
c[i][j]=a[i][j]+b[i][j];
printf("sum of two matrix is\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
printf("%d ",c[i][j]);
printf("\n");
}
getch();
}
Output :
Enter the first matrix
121
221
351
Enter the second matrix
111
211
345
sum of two matrix is
232
432
696
#include<stdio.h>
#include<conio.h>
int swap(int a, int b);
void main( )
{
int a,b;
clrscr();
printf("enter the value of a and b\n");
scanf("%d%d",&a,&b);
printf("a=%d\nb=%d\n",a,b);
swap(a,b);
getch();
}
int swap( int x, int y )
{
int t ;
t=x;
x=y;
y=t;
printf ( "a=%d\nb=%d",x,y) ;
}
Output :
enter the value of a and b
10 5
a=10
b=5
a=5
b=10