[go: up one dir, main page]

0% found this document useful (0 votes)
16 views19 pages

c Programs 1

The document contains multiple C programs that demonstrate various programming concepts such as a simple calculator, finding roots of quadratic equations, electricity bill generation, binary search, matrix multiplication, Taylor series for sine and cosine, bubble sort, string operations, structures for student data, and using pointers for statistical calculations. Each program includes user input for parameters and outputs the results based on the computations performed. The code snippets illustrate fundamental programming techniques and algorithms in C.

Uploaded by

khanrufee24
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views19 pages

c Programs 1

The document contains multiple C programs that demonstrate various programming concepts such as a simple calculator, finding roots of quadratic equations, electricity bill generation, binary search, matrix multiplication, Taylor series for sine and cosine, bubble sort, string operations, structures for student data, and using pointers for statistical calculations. Each program includes user input for parameters and outputs the results based on the computations performed. The code snippets illustrate fundamental programming techniques and algorithms in C.

Uploaded by

khanrufee24
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

PROGRAM1: SIMPLE CALCULATOR

#include<stdio.h>
#include<conio.h>
void main()
{
int num1,num2;float
result=0; char op;
clrscr();
printf("Enter first number:"); scanf("%d",
&num1); printf("Enter second number:");
scanf("%d", &num2);
printf("Choose operation to perform(+,-,*,/,%) ");
scanf(" %c", &op);
switch(op)

case'+': result=num1+num2;break;
case'-': result=num1-num2;break;
case'*': result=num1*num2;break;
case'/': result=(float)num1/(float)num2;break;
case'%': result=num1%num2; break;
default: printf("Invalid operation\n");
}

printf("result:%f\n",result);

getch();
}
PROGRAM2: ROOTS OF QUADRATIC EQUATION

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

void main()
{
float a,b,c,d,rpart,ipart,root1,root2;clrscr();
printf("Enter three co-efficients\n");scanf("%f %f
%f", &a, &b, &c); if(a==0 && b==0)
{
printf("Invalid inputs");
}
else if(a==0)
{
printf("Linear equation\n");

root1=-c/b; printf("Root=%f\n",root1);
}
else
{
d=(b*b)-(4*a*c);

if(d= =0)
{
printf("The roots real and equal\n");

root1=root2=-b/(2*a);
printf("the roots are root1=%f and root2=%f", root1, root2);
}
else
if(d>0)
{
printf("The roots are real and distinct\n");

root1=(-b+sqrt(d))/(2*a);
root2=(-b-sqrt(d))/(2*a);
printf("The roots are root1=%f and root2=%f", root1, root2);
}
else
{
printf("The roots are imaginary\n");

rpart=-b/(2*a); ipart=sqrt(fabs(d)/(2*a));
printf("The root1=%f+i%f\n", rpart,ipart);
printf("The root2=%f-i%f\n", rpart,ipart);
}
}
getch();
}
PROGRAM3: ELECTRICITY BILL GENERATION

#include<stdio.h>
#include<conio.h>
void main()
{
char name[10];

float units,charge;

clrscr();
printf("Enter name and unit consumed\n");
scanf("%s %f", &name, &units);
if(units<=200)
c=(units*0.8)+100;
else
if(units>200)
charge=(units-200)*0.9+160+100;
else
if(units>300)
charge=(units-300)+160+90+100;
if(charge>=400)
charge=charge+charge*0.15;
printf("name=%s\n",name);
printf("charge=%f\n",charge);
getch();
}
PROGRAM4: BINARY SEARCH

#include<stdio.h>
#include<conio.h> void
main()
{
int i,n,a[10],low,high,mid,key,found=0;

clrscr();
printf("enter size of array\n");
scanf("%d", &n);
printf("enter elements of array\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("key");
scanf("%d",&key);

low=0;
high=n-1;
while(low<=high)
{
mid=(low+high)/2;
if(key==a[mid])
{
found=1;
break;
}

else if(key>a[mid])
{
low=mid+1;
}
else
{
high=mid-1;
}
}
if(found==1)
printf("item found in position:%d",mid+1);

else
printf("item not found");
getch();
}
PROGRAM5: MATRIX MULTIPLICATION

#include<stdio.h>
#include<conio.h> void
main()
{
int m,n,p,q,i,j,k,a[10][10],b[10][10],c[10][10];
clrscr();
printf("Enter the size of matrix A\n");scanf("%d %d",
&m, &n); printf("Enter the size of matrix B\n");
scanf("%d %d", &p, &q);
if(n!=p)
printf("Matrix multiplication is not possible\n");else
{
printf("Enter the elements of matrix A\n");for(i=0;i<m;i++)
for(j=0;j<n;j++)
{
scanf("%d", &a[i][j]);
}
printf("Enter the elements of matrix B\n");

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

for(j=0;j<q;j++)

{
scanf("%d", &b[i][j]);
}

for(i=0;i<m;i++)
for(j=0;j<q;j++)
{ c[i][j]=0;
for(k=0;k<n;k++)
c[i][j]=c[i][j]+a[i][k]*b[k][j];
}
printf("A matrix is\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("%d\t",a[i][j]);
}
printf("\n");
}
printf("B matrix is\n");
for(i=0;i<p;i++)
{
for(j=0;j<q;j++)
{
printf("%d\t",b[i][j]);
}
printf("\n");
}
printf("The resultant matrix C is\n");for(i=0;i<m;i++)
{
for(j=0;j<q;j++)

printf("%d\t",c[i][j]);
}
printf("\n");
}
}
getch();
}
PROGRAM7: TAYLOR SERIES FOR SINX/COSX

A. FOR COSX:
#include<stdio.h>
#include<conio.h>
#include<math.h>

void main()

int n,i;
float degree,x,num,den,value,term;

clrscr();
printf("Enter number of terms\n");
scanf("%d", &n);
printf("Enter the angle\n");
scanf("%f", &degree);
x=degree*3.1416/180; num=1;
den=1; term=num/den;
value=0;
for(i=1;i<=n;i++)
{
value=value+term;

num=-num*x*x;
den=den*(2*i)*(2*i-1);
term=num/den;
}
printf("Calculated value of cos(%f) is %f\n",degree,value);
printf("value of sin(%f) using built in function is %f",degree,cos(x));
getch();
}
B. FOR SINX
#include<stdio.h>
#include<conio.h>
#include<math.h>void
main()

int n,i;
float degree,x,num,den,value,term;clrscr();
printf("Enter number of terms\n");
scanf("%d", &n);
printf("Enter the angle\n");
scanf("%f", &degree);
x=degree*3.1416/180;
num=x;
den=1;
term=num/den;
value=0;
for(i=1;i<=n;i++)

value=value+term;

num=-num*x*x;
den=den*(2*i)*(2*i+1);
term=num/den;
}
printf("Calculated value of sin(%f) is %f\n",degree,value);

printf("value of sin(%f) using built in function is %f",degree,sin(x));

getch();
}
PROGRAM7: BUBBLE SORT

#include<stdio.h>
#include<conio.h>
void main()
{

int a[10],n,i,j,temp;clrscr();
printf("Enter number of elements\n");
scanf("%d", &n);
printf("Enter the elements of array\n");
for(i=0;i<n;i++)
scanf("%d", &a[i])
; printf("the input array is\n");
for(i=0;i<n;i++)

printf("%d\t", 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("\nThe sorted array is\n");

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

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

}
PROGRAM8: FUNCTIONS FOR STRING OPERATIONS

#include<stdio.h>
#include<conio.h>
int compare_string(char [], char []);
void concatenate(char [], char []);
int string_length(char []);
int main()
{
char s1[1000], s2[1000];
int lenght1,length2;

clrscr();
printf("Input a string1\n");
gets(s1);
printf("Input a string2\n");
gets(s2);
length1=string_length(s1);
length2=string_length(s2);
printf("Length of s1=%d\n",length1);
printf("Length of s2=%d\n",length2);
if(compare_string(s1,s2)==0)
printf("Equal strings\n");
else
printf("Unequal strings\n");
concatenate(s1, s2);
printf("string obtained on concatenation:\"%s\\n",s1);
getch();
return 0;

}
int string_length(char s1[])

int c=0;

while (s1[c]!='\0')

c++;
return c;

int compare_string(char s1[], char s2[])

int c=0;
while(s1[c]==s2[c])
{

if(s1[c]=='\0'|| s2[c]=='\0')

break;
c++;

if(s1[c]=='\0' && s2[c]=='\0')

return 0;
else
return 1;
}

void concatenate(char s1[], char s2[])

int c,d;
c=0;
while(s1[c]!='\0')

c++;
}
d=0;
while(s2[d]!='\0')

s1[c]=s2[d];
d++;
c++;

s1[c]='\0';
}
PROGRAM 9: STRUCTURES TO READ, WRITE AND COMPUTEAVERAGE
#include<stdio.h>
#include<conio.h>
struct student
{

char usn[50];

char name[50]

;int marks;
} s[10];

void main()

int i,n,countav=0,countbv=0;

float sum=0,avg;
clrscr();

printf("Enter number of students\n")

;scanf("%d",&n);
printf("Enter students information\n");
for(i=0;i<n;i++)
{

printf("Enter USN\n");

scanf("%s",&s[i].usn);

printf("Enter name\n");

scanf("%s",&s[i].name);

printf("Enter marks\n");
scanf("%d",&s[i].marks);
printf("\n");
}

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

printf("USN:%s\n",s[i].usn);

printf("Name:%s\n",s[i].name);

printf("Marks:%d\n",s[i].marks);
}

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

sum=sum+s[i].marks;

printf("sum:%f\n",sum);

avg=sum/n;
printf("\nAverage marks:%f",avg);
for(i=0;i<n;i++)
{

if(s[i].marks>=avg)

countav++;
else
countbv++;
}

printf("\nTotal number of students above average:%d",countav);

printf("\nTotal number of students below average:%d",countbv);

getch();
}
PROGRAM 10:USING POINTERS COMPUTE SUM, MEAN AND STANDARD DEVIATION

#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{

float a[10],*ptr,mean,std,sum=0,sumstd=0;int i,n;


clrscr();

printf("Enter the number of elements\n");

scanf("%d", &n);
printf("Enter array elements\n");
for(i=0;i<n;i++)
scanf("%d", &a[i]);
ptr=a; for(i=0;i<n;i++)
{

sum=sum+ *ptr;ptr++;
}

mean=sum/n;

ptr=a;

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

sumstd=sumstd+pow((*ptr-mean),2);

ptr++;
}
std=sqrt(sumstd/n);

printf("sum=%f\t Mean=%f\t standard deviation=%f",sum,mean,std);

getch();
}

You might also like