You are on page 1of 6

Lab Tasks Programming Fundamentals Course Supervisor: Syeda Nazia Ashraf

Lab # 09

Student’s Tasks:

1) The program below shows the result for math and trigonometric functions. The
functions pass the values to variables which are further used for printing in printf.

Source Code:
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main(){
float a=45,b=1,sn,cs,tn,snh,csh,tnh;
clrscr();
sn=sin(a);
cs=cos(a);
tn=tan(a);
snh=sinh(b);
csh=cosh(b);
tnh=tanh(b);
printf("\n\n\n Trignometric Functions");
printf("\nsin 45 = %.2f",sn);
printf("\ncos 45 = %.2f",cs);
printf("\ntan 45 = %.2f",tn);
printf("\n\n\n Hyperbolic Functions");
printf("\nsinh 1 = %.2f",snh);
printf("\ncosh 1 = %.2f",csh);
printf("\ntanh 1 = %.2f",tnh);
getch();

}
Output:
Lab Tasks Programming Fundamentals Course Supervisor: Syeda Nazia Ashraf

2) The program below shows the result for math and trigonometric functions. It also
demonstrates that some functions may be called within the body of another function.
For example here all the trigonometric functions are called inside printf function.

Source Code:
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main(void)
{
clrscr();
printf("\n\n\n Trignometric Functions");
printf("\nsin 45 = %.2f",sin(45));
printf("\ncos 45 = %.2f",cos(45));
printf("\ntan 45 = %.2f",tan(45));
printf("\n\n\n Hyperbolic Functions");
printf("\nsinh 1 = %.2f",sinh(1));
printf("\ncosh 1 = %.2f",cosh(1));
printf("\ntanh 1 = %.2f",tanh(1));
printf("\n\n\n Math Functions");
printf("\npow 2,3 = %.2f",pow(2,3));
printf("\nsqrt 49 = %.2f",sqrt(49));
getch();
}

Output:
Lab Tasks Programming Fundamentals Course Supervisor: Syeda Nazia Ashraf

3) A 5 digit positive integer is entered through the keyboard, write a function to calculate
sum of digits of five digit number (without using recursion and using recursion).

Source Code: (Using Recursion)


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

void main()
{
int i,j;
clrscr();
printf("\nInput a 5 digit number: \n");
scanf("%d",&i);
j=sum(i);
printf("\nThe sum of the digits is: %d",j);
getch();
}

int sum(int k)
{
if(k==0)
return 0;
Lab Tasks Programming Fundamentals Course Supervisor: Syeda Nazia Ashraf

else
return(k%10+sum(k/10));
}

Output:

Source Code: (Without Recursion)


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

void main()
{
int i,j;
clrscr();
printf("\nInput a 5 digit number: \n");
scanf("%d",&i);
j=sum(i);
printf("\nThe sum of the digits is: %d",j);
getch();
}

int sum(int k)
{
int j =0;
while(k){
j += k%10;
k /= 10;
}
return j;
}

Output:
Lab Tasks Programming Fundamentals Course Supervisor: Syeda Nazia Ashraf

4) Write a C function to receive an integer of 3 digits then calculates and returns the sum
of the MSD(Most Significant Digit)-First Digit and the LSD(Least Significant Digit)-Last
Digit. For example: if your function received 345 it should return 8.

Source Code:
#include<stdio.h>
#include<conio.h>

void main()
{
clrscr();
sum();
getch();
}

int sum()
{
int n;
printf("Enter a 3 Digit Number: \n");
scanf("%d",&n);
if(n>=101&&n<=999){
printf("%d",(n/100)+(n%10));
}
else{
printf("Enter a Valid 3 digit number");
}
return n;
}

Output:

2
5) Write a program to find roots of Quadratic Equation. ax + bx + c = 0. Take input
a,b,c and apply quadratic formula for the roots of the general quadratic equation:

Source Code:
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
clrscr();
quad();
Lab Tasks Programming Fundamentals Course Supervisor: Syeda Nazia Ashraf

getch();
}
quad()
{
int a,b,c,r;
float x,y;
printf("Enter coefficient of x^2, x and constant term: \n");
scanf("%d %d %d",&a,&b,&c);
r = b*b-4*a*c;
if(r<0){
printf("Both roots are imaginary \n");
}
if(r==0){
printf("Both roots are equal \n");
x = -b/(2.0*a);
printf("Root is %f",x);
}
if(r>0){
printf("Roots are real and Distinct \n");
x=(-b+sqrt(r))/(2*a);
y=(-b-sqrt(r))/(2*a);
printf("\n Roots are: %.3f, %.3f",x,y);
}
}

Output:

You might also like