Sei sulla pagina 1di 17

C Programmes

C Programmes MVP Samaj’s KTHM College, Nashik Department of Statistics T.Y.B.Sc. C-Programming (List of Programmes) /*

MVP Samaj’s KTHM College, Nashik Department of Statistics

T.Y.B.Sc. C-Programming (List of Programmes)

/* Convertion of degree celsius to farenhit*/

#include<stdio.h>

#include<conio.h>

void main()

{

clrscr();

float c,fh;

printf("Enter the temperature in degree celsius \n");

scanf("%f",&c);

/*c=13;*/

fh=(c*(9/5))+32;

printf("%f degree celsius temperature to farenhit is %f",c,fh);

getch();

}

/* To check whether given number is odd or even*/

#include<stdio.h>

#include<conio.h>

void main()

{

clrscr();

int x;

printf("Enter the given integer \n");

scanf("%d",&x);

if(x%2==0)

{

int x; printf("Enter the given integer \n"); scanf("%d",&x); if(x%2==0) { Nutan Vijay Khangar 1
int x; printf("Enter the given integer \n"); scanf("%d",&x); if(x%2==0) { Nutan Vijay Khangar 1

C Programmes

C Programmes printf("The given %d integer number is even",x); } else { printf("The given %d integer

printf("The given %d integer number is even",x);

}

else

{

printf("The given %d integer number is odd",x);

}

getch();

}

/* To find area of circle*/ #include<stdio.h> #include<conio.h> void main()

{

 

float r,area; clrscr(); printf("Enter the radius of circle \n"); scanf("%f",&r);

area=3.14*r*r;

printf("Area of circle is %f ",area); getch();

}

/* To find area of triangle*/ #include<stdio.h> #include<conio.h> #include<conio.h> void main()

{

#include<stdio.h> #include<conio.h> #include<conio.h> void main() { Nutan Vijay Khangar 2
#include<stdio.h> #include<conio.h> #include<conio.h> void main() { Nutan Vijay Khangar 2

C Programmes

C Programmes clrscr(); float b,ht; printf("enter the base of triangle\n"); scanf("%f",&b);

clrscr(); float b,ht; printf("enter the base of triangle\n"); scanf("%f",&b); printf("enter the height of triangle"); scanf("%f",&ht);

area=0.5*b*ht;

printf("area of trianglr %f is",area); getch();

}

OR

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

{

 

clrscr(); float a,b,c,s,area; printf("Enter three sides of triangle \n"); scanf("%f%f%f",&a,&b,&c);

s=(a+b+c)/2;

area=sqrt(s*(s-a)*(s-b)*(s-c)); printf("Area of triangle is %f ",area); getch();

}

/* To find maximum of two numbers */ #include<stdio.h> #include<conio.h>

} /* To find maximum of two numbers */ #include<stdio.h> #include<conio.h> Nutan Vijay Khangar 3
} /* To find maximum of two numbers */ #include<stdio.h> #include<conio.h> Nutan Vijay Khangar 3

C Programmes

C Programmes void main() { clrscr(); int x,y; printf("Enter two numbers \n"); scanf("%f%f",&x,&y);

void main()

{

clrscr();

int x,y;

printf("Enter two numbers \n"); scanf("%f%f",&x,&y); if(x>y)

{

 

printf("%d is the maximum number",x);

}

if(x<y)

{

 

printf("%d is the minimum number",x);

}

else

{

 

printf("Two numbers are equal");

}

getch();

}

/* To find maximum of three numbers */ #include<stdio.h> #include<conio.h> void main()

{

clrscr(); int x,y,z; printf("Enter three numbers \n"); scanf("%d%d%d",&x,&y,&z);

int x,y,z; printf("Enter three numbers \n"); scanf("%d%d%d",&x,&y,&z); Nutan Vijay Khangar 4
int x,y,z; printf("Enter three numbers \n"); scanf("%d%d%d",&x,&y,&z); Nutan Vijay Khangar 4

C Programmes

C Programmes if(x>y & x>z) { printf("%d is the maximum number",x); } if(y>x & y>z) {

if(x>y & x>z)

{

printf("%d is the maximum number",x);

}

if(y>x & y>z)

{

printf("%d is the maximum number",y);

}

if(z>x & z>y)

{

printf("%d is the maximum number",z);

}

getch();

}

/* Program to carry out arithmetic operations */ #include<stdio.h> #include<conio.h> void main()

{

float a,b,sum,sub,prod,div; clrscr(); printf("Enter two numbers a and b\n"); scanf("%f %f",&a,&b); sum=a+b; sub=a-b; prod=a*b; div=a/b; printf("Addition of two number is %f\n",sum); printf("Subtraction of two number is %f\n",sub);

of two number is %f\n",sum); printf("Subtraction of two number is %f\n",sub); Nutan Vijay Khangar 5
of two number is %f\n",sum); printf("Subtraction of two number is %f\n",sub); Nutan Vijay Khangar 5

C Programmes

C Programmes printf("Multiplication of two number is %f\n",prod); printf("Divison of two number is

printf("Multiplication of two number is %f\n",prod); printf("Divison of two number is %f\n",div); getch();

}

/* Program to check whether the integer m is divisible by n or not*/ #include<stdio.h> #include<conio.h> void main()

{

 

clrscr();

int m,n;

printf("Enter the integers m and n\n"); scanf("%d %d",&m,&n); if(m % n == 0)

{

printf("m is divisible by n");

}

else

{

printf("m is not divisible by n");

}

getch();

}

/* Program to find roots of quadratic equation*/ #include<stdio.h> #include<conio.h> #include<math.h>

quadratic equation*/ #include<stdio.h> #include<conio.h> #include<math.h> Nutan Vijay Khangar 6
quadratic equation*/ #include<stdio.h> #include<conio.h> #include<math.h> Nutan Vijay Khangar 6

C Programmes

C Programmes void main() { clrscr(); int a,b,c,d; float r1,r2; printf("Enter the coefficients a,b and c\n");

void main()

{

clrscr();

int a,b,c,d;

float r1,r2; printf("Enter the coefficients a,b and c\n"); scanf("%d %d %d",&a,&b,&c);

d=(b*b)-(4*a*c);

if(d > 0)

{

r1=(-b+(sqrt(d)))/(2*a);

printf("Roots of quadratic equation %f and %f",r1,r2);

}

else

{

printf("Roots of quadratic equation are imaginary");

}

getch();

}

/* Program to generate Fibonacci series*/ #include<stdio.h> #include<conio.h> void main()

{

int a=0,b=1,c,i,n; printf("Enter how many elements ? \n"); scanf("%d",&n); printf("\n%d %d",a,b);

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

? \n"); scanf("%d",&n); printf("\n%d %d",a,b); for(i=2;i<n;i++) Nutan Vijay Khangar 7
? \n"); scanf("%d",&n); printf("\n%d %d",a,b); for(i=2;i<n;i++) Nutan Vijay Khangar 7

C Programmes

C Programmes { c=a+b; printf(" %d",c); a=b; b=c; } getch(); } /* Program to evaluate simple

{

c=a+b;

printf(" %d",c);

a=b;

b=c;

}

getch();

}

/* Program to evaluate simple and compound interest */ #include<stdio.h> #include<conio.h> #include<math.h> void main()

{

int n; float i,simple,compound; printf("Enter number of years and interest rate\n"); scanf("%d %f",&n,&i);

simple=(1+i*n);

compound=pow((1+i),n);

printf("Sum assured by simple interest = %f",simple);

printf("Sum assured by compound interest = %f",compound); getch();

}

/* Program to find mean, geometric mean and harmonic mean */ #include<stdio.h> #include<conio.h>

mean, geometric mean and harmonic mean */ #include<stdio.h> #include<conio.h> Nutan Vijay Khangar 8
mean, geometric mean and harmonic mean */ #include<stdio.h> #include<conio.h> Nutan Vijay Khangar 8

C Programmes

C Programmes #include<math.h> void main() { int i,n; float x,sum,product,inverse_sum,mean,gm,hm; sum=0; product=1;

#include<math.h> void main()

{

int i,n;

float x,sum,product,inverse_sum,mean,gm,hm;

sum=0;

product=1;

inverse_sum=0;

printf("Enter number of observations\n"); scanf("%d",&n); printf("Enter observations\n");

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

{

scanf("%f",&x); sum=sum+x; product = product*x; inverse_sum = inverse_sum + 1/(x);

}

mean=sum/n;

gm=pow(product,1/n);

hm=n/inverse_sum; printf("Arithmetic mean = %f\n",mean); printf("Geometric mean = %f\n",gm); printf("Harmonic mean = %f\n",hm);

getch();

}

/* Program to prepare multiplication table*/ #include<stdio.h> #include<conio.h>

/* Program to prepare multiplication table*/ #include<stdio.h> #include<conio.h> Nutan Vijay Khangar 9
/* Program to prepare multiplication table*/ #include<stdio.h> #include<conio.h> Nutan Vijay Khangar 9

C Programmes

C Programmes void main() { int i,n,table; clrscr(); printf("Enter a number \n"); scanf("%d",&n);

void main()

{

int i,n,table; clrscr(); printf("Enter a number \n"); scanf("%d",&n);

printf("Table of %d is\n",n);

table=1;

for(i=1;i<=10;i++)

{

table=i*n; printf("%d\n", table);

}

getch();

}

/* Program to find sum of digits of a number*/ #include<stdio.h> #include<conio.h> void main()

{

int x,s,a;

s=0;

clrscr();

printf("Enter a number\n"); scanf("%d",&x);

while(x>0)

{

a=x%10;

s=s+a;

printf("Enter a number\n"); scanf("%d",&x); while(x>0) { a=x%10; s=s+a; Nutan Vijay Khangar 10
printf("Enter a number\n"); scanf("%d",&x); while(x>0) { a=x%10; s=s+a; Nutan Vijay Khangar 10

C Programmes

C Programmes } x=x/10; printf("Sum of digits = %f",s); getch(); } /* To find roots of

}

x=x/10;

printf("Sum of digits = %f",s); getch();

}

/* To find roots of equation using Newton Raphson Method */ #include<stdio.h> #include<conio.h> #include<math.h> float f(float x)

{

return x*x-4*x-7;

}

float df (float x)

{

return 2*x-4;

}

main()

{

int itr, maxmitr; float h, x0, x1, allerr;

printf("\nEnter x0, allowed error and maximum iterations\n"); scanf("%f %f %d", &x0, &allerr, &maxmitr); for (itr=1; itr<=maxmitr; itr++)

{

h=f(x0)/df(x0);

x1=x0-h;

printf(" At Iteration no. %3d, x = %9.6f\n", itr, x1); if (fabs(h) < allerr)

x1=x0-h; printf(" At Iteration no. %3d, x = %9.6f\n", itr, x1); if (fabs(h) < allerr) Nutan
x1=x0-h; printf(" At Iteration no. %3d, x = %9.6f\n", itr, x1); if (fabs(h) < allerr) Nutan

C Programmes

C Programmes { printf("After %3d iterations, root = %8.6f\n", itr, x1); return 0; } } x0=x1;

{

printf("After %3d iterations, root = %8.6f\n", itr, x1); return 0;

}

}

x0=x1;

/* printf(" The required solution does not converge or iterations are insufficient\n");*/ return 1;

}

/* Program to convert decimal number to equivalent binary number */ #include<stdio.h> #include<conio.h> void main()

{

clrscr(); int dec,rem,i=1; long int bin=0;

printf("Enter the decimal number : "); scanf("%d",&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();

}

dec=dec/2; bin=bin+(i*rem); i=i*10; } printf("The binary number is %d",bin); getch(); } Nutan Vijay Khangar 12
dec=dec/2; bin=bin+(i*rem); i=i*10; } printf("The binary number is %d",bin); getch(); } Nutan Vijay Khangar 12
dec=dec/2; bin=bin+(i*rem); i=i*10; } printf("The binary number is %d",bin); getch(); } Nutan Vijay Khangar 12

C Programmes

C Programmes /* Program to check whether number is prime or not*/ #include<stdio.h> #include<conio.h>

/* Program to check whether number is prime or not*/ #include<stdio.h> #include<conio.h> #include<math.h> void main()

{

int i,x,y;

clrscr(); printf("Enter a number: "); scanf("%d",&x);

if(x==1)

{

printf("%d is not a prime number",x); goto e;

}

else

{

for (i=2;i<=pow(x,0.5);i++)

{

y=x%i;

if(y==0)

{

printf("%d is not a prime number",x); goto e;

}

}

printf("%d is a prime number",x);

}

e:

getch();

not a prime number",x); goto e; } } printf("%d is a prime number",x); } e: getch();
not a prime number",x); goto e; } } printf("%d is a prime number",x); } e: getch();

}

C Programmes

} C Programmes /* Program to test palindrome string*/ #include <stdio.h> #include <conio.h> #include
} C Programmes /* Program to test palindrome string*/ #include <stdio.h> #include <conio.h> #include
} C Programmes /* Program to test palindrome string*/ #include <stdio.h> #include <conio.h> #include

/* Program to test palindrome string*/ #include <stdio.h> #include <conio.h> #include <string.h> void main()

{

 

char a[100], b[100]; 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("Entered string is a palindrome.\n");

}

else

{

printf("Entered string isn't a palindrome.\n");

}

getch();

}

/* Program to sort a string using string function*/ #include <stdio.h> #include <conio.h> #include <string.h> void main()

#include <stdio.h> #include <conio.h> #include <string.h> void main() Nutan Vijay Khangar 14
#include <stdio.h> #include <conio.h> #include <string.h> void main() Nutan Vijay Khangar 14

C Programmes

C Programmes {   clrscr(); char string[] = "kthmcollegenashik"; char temp; int i, j; int n

{

 

clrscr(); char string[] = "kthmcollegenashik"; char temp; int i, j; int n = strlen(string);

printf("String before sorting - %s \n", string); for (i = 0; i < n-1; i++)

{

for (j = i+1; j < n; j++)

{

 

if (string[i] > string[j])

{

temp = string[i]; string[i] = string[j]; string[j] = temp;

}

 

}

}

printf("String after sorting - %s \n", string);

getch();

}

/* Program to search string using string function*/ #include <stdio.h> #include <conio.h> #include <string.h> void main()

{

char mystr[30] = "Example of function strchr";

#include <string.h> void main() { char mystr[30] = "Example of function strchr"; Nutan Vijay Khangar 15
#include <string.h> void main() { char mystr[30] = "Example of function strchr"; Nutan Vijay Khangar 15

C Programmes

C Programmes printf ("%s", strchr(mystr, 'f')); getch(); } /* Program to combine given two strings using

printf ("%s", strchr(mystr, 'f')); getch();

}

/* Program to combine given two strings using string function*/ #include <stdio.h> #include <conio.h> #include <string.h> void main()

{

char a[100], b[100]; printf("Enter the first string\n"); gets(a); printf("Enter the second string\n"); gets(b); strcat(a,b); printf("String obtained on concatenation is %s\n",a); getch();

}

/* C Efficient program to calculate */ /* e raise to the power x */ #include <stdio.h> #include <conio.h>

/* Returns approximate value of e^x */ /* using sum of first n terms of Taylor Series */ float exponential(int n, float x)

{

float sum = 1.0f; // initialize sum of series

Series */ float exponential(int n, float x) { float sum = 1.0f; // initialize sum of
Series */ float exponential(int n, float x) { float sum = 1.0f; // initialize sum of

C Programmes

C Programmes for (int i = n - 1; i > 0; --i ) sum =

for (int i = n - 1; i > 0; --i ) sum = 1 + x * sum / i; return sum;

}

/* Driver program to test above function */ void main()

{

int n = 10; float x = 1.0f; printf("e^x = %f", exponential(n, x)); getch();

}

main() { int n = 10; float x = 1.0f; printf("e^x = %f", exponential(n, x)); getch();
main() { int n = 10; float x = 1.0f; printf("e^x = %f", exponential(n, x)); getch();