0% found this document useful (0 votes)
101 views9 pages

Cprogramming For 5th Sem Mech

The document contains code snippets for various C programs that demonstrate basic programming concepts like finding the sum and average of numbers, determining the largest of three numbers, checking if a number is even or odd, checking if a number is prime, finding the roots of a quadratic equation, finding the sum of digits of a number, sorting an array in ascending/descending order using bubble sort, adding/subtracting matrices, and swapping two numbers using a user defined function.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
101 views9 pages

Cprogramming For 5th Sem Mech

The document contains code snippets for various C programs that demonstrate basic programming concepts like finding the sum and average of numbers, determining the largest of three numbers, checking if a number is even or odd, checking if a number is prime, finding the roots of a quadratic equation, finding the sum of digits of a number, sorting an array in ascending/descending order using bubble sort, adding/subtracting matrices, and swapping two numbers using a user defined function.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

/*Program to Find Sum and Average of Three RealNumbers */

#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

/* To find Largest of 3 numbers using if statements */

#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

/* To find even or odd number using if-else*/


#include<stdio.h>

#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

/* To check prime number or not */


#include<stdio.h>
#include<conio.h>

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:

Enter the value of a,b,c


/* Program to find roots of quadratic equation
1 2 1 */
dis=0.000000
#include<stdio.h>
real and equal
#include<conio.h>
root1=-1.000000
#include<math.h>
root2=-1.000000
void main()
Output 3:
{
Enter the value of a,b,c
5 2 3
dis=-56.000000
roots are imaginary no
real solution

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();
}

/* to find sum of the digits of a given number */


#include<stdio.h>
#include<conio.h>
void main()
{
int num,rem,sum=0;
clrscr();
printf("Enter the number\n");

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

arrange n-numbers in ascending/descending using

Bubble sort technique */


#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,temp,a[50],j;
To arrange in
clrscr();
printf("enter the number of elements\n");
ascending order
scanf("%d",&n);

Change
if(a[j]<a[j+1]) to
if(a[j]>a[j+1])

printf("enter the array\n");


for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n-1;i++)
{
for(j=0;j<n-1;j++)
{
if(a[j]<a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
printf("number in descending order is\n");
for(i=0;i<n;i++)
printf("%d ",a[i]);
getch();
}
Output:
enter the number of elements
5
enter the array
3 5 10 12 1
number in descending order is
12 10 5 3 1
/* to find the addition/subtraction of two matrices*/
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10][10],b[10][10],c[10][10],i,j;
clrscr();
printf("Enter the first matrix\n");
for(i=0;i<3;i++)
for(j=0;j<3;j++)
scanf("%d",&a[i][j]);
printf("Enter the second matrix\n");
for(i=0;i<3;i++)

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

/* To swap two numbers using user defined


functions. */

#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

You might also like