/* Centigrade to Fahrenheit conversion */
#include<stdio.h>
void main()
{
float celsius, fahrenheit;
printf( “\ Enter temp in celsius: ”);
scanf(“%f”,&celsius);
fahrenheit=(1.8*celsius)+32;
printf(“\n temperature in Fahrenheit: %f”,fahrenheit);
}
OUT PUT:
Enter temp in celsius: 32
Temperature in fahrenheit: 89.59998
/* Even or Odd */
# include<stdio.h>
void main()
{
int number;
printf("enter an integer: ");
scanf("%d",&number);
if (number%2==0)
printf("%d is even.",number);
else
printf("%d is odd.",number);
}
OUT PUT:
Enter an integer: 4
4 is Even.
/* Greatest of three numbers */
#include<stdio.h>
void main()
{
int a,b,c;
printf("/n enter value of a,b& c");
scanf("%d%d%d",&a,&b,&c);
if ((a>b) && (a>c))
printf("/n a is greatest");
else
if ((b>c && b>a))
printf("/n b is greatest");
else
printf("/n c is greatest");
}
OUT PUT:
Enter the value for a,b& c :
15
34
78
C is greatest
/*Switch statement - Display Monday To Sunday*/
#include<stdio.h>
#include<conio.h>
void main()
{
int n;
clrscr();
printf(“Enter any number b/w 1 and 7: “);
scanf(“%d”,&n);
switch(n)
{
case 1:
printf(“Sunday is the first day of the week”);
break;
case 2:
printf(“Monday is the second day of the week”);
break;
case 3:
printf(“Tuesday is the third day of the week”);
break;
case 4:
printf(“Wednesday is the fourth day of the week”);
break;
case 5:
printf(“Thursday is the fifthe day of the week”);
break;
case 6:
printf(“Friday is the sixth day of the week”);
break;
case 7:
printf(“Saturday is the seventh day of the week”);
break;
default:
printf(“You have entered a wrong choice”);
}
getch();
}
OUT PUT:
Enter any number between 1 and 7:
3
Tuesday is the third day of the week
/*First Ten natural numbers and their Sum */
#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,sum=0;
clrscr(); //to clear the screen
printf(“Enter the value of n:”);
scanf(“%d”,&n);
for(i=1;i<=n;++i)
{
printf(“\n%d”,i);
sum+=i;
}
printf(“Sum of first n natural numbers is%d”,sum);
getch();
}
OUT PUT:
Enter the value of n:4
1
2
3
4
Sum of first n natural numbers is 10
/*Multiplication of two matrices */
# include<stdio.h>
void main()
{
int a[5][5],b[5][5],c[5][5],i,j,k,m,n,p,q;
printf("\n enter the row and column of first matrix");
scanf("%d%d",&m,&n);
printf("\n enter the row and column of second matrix");
scanf("%d%d",&p,&q);
if(n!=p)
{
printf("matrix multiplication is not possible");
printf("\n column of first matrix must be same as row of second matrix");
}
else
{
printf("\n enter the first matrix->");
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
printf("\n enter the second matrix->");
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("\n the multiplication of two matrix is \n");
for(i=0;i<m;i++)
{
printf("\n");
for(j=0;j<p;j++)
printf("%d\t",c[i][j]);
}
}
}
OUT PUT:
Enter the row and column of first matrix
2 2
Enter the row and column of second matrix
2 2
Enter the first matrix
2 2
2 2
Enter the second matrix
2 2
2 2
The multiplication of two matrix is
8 8
8 8
/* Finding Maximum Numbers in Array Using Pointer */
#include<conio.h>
void main()
{
int a[10],n,i,max;
int *p;
clrscr();
printf(“Enter the size of array: “);
scanf(“%d”,&n);
printf(“Enter %d elements in the array:\n”,n);
for(i=0;i<n;i++)
scanf(“%d”,&a[i]);
printf(“Elements in the array are:\n”);
for(i=0;i<n;i++)
printf(“%5d”,a[i]);
p=&a[0];
max=a[0];
for(i=0;i<n;i++)
{
if(max<=*p)
max=*p;
p++;
}
printf(“\n Maximum element in the array is: %d”,max);
getch();
}
OUTPUT:
Enter the size of array:4
Enter 4 elements in the array:
7
9
0
6
Elements in the array are
7 9 0 6
Maximum element in the array:9
/*Reverse a Number using Pointer */
#include<stdio.h>
#include<conio.h>
void main()
{
int n,a;
int *rev,*rem,*temp;
clrscr();
printf(“Enter any number: “);
scanf(“%d”,&n);
a=n;
temp=&n;
*rev=0;
while(*temp>0)
{
*rem=*temp%10;
*temp=*temp/10;
*rev=(*rev)*10+*rem;
}
printf(“Reverse of %d is %d”,a,*rev);
getch();
}
OUT PUT:
Enter any number: 3457
Reverse of 3457 is 7543
/* Quadratic Equation using Function*/
#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<iostream.h>
float a,b,c,x1,x2,disc;
void main()
{
void realequal();
void realunequal();
void imaginary();
clrscr();
printf("Enter the co-efficients\n");
scanf("%f%f%f",&a,&b,&c);
disc=b*b-4*a*c;
if(disc>0)
realunequal();
else if(disc==0)
realequal();
else
imaginary();
getch();
}
void realequal()
{
x1=-b/(2*a);
printf("The roots are real and equal\n");
printf("Root = %5.2f",x1);
}
void realunequal()
{
x1=(-b+sqrt(disc))/(2*a);
x2=(-b-sqrt(disc))/(2*a);
printf("The roots are real and unequal\n");
printf("Root 1 = %5.2f\nRoot 2 = %5.2f",x1,x2);
}
void imaginary()
{
x1=-b/(2*a);
x2=sqrt(fabs(disc))/(2*a);
printf("The roots are imaginary\n");
printf("The real part of the Root = %5.2f\n",x1);
printf("The imaginary part of the root = %5.2f",x2);
}
OUT PUT:
Enter the co-efficient
111
The roots are imaginary
The real part of the Root = -0.50
The imaginary part of the root = 0.87
Enter the co-efficient
121
The roots are real and equal
Root = -1.00
Enter the co-efficient
10 1
The roots are real and unequal
Root 1 = 0.00
Root 2 = - 0.10
/* Factorial Number Using Recursion*/
#include<stdio.h>
#include<conio.h>
void main()
{
int fact(int);
int n;
clrscr();
printf("\n\t enter the value of N:");
scanf("%d",&n);
printf("\n\tfactorial of %d is %d",n,fact(n));
}
int fact(int n)
{
if(n==1)
return 1;
else
return(n*fact(n-1));
}
OUT PUT:
enter the value of N:5
factorial of 5 is 120
/ * call by Value */
include<stdio.h>
void call_by_value(int x)
{
printf("Inside call_by_value x = %d before adding 10.\n", x);
x += 10;
printf("Inside call_by_value x = %d after adding 10.\n", x);
}
void main()
{
int a=10;
printf("a = %d before function call_by_value.\n", a);
call_by_value(a);
printf("a = %d after function call_by_value.\n", a);
}
OUT PUT:
a = 10 before function call_by_value.
Inside call_by_value x = 10 before adding 10.
Inside call_by_value x = 20 after adding 10.
a = 10 after function call_by_value.
/* Call by reference */
#include <stdio.h>
voidcall_by_reference(int *y)
{
printf("Inside call_by_reference y = %d before adding 10.\n", *y);
(*y) += 10;
printf("Inside call_by_reference y = %d after adding 10.\n", *y);
}
void main()
{
int b=10;
printf("b = %d before function call_by_reference.\n", b);
call_by_reference(&b);
printf("b = %d after function call_by_reference.\n", b);
OUT PUT:
b = 10 before function call_by_reference.
Inside call_by_reference y = 10 before adding 10.
Inside call_by_reference y = 20 after adding 10.
b = 20 after function call_by_ref
/* Add Two Number Using Pointer */
#include<stdio.h>
void main()
{
int first,second,*p,*q,sum;
printf(“Enter two numbers to Add\n”);
scanf(“%d%d”,&first,&second);
p=&first;
q=&second ;
sum=*p+*q;
printf(“sum of entered numbers=%d\n”,sum);
}
OUT PUT:
Enter two numbers to add
4
5
Sum of entered numbers=9
/* File Creation using student Details */
#include<stdio.h>
#include<conio.h>
struct stu
{
int rno;
char name[20];
int m[3];
}s;
void main()
{
FILE *f;
int i,n;
clrscr();
f=fopen("in.dat","w");
printf("Enter the no. of students ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("enter the rno ");
scanf("%d",&s.rno);
printf("enter the name ");
scanf("%s",s.name);
printf("enter the marks in three subjects ");
scanf("%d%d%d",&s.m[0],&s.m[1],&s.m[2]);
fwrite(&s,sizeof(s),1,f);
}
fclose(f);
f=fopen("in.dat","r");
fread(&s,sizeof(s),1,f);
printf("\n Roll no. name mark 1 mark 2 mark 3\n");
while (!feof(f))
{
printf(" %d \t\t %s \t %d \t %d \t %d\n",s.rno,s.name,s.m[0],s.m[1],s.m[2]);
fread(&s,sizeof(s),1,f);
}
fclose(f);
getch();
}
OUTPUT:
Enter the no. of students 2
enter the rno 34
enter the name mala
enter the marks in three subjects 45 78
90
enter the rno 35
enter the name vani
enter the marks in three subjects
56 89 67
Roll no. name mark 1 mark 2 mark 3
34 mala 45 78 90
35 vani 56 89 67
/* Student information using various File modes */
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
struct stu
{
int rno;
char name[20];
int m[3];
}s;
void main()
{
FILE *f;
int i,n,choice;
clrscr();
printf("FILE CREATION STEP\n");
f=fopen("in.dat","w");
printf("Enter the no. of students ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("enter the rno ");
scanf("%d",&s.rno);
printf("enter the name ");
scanf("%s",s.name);
printf("enter the marks in three subjects ");
scanf("%d%d%d",&s.m[0],&s.m[1],&s.m[2]);
fwrite(&s,sizeof(s),1,f);
}
fclose(f);
while(1)
{
printf("\n Enter the choice\n");
printf(" 1 - displaying records 2 - appending a record 3 - exit");
scanf("%d",&choice);
if (choice==1)
{
f=fopen("in.dat","r");
fread(&s,sizeof(s),1,f);
printf("\n Roll no. name mark 1 mark 2 mark 3\n");
while (!feof(f))
{
printf("%d\t\t%s\t%d\t%d\t %d\n",s.rno,s.name,s.m[0],s.m[1], s.m[2]);
fread(&s,sizeof(s),1,f);
}
fclose(f);
}
if (choice==2)
{
f=fopen("in.dat","a");
printf("enter the rno ");
scanf("%d",&s.rno);
printf("enter the name ");
scanf("%s",s.name);
printf("enter the marks in three subjects ");
scanf("%d%d%d",&s.m[0],&s.m[1],&s.m[2]);
fwrite(&s,sizeof(s),1,f);
fclose(f);
}
if (choice==3)
{
exit(0);
}
}
}
}
}
OUT PUT:
FILE CREATION STEP
Enter the no. of students
2
enter the rno 01
enter the name mala
enter the marks in three subjects 67 89 90
enter the rno 02
enter the name nila
enter the marks in three subjects 56 78 84
Enter the choice
1 - displaying records 2 - appending a record 3 - exit
1
Roll no. name mark 1 mark 2 mark 3
1 mala 67 89 90
2 nila 56 78 84
Enter the choice
1 - displaying records 2 - appending a record 3 - exit
2
enter the rno 01
enter the name nila
enter the marks in three subjects 78 89 90
Enter the choice
1 - displaying records 2 - appending a record 3 - exit
1
Roll no. name mark 1 mark 2 mark 3
1 mala 67 89 90
2 nila 78 89 90
1 nila 78 89 90
Enter the choice
1 - displaying records 2 - appending a record 3 – exit
3