programming in C practical
programming in C practical
Write a program to find the sum, average, standard deviation for a given set of
numbers. */
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
intn,x[100],i,sum;
float mean,vsum,ksum,sd;
clrscr();
printf("Enter the n value\n");
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%d",&x[i]);
}
sum=0;
for(i=0;i<n;i++)
{
sum=sum+x[i];
}
printf("Sum=%d\n",sum);
mean = 0;
mean = sum/n;
printf(" mean=%f\n",mean);
vsum=0;
for(i=0;i<n;i++)
vsum=vsum+((x[i]-mean)*(x[i]-mean));
ksum=vsum/n;
sd=sqrt(ksum);
printf("standard deviation = %f\n",sd);
getch();
}
OUTPUT:
/* 2. Write a program to print prime numbers up to a given number.*/
#include<stdio.h>
#include<conio.h>
void main()
{
intn,i,fact,j;
clrscr();
printf("Enter the Number");
scanf("%d",&n);
printf("Prime Numbers are: \n");
for(i=1; i<=n; i++)
{
fact=0;
for(j=1; j<=n; j++)
{
if(i%j==0)
fact++;
}
if(fact==2)
printf("%d " ,i);
}
getch();
}
OUTPUT:
/* 3. Program for Converting Decimal to Binary */
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
intdec,rem,i;
i=1;
long int bin;
bin=0;
printf("enter the decimal number");
scanf("%ld",&dec);
while(dec>0)
{
rem=dec %2;
dec=dec/2;
bin=bin+(i*rem);
i=i*10;
}
printf("the binary number is %d",bin);
getch();
}
OUTPUT:
/*4. Program to multiply two matrices using functions.*/
#include<stdio.h>
#include<conio.h>
Int i,j,k;
void main()
{
int a[10][10],b[10][10],c[10][10],m,n,p,q;
void mul(int x[10][10],int y[10][10],int z[10][10],int m,int n,int q);
void read(int x[10][10],int m,int n);
void display(int x[10][10], int m,int n);
clrscr();
printf("Enter the size of A Matrix (Row and Col): \n");
scanf("%d%d",&m,&n);
printf("Enter the size of B Matrix (Row and Col): \n");
scanf("%d%d",&p,&q);
if(n!=p)
{
printf("Multiplication Not Possible\n Please re-enter\n");
printf("correct size and try again .....\n");
}
else
{
read(a,m,n);
read(b,m,n);
mul(a,b,c,m,n,q);
printf("A Matrix is :\n");
display(a,m,n);
printf("B Matrix is :\n");
display(b,m,n);
printf("C Matrix is :\n");
display(c,m,n);
}
getch();
}
void mul(int x[10][10],int y[10][10],int z[10][10],int m,int n,int q)
{
for (i=0;i<m;i++)
for(j=0;j<q;j++)
{
z[i][j]=0;
for(k=0;k<n;k++)
z[i][j]+= x[i][k]*y[k][j];
}
}
void read(int x[10][10], intm,int n)
{
printf("Enter Matrix Value Row by Row\n");
for (i=0;i<m;i++)
for(j=0;j<n;j++)
scanf("%d",&x[i][j]);
}
void display(int x[10][10], intm,int n)
{
for (i=0;i<m;i++)
{
for(j=0;j<n;j++)
printf("%5d",x[i][j]);
printf("\n");
}
printf("\n");
}
OUTPUT:
/* 5. Calculate the binomial co-efficient using functions.*/
#include<stdio.h>
#include<conio.h>
int bc(int n, int k)
{
if (k==0 || k==n)
return 1;
return( bc(n-1, k-1) + bc(n-1, k));
}
int main()
{
clrscr();
int n,k;
printf(" Enter the value of n\n");
scanf("%d",&n);
printf(" Enter the value of k\n");
scanf("%d",&k);
printf("Value of C(%d, %d) is %d ",n,k, bc(n, k));
return 0;
}
OUTPUT:
/* 6. program to check whether a given word is a palindrome or not*/
#include <stdio.h>
#include <string.h>
#include<conio.h>
int main()
{
char a[100], b[100];
clrscr();
printf("Enter a string to check if it is a palindrome\n");
gets(a);
strcpy(b, a);
strrev(b);
if (strcmp(a, b) == 0)
printf("The string is a palindrome.\n");
else
printf("The string isn't a palindrome.\n");
getch();
return 0;
}
OUTPUT:
/* 7. binary search to find a particular name in a list of names.*/
#include <stdio.h>
#include <string.h>
#include<conio.h>
#include <stdlib.h>
void main()
clrscr();
inti,n,low,high,mid;
char a[50][50],key[20];
scanf("%d",&n);
for(i=0;i<=n-1;i++)
scanf("%s",&a[i]);
printf("\n");
scanf("%s",&key);
low=0;
high=n-1;
while(low<=high)
mid=(low+high)/2;
if (strcmp(key,a[mid])==0)
{
printf("key found at the position %d\n",mid+1);
exit(0);
else if(strcmp(key,a[mid])>0)
high=high;
low=mid+1;
else
low=low;
high=mid-1;
}
OUTPUT:
/* 8. Program for Ascending order of given N numbers */
#include<stdio.h>
#include<conio.h>
main()
{
inti,j,a,n,number[30];
clrscr();
printf(" enter the value of n \n");
scanf("%d",&n);
printf("enter the numbers \n");
for(i=0;i<n;i++)
scanf("%d",&number[i]);
for(i=0;i<n;i++)
{
for(j=i+1;j<n;++j)
{
if(number[i]> number[j])
{
a=number[i];
number[i]=number[j];
number[j]=a;
}
}
}
printf("the numbers arranged in ascending order are given below \n");
for(i=0;i<n;i++)
printf("%d\n",number[i]);
return(0);
}
OUTPUT:
/*9. program to print the Student's Mark sheet assuming Register number, name, and
marks in 5 subjects in a Structure. */
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
structmark_sheet
{
char name[20];
long introllno;
int marks[10];
int total;
float average;
char rem[10];
char cl[20];
}
students[100];
int main()
{
inta,b,n,flag=1;
clrscr();
printf("How many students : \n");
scanf("%d",&n);
for(a=1;a<=n;++a)
{
printf("\n\nEnter the details of %d students : ", n-a+1);
printf("\n\nEnter student %d Name : ", a);
scanf("%s", students[a].name);
printf("\n\nEnter student %d Roll Number : ", a);
scanf("%ld", &students[a].rollno);
students[a].total=0;
for(b=1;b<=5;++b)
{
printf("\n\nEnter the mark of subject-%d : ", b);
scanf("%d", &students[a].marks[b]);
students[a].total += students[a].marks[b];
if(students[a].marks[b]<40)
flag=0;
}
students[a].average = (float)(students[a].total)/5.0;
if((students[a].average>=75)&&(flag==1))
strcpy(students[a].cl,"Distinction");
else
if((students[a].average>=60)&&(flag==1))
strcpy(students[a].cl,"First Class");
else
if((students[a].average>=50)&&(flag==1))
strcpy(students[a].cl,"Second Class");
else
if((students[a].average>=40)&&(flag==1))
strcpy(students[a].cl,"Third Class");
if(flag==1)
strcpy(students[a].rem,"Pass");
else
strcpy(students[a].rem,"Fail");
flag=1;
}
for(a=1;a<=n;++a)
{
clrscr();
printf("\n\n\t\t\t\tMark Sheet\n");
printf("\nName of Student : %s", students[a].name);
printf("\t\t\t\t Roll No : %ld", students[a].rollno);
printf("\n------------------------------------------------------------------------");
for(b=1;b<=5;b++){
printf("\n\n\t Subject %d \t\t :\t %d", b, students[a].marks[b]);
}
printf("\n\n------------------------------------------------------------------------\n");
printf("\n\n Totl Marks : %d", students[a].total);
printf("\t\t\t\t Average Marks : %5.2f", students[a].average);
printf("\n\n Class : %s", students[a].cl);
printf("\t\t\t\t\t Status : %s", students[a].rem);
}
return(0);
}
OUTPUT:
/*10. Program to count the number of alphabets, special characters and words in a line
of text using file */
#include <stdio.h>
#include<conio.h>
int main()
{
clrscr();
FILE *fp;
char filename[100];
char ch;
intwc,ac,scc;
wc=ac=scc=0;
printf("Enter a filename :");
gets(filename);
fp = fopen(filename,"r");
if ( fp )
{
while ((ch=getc(fp)) != EOF)
{
if(ch==' '|| ch<='\n')
wc++;
else if((ch>='A' &&ch<='Z')||(ch>='a' &&ch<='z'))
ac++;
else
scc++;
}
}
printf(" wordcount: %d\n alphabets:%d\nSpecial Characters:%d\n ",wc,ac,scc);
return (0);
}
OUTPUT: