Lab Manual4,5

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 11

EXERCISE-4

C PROGRAM USING SIMPLE STATEMENTS AND EXPRESSIONS

4.a) AREA & CIRCUMFERENCE OF CIRCLE

PROGRAM:

/*TO FIND THE AREA AND CIRCUMFERENCE OF THE CIRCLE*/

#include<stdio.h>
#include<conio.h>
void main()
{
float rad,area,circum;
clrscr();
printf(“\nEnter the radius of the circle”);
scanf(“%f”,&rad);
area=3.14*rad*rad;
circum=2*3.14*rad;
printf(“\nArea=%f”,area);
printf(“\nCircumference=%f”,circum);
getch();
}

SAMPLE OUTPUT:

Enter the radius of the circle


5
Area=78.500000
Circumference=31.400000
4.b) FAHRENHEIT TO CELSIUS

PROGRAM:

/*TO FIND THE FAHRENHEIT TO CELSIUS*/

#include<stdio.h>
#include<conio.h>
void main()
{
float ft , ct;                                                               // ft stands for fahrenheit temprature.
                                                                                // ct stands for celsius temprature.
clrscr();
printf("Enter the temprature in fahrenheit/n");
scanf("%f" , &ft);
ct=(ft-32)/18;                                                                // Formula of Celsius
printf("Fahrenheit temprature = %f/n" , ft);
printf("Celsius temprature = %f/n" ,ct);
getch()
}

SAMPLE OUTPUT:
Enter the temperature in Fahrenheit
64
Fahrenheit temperature = 64
Celsius temperature = 1.777
4.c) SWAPPING OF 2 NOS USING TEMPORARY VARIABLES

PROGRAM:

/*TO FIND SWAPPING OF 2 NOS USING TEMPORARY VARIABLES*/

#include<stdio.h>
#include<conio.h>
int main()
{
int a, b, temp;
clrscr();
printf("En ter the value of a and b: \n");
scanf("%d %d",&a,&b);
printf("Be fore swapping a=%d, b=%d \n", a, b);
/*Swapping logic */
temp = a;
a = b;
b = temp;
printf("After swapping a=%d, b=%d", a, b);
getch();
return 0;
}

SAMPLE OUTPUT:
Enter the values of a and b: 2 3
Before swapping a=2, b=3
After swapping a=3, b=2
4.d) SWAPPING OF 2 NOS WITHOUT USING TEMPORARY VARIABLES

PROGRAM:

/*TO FIND SWAPPING OF 2 NOS WITHOUT USING TEMPORARY VARIABLES*/

#include<stdio.h>
#include<conio.h>
int main()
{
int a, b;
clrscr();
printf("En ter values of a and b: \n");
scanf("%d%d",&a,&b);
printf("Before swapping a=%d, b=%d\n", a,b);
/*Swapping logic */
a = a + b;
b = a - b;
a = a - b;
printf("The values After swapping are a=%d b=%d\n", a, b);
getch();
return 0;
}

SAMPLE OUTPUT:

Enter values of a and b: 2 3


Before swapping a=2, b=3
The values After swapping are a=3 b=2
4.e) BIGGEST OF 3 NUMBERS

PROGRAM:

//TO FIND THE LARGEST OF THE THREE NUMBERS


#include<stdio.h>
#include<conio.h>
int main()
{
int a, b, c;
clrscr();
printf("En ter a,b,c: \n");
scanf("%d %d %d",&a,&b,&c);
if (a>b&&a>c)
{
printf("a is Greater than b and c");
}
else if (b>a&&b>c)
{
printf("b is Greater than a and c");
}
else if (c>a&&c>b)
{
printf("c is Greater than a and b");
}
else
{
printf("al l are equal or any two values are equal");
}
getch();
return 0;
}

SAMPLE OUTPUT:

Enter a,b,c: 3 5 8
c is Greater than a and b
EXERCISE – 5

SCIENTIFIC PROBLEM SOLVING USING DECISION MAKING AND LOOPING

5.a) CONVERSIONS FROM BINARY TO DECIMAL

PROGRAM

//CONVERT BINARY NO TO DECIMAL NO


#include<stdio.h>
#include<conio.h>
void main()
{
int bnum,digit,decimal=0,bin,base=0;
clrscr();
printf("\n Enter the Binary No:");
scanf("%d",&bnum);
bin=bnum;
while(bnum!=0)
{
digit=bnum%10;
decimal=decimal+(digit<<base);
base=base+1;
bnum=bnum/10;
}
printf("\n The Binary %d to Decimal is=%d",bin,decimal);
getch();
}

OUTPUT:-

Enter the Binary No:100

The Binary 100 to Decimal is=4

SAMPLE OUTPUT

Enter any number any binary number: 1101


Equivalent decimal value: 13
5.B) SINE SERIES

PROGRAM:

#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int no,i;
float x, a, sum,b;
clrscr();
printf("Enter the numbers");
scanf("%f %d",&x,&no);
b=x;
x=x*3.141/180;
a=x;
sum=x;
for(i=1;i<no+1;i++)
{
a= (a*pow((double)(-1),(double)(2*i-1))*x*x)/(2*i*(2*i+1));
sum=sum+a;
}
printf("Sine(%f) value is %f",b,sum);
getch();
}

OUTPUT:

Enter the numbers


30
100
sin(30.000000) value is 0.499914
PASCAL’S TRIANGLE

PROGRAM:

#include<stdio.h>
#include<conio.h>
main()
{
int i,j,l,m,n;
clrscr();
printf("How many rows?\n");
scanf("%d",&n);
printf("\n\t\t\t\t Pascal’s Triangle\n");
m=1;
for(l=0;l<n;l++)
{
for(i=40-3*l;i>0;i--)
printf(" ");
for(j=0;j<=l;j++)
{
if((j==0)||(l==0))
m=1;
else
m=(m*(l-j+1))/j;
printf("%6d",m);
}
printf("\n");
}
getch();
}

OUTPUT:-
How many rows?
5 Pascal’s Triangle

1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
AMSTRONG NUMBER

#include <stdio.h>
#include<conio.h>
int main()
{
int n, n1, rem, num=0;
printf("Enter a positive integer: ");
scanf("%d", &n);
n1=n;
while(n1!=0)
{
rem=n1%10;
num+=rem*rem*rem;
n1/=10;
}
if(num==n)
printf("%d is an Armstrong number.",n);
else
printf("%d is not an Armstrong number.",n);
getch();
return 0;
}

SAMPLE OUTPUT:

Enter a positive integer: 371


371 is an Armstrong number.
LEAP YEAR OR NOT

PROGRAM

#include <stdio.h>
int main()
{
int year;
printf("Enter a year: ");
scanf("%d",&year);
if(year%4 == 0)
{
if( year%100 == 0) /* Checking for a century year */
{
if ( year%400 == 0)
printf("%d is a leap year.", year);
else
printf("%d is not a leap year.", year);
}
else
printf("%d is a leap year.", year );
}
else
printf("%d is not a leap year.", year);
return 0;
}

SAMPLE OUTPUT

Enter year: 2000


2000 is a leap year.
SIMPLE CALCULATOR

PROGRAM

# include <stdio.h>
int main()
{
char operator;
float num1,num2;
printf("Enter operator either + or - or * or divide :
scanf("%c",&operator);
printf("Enter two operands: ");
scanf("%f%f",&num1,&num2);
switch(operator)
{
case '+':
printf("num1+num2=%.2f",num1+num2);
break;
case '-':
printf("num1-num2=%.2f",num1-num2);
break;
case '*':
printf("num1*num2=%.2f",num1*num2);
break;
case '/':
printf("num2/num1 = %.2f",num1/num2);
break;
default: /* If operator is other than +, -, * or /, error message is shown */
printf("Error! operator is not correct");
break;
}
getch();
return 0;
}

SAMPLE OUTPUT

Enter operator either + or - or * or divide: /


Enter two operands:
13.456
4.56
num2/num1 = 2.95

You might also like