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

C Programming

The document contains various C programming code snippets for mathematical operations such as checking for prime numbers, sorting arrays, calculating Fibonacci series, and finding roots of quadratic equations. It also includes functions for matrix operations, calculating mean, median, and mode for both ungrouped and grouped data. Each code snippet is structured to prompt user input and display the results accordingly.

Uploaded by

bhagavanrt
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

C Programming

The document contains various C programming code snippets for mathematical operations such as checking for prime numbers, sorting arrays, calculating Fibonacci series, and finding roots of quadratic equations. It also includes functions for matrix operations, calculating mean, median, and mode for both ungrouped and grouped data. Each code snippet is structured to prompt user input and display the results accordingly.

Uploaded by

bhagavanrt
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

INDEX

S.No. Date Content Page No. Mark Signature

1.

2.

3.

4.

5.

6.

7.

8.

9.

10.

11.

12.

13.

14.
Check the number is prime or not
# include<stdio.h>
#include<conio.h>
Void main()
{
int n,i,flag=0;
clrscr();
printf(“Enter the Number:”);
scanf(“%d”, &n);
for(i=2;i<=n/2;++i)
{
if(n%i==0)
{
flag=1;
break;
}
}
if(flag==0)
printf(“%d is a prime number”,n);
else
printf(“%d is not a prime number”,n);
getch();
}
Sort a list of number in ascending order
# include<stdio.h>
#include<conio.h>
Void main()
{
int ar[100],i,j,n,t;
clrscr();
printf(“Enter the size of the array\n”);
scanf(“%d”, &n);
printf(“Enter the element in the array\n”);
for(i=0;i<n;i++)
{
scanf(“%d”,&ar[i]);
}
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(ar[i]>ar[j])
{
t=ar[j];
ar[j]=ar[i];
ar[i]=t;
}
}
}
printf(“Array in ascending order is \n”);
for(i=0;i<n;i++)
{
printf(“%d\t”,ar[i]);
}
getch();
}
Sort a list of number in descending order

# include<stdio.h>
#include<conio.h>
Void main( )
{
int ar[100],i,j,n,t;
clrscr();
printf(“Enter the size of the array\n”);
scanf(“%d”, &n);
printf(“Enter the element in the array\n”);
for(i=0;i<n;i++)
{
scanf(“%d”,&ar[i]);
}
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(ar[i]>ar[j])
{
t=ar[i];
ar[i]=ar[j];
ar[j]=t;
}
}
}
printf(“Array in descending order is \n”);
for(i=n-1;i>=0;i--)
{
printf(“%d\t”,ar[i]);
}
getch();
}
Print Fibonacci series using while statement

# include<stdio.h>
#include<conio.h>
Void main()
{
int n,i,a,b,c;
clrscr();
printf(“Enter the number:”);
scanf(“%d”,&n);
i=1
a=0
b=1
while(i<=n)
{
printf(“\t%d”,a);
c=a+b;
a=b;
b=c;
i++;
}
getch();
}
Calculate roots of a quadratic equation using simple if statement

# include<stdio.h>
#include<conio.h>
#include<math.h>
Void main()
{
int a,b,c,d;
float x1,x2;
clrscr();
printf(“Enter the value of a,b&c\n”);
scanf(“%d%d%d”,&a,&b,&c);
d=b*b-4*a*c;
if(d==0)
{
printf(“Both roots are equal\n”);
x1=-b/(2.0*a);
x2=x1;
printf(“First root x1=%f\n”,x1);
printf(“Second root x2=%f\n”,x2);
}
else if(d>0)
{
printf(“Both roots are real and different\n”);
x1=(-b+sqrt(d))/(2*a);
x2=(-b-sqrt(d))/(2*a);
printf(“First root x1=%f\n”,x1);
printf(“Second root x2=%f\n”,x2);
}
else
printf(Both roots are imaginary \n no solution\n”);
getch();
}
Product of two matrix
#inclde<stdio.h>
#include<conio.h>
#include<math.h>
void main ()
{
int a[10][10],b[10][10],c[10][10],i,j,k,rl,cl,r2,c2;
clrscr();
printf(“Enter the row & column of 1st matrix\n”);
scanf(“%d%d”,&r1,&c1);
printf(“Enter the row & column of 2nd matrix\n”);
scanf(“%d%d”,&r2,&c2);
if(r1==c2)
{
printf(“Enter the elements of 1st matrix\n”);
for(i=0;i<r1;i++)
for(j=0;j<c1;j++)
scanf(“%d”,&a[i][j]);
printf(“Enter the elements of 2nd matrix\n”);
for(i=0;i<r2;i++)
for(j=0;j<c2;j++)
scanf(“%d”,&b[i][j]);
printf(“The first matrix is\n”);
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
printf(“%d\t”,a[i][j]);
}
printf(“\n”);
}
printf(“The second matrix is\n”);
for(i=0;i<r2;i++)
{
for(j=0;j<c2;j++)
{
printf(“%d\t”,b[i][j]);
}
printf(“\n”);
}
printf(“multiplication of two matrix is \n”);
for(i=0;i<r1;i++)
{
for(j=0;j<c2;j++)
{
c[i][j]=0;
for(k=0;k<c1;k++)
c[i][j]=c[i][j]+a[i][k]*b[k][j];
printf(“%d\t”,c[i][j]);
}
printf(“\n\n”);
}
}
else
{
printf(“Matrix multiplication is not possible”);
}
getch();
}
Transpose of Matrix
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main ()
{
intr1,cl,i,j,a[10][10];
clrscr();
printf(“Enter the row & column of the matrix\n”);
scanf(“%d%d”,&r1,&c1);
printf(“Enter the element of the matrix\n”);
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
scanf(“%d”,&a[i][j]);
}
}
printf(“Given matrix is \n”);
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
printf(“%d\t”,a[i][j]);
}
printf(“\n”);
}
printf(“Transpose of given matrix \n”);
for(j=0;j<c1;j++)
{
for(i=0;i<r1;i++)
{
printf(“%d\t”,a[i][j]);
}
printf(“\n”);
}
getch();
}
Calculate the mean for ungrouped data

#include<stdio.h>
#include<conio.h>
void main()
{
int i;
float num,mean,n,sum=0;
clrscr();
printf(“Enter the total number of items:\n”);
scanf(“%f”,&n);
printf(“Enter the number:\n”);
for(i=0;i<n;i++)
{
scanf(“%f”,&num);
sum=sum+num;
}
mean=sum/n;
printf(“\nsum of the %.2f numbers = %.2f”,n,sum);
printf(“\nmean=%.2f”,mean);
getch();
}
Calculate the mean for grouped data (Discrete Series)
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int i,n,x[100],f[100],fx[100],sf,sfx;
float mean;
clrscr();
printf(“Enter the total number of observations:”);
scanf(“%d”,&n);
for(i=0;i<n;i++)
{
printf(“Enter the observation %d:”,i+1);
scanf(“%d”,&x[i]);
printf(“Enter the frequency for observation %d:”,i+1);
scanf(“%d”,&f[i]);
}
sf=0;
sfx=0;
for(i=0;i<n;i++)
{
sf=sf+f[i];
fx[i]=x[i]*f[i];
sfx=sfx+fx[i];
}
mean=(float)sfx/(float)sf;
printf(“\nSum of the Frequency=%d”,sf);
printf(“\nsummation fx=%d”,sfx);
printf(“\nmean=%.2f”,mean);
getch();
}
Calculate the mean for grouped data (Continuous Series)

#include<stdio.h>
#include<conio.h)
#include<math.h>
void main()
{
int i,ll,ul,nc,cl,l[100],u[100],f[100],x[100],fx[100],sf,sfx;
float mean;
clrscr();
printf(“Enter the lower limit:”);
scanf(“%d”,&ll);
printf(“Enter the class length:”);
scanf(“%d”,&cl);
printf(“Enter the total number of class:”);
scanf(“%d”,&nc);
for(i=0;i<nc;i++)
{
ul=ll+cl;
l[i]=ll;
u[i]=ul;
ll=ul;
x[i]=((float)1[i]+(float)u[i])/2;
}
for(i=0;i<nc;i++)
{
printf(“Enter the frequency for the class %d-%d:”,l[i],u[i]);
scanf(“%d”,&f[i]);
}
sf=0;
sfx=0;
for(i=0;i<nc;i++)
{
sf=sf+f[i];
fx[i]=x[i]*f[i];
sfx=sfx+fx[i];
}
mean=(float)sfx/(float)sf;
printf(“\nSum of the Frequency=%d”,sf);
printf(“\nsummation fx=%d”,sfx);
printf(“\nmean=%.2f”,mean);
getch();
}
Calculate the median for ungrouped data
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int i,j,n,a[100],t;
float median;
clrscr();
printf(“Enter the total number of observations:\n”);
scanf(“%d”,&n);
printf(“Enter the element of the observations:\n”);
for(i=0;i<n;i++)
{
scanf(“%d”,&a[i]);
}
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(a[i]>a[j])
{
t=a[j];
a[j]=a[i];
a[i]=t;
}
}
}
Printf(“Elements in ascending order is \n”);
for(i=0;i<n;i++)
{
printf(“%d\t”,a[i]);
}
If(n%2==0)
median=(float)(a[n/2]+a[(n-1)/2])/2;
else
median=a[n/2];
printf(“\nmedian is %.2f”,median);
getch();
}
Calculate the median for grouped data (Discrete Series)

#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int i,n,x[100],f[100],cf[100],sf,temp;
float median;
clrscr();
printf(“Enter the total number of observations:”);
scanf(“%d”,&n);
for(i=0;i<n;i++)
{
printf(“Enter the observation %d:”,i+1);
scanf(“%d”,&x[i]);
printf(“Enter the frequency for observation %d:”,i+1);
scanf(“%d”,&f[i]);
}
cf[0]=f[0];
sf=0;
for(i=0;i<n;i++)
{
cf[i+1]=cf[i]+f[i+1];
sf=sf+f[i];
}
printf(“\ncumulative frequency is \n”);
for(i=0;i<n;i++)
{
printf(“%d\t”,cf[i]);
}
for(i=0;i<n;i++)
{
If((sf/2)>=cf[i])
Temp=i+1;
}
printf(“\nsum of frequency = %d”,sf);
printf(“\nN/2:%.2f”,((float)sf/(float)2));
printf(“\nmedian is %d”,x[temp]);
getch();
}
Calculate the median for grouped data (Continuous Series)
#include<stdio.h>
#include<conio.h>
#include<math.h>
Void main()
{
int i,ll,ul,cl,nc,l[100],u[100],f[100],sf,cf[100],temp;
float median;
clrscr();
printf(“Enter the lower limit:”);
scanf(“%d”,&ll);
printf(“Enter the class length:”);
scanf(“%d”,&cl);
printf(“Enter the total number of class:”);
scanf(“%d”,&nc);
for(i=0;i<nc;i++)
{
ul=ll+cl;
l[i]=ll;
u[i]=ul;
ll=ul;
}
for(i=0;i<nc;i++)
{
printf(Enter the frequency for class %d-%d:”,l[i],u[i]);
scanf(“%d”,&f[i]);
}
cf[0]=f[0];
sf=0;
for(i=0;i<nc;i++)
{
cf[i+1]=cf[i]+f[i+1];
sf=sf+f[i];
}
for(i=0;i<nc;i++)
{
if((sf/2)>=cf[i])
temp=i+1;
}
printf(“Cumulative frequency is \n”);
for(i=0;i<nc;i++)
printf(“%d\t”,cf[i]);
printf(“\nsum of the frequency %d”,sf);
printf(“\nlower limit:%d N/2:%.2f”,l[temp],((float)sf/(float)2));
printf(“\ncf:%d f:%d h:%d”, cf[temp-1], f[temp],cl);
printf(“\nmedian is: %.2f”, 1[temp]+((((float)sf/(float)2) – cf[temp-1]/f[temp]*cl));
getch();
}
Calculate the mode for ungrouped data
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int i,j,n,k=0,a[100],b[100],t;
int count=1,max=0,mode;
clrscr();
printf(“Enter the total number of item\n”);
scanf(“%d”,&n);
printf(“Enter the elements of the item\n”);
for(i=0;i<n;i++)
{
scanf(“%d”,&a[i]);
}
for(i=0;i<n-1;i++)
{
mode=0;
for(j=i+1;j<n;j++)
{
if(a[i]==a[j])
{
mode++;
}
}
if((mode>max)&&(mode!=0))
{
k=0;
max=mode;
b[k]=a[i];
k++;
}
else if(mode==max)
{
b[k]=a[i];
k++;
}
}
for(i=0;i<n;i++)
{
if(a[i]==b[i])
count++;
}
if(count==n)
printf(“\n There is no mode”);
else
{
printf(“\nmode=”);
for(i=0;i<k;i++)
printf(%d\t”,b[i]);
}
getch();
}

You might also like