0% found this document useful (0 votes)
3 views

page2

The document contains a series of practical coding exercises in C programming, each addressing different mathematical problems such as sorting numbers, displaying squares and cubes, generating arithmetic and geometric progressions, and calculating Fibonacci and Tribonacci sequences. Each exercise includes the coding solution and expected output. The document also covers approximating values of π, e^x, and sin(x)/cos(x) using Taylor series.

Uploaded by

Shravan Ghasti
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

page2

The document contains a series of practical coding exercises in C programming, each addressing different mathematical problems such as sorting numbers, displaying squares and cubes, generating arithmetic and geometric progressions, and calculating Fibonacci and Tribonacci sequences. Each exercise includes the coding solution and expected output. The document also covers approximating values of π, e^x, and sin(x)/cos(x) using Taylor series.

Uploaded by

Shravan Ghasti
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 21

Practical No :- 2

Date:

Q.1]Display three input number in sorted(non-decreasing) order?

CODING :-
#include<stdio.h>

#include<conio.h>

void main()

int a,b,c;

clrscr();

printf("enter 3 number to sort:");

scanf("%d %d %d",&a,&b,&c);

printf("\n----------------------------------");

printf("\n original entered number is: %d,%d,%d",a,b,c);

printf("\n sorted number:");

if(a<=b && b<=c)

printf("%d,%d,%d",a,b,c);

else

if(a<=c && c<=b)

printf("%d,%d,%d",a,c,b);

}
else

if(b<=a && a<=c)

printf("%d,%d,%d",b,a,c);

else

if(b<=c && c<=a)

printf("%d,%d,%d",b,c,a);

else

if(c<=a && a<=b)

printf("%d,%d,%d",c,a,b);

else

if(a==b && b==c)

printf("%d,%d,%d",a,b,c);

else

{
printf("%d,%d,%d",c,b,a);

getch();

OUTPUT :-
Practical No :- 3
Date:

Q.1]Given a positive integer value n(>=0) display number, square


and cube of number from 1 to n in a tabular format?
CODING :-
#include<stdio.h>

#include<conio.h>

void main()

int n,i;

clrscr();

printf("input number of lines:");

scanf("%d",&n);

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

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

getch();

OUTPUT :-
Practical No :- 4
Date:
Q.1]Given an input positive integer number ,display odd numbers
from in the range[1,n]?
CODING :-
#include<stdio.h>
#include<conio.h>
void main()
{
int i,n;
clrscr();
printf("enter nth number:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
if(i%2==1)
{
printf("\n odd number is: %d",i);
}
}
getch();
}

OUTPUT :-
Practical No :- 5
Date:

Q.1]Display first mathematical tables, each table upto 10 rows?

Generalise this to display first n(>0) mathematical tables upto


m(m>0) rows>?
CODING :-
#include<stdio.h>

#include<conio.h>

void main()

int i,n,m;

clrscr();

printf("enter first multiplication table:");

scanf("%d",&n);

printf("enter upto multiplication table:");

scanf("%d",&m);

for(n;n<=m;n++)

printf("\n\n table of %d",n);

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

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

}
}

getch();

OUTPUT :-
Practical No :- 8
Date:
Q.1]Given the first term(a), difference/multiplier(d) and number of
terms (n>0), display the first n terms of the arithmetic/ geometric
progression?
A]Arithmetic progression
CODING :-
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,d,i,n;
clrscr();
printf("enter first two terms of AP\n");
scanf("%d %d",&a,&b);
printf("enter number of term for summation\n");
scanf("%d",&n);
d=b-a;
printf("AP is \n");
for(i=1;i<=n;i++)
{
if(i<n)
{
printf("%d ",a);
a=a+d;
}
else
{
printf("%d",a);
}
}
getch();
}

OUTPUT :-
B]Geometric progression

CODING :-
#include<stdio.h>

#include<conio.h>

void main()

int a,b,d,i,n;

clrscr();

printf("enter first two terms of GP\n");

scanf("%d %d",&a,&b);

d=b-a;

printf("\n Difference:%d",d);

printf("\n enter number of term for summation:");

scanf("%d",&n);

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

printf(" %d\t",a);

a*=d;

getch();

OUTPUT :-
Practical No :- 9
Date:
Q.1]Display the first n(n>0) terms of the fibonacci sequence?
CODING :-
#include<stdio.h>
#include<conio.h>
void main()
{
int i,n,t1,t2,nextterm;
clrscr();
t1=0 ;
t2=1;
nextterm=t1+t2;
printf("enter the number of terms:");
scanf("%d",&n);
printf("\n fibonacci series:%d,%d",t1,t2);
for(i=3;i<=n;++i)
{
printf(",%d",nextterm);
t1=t2;
t2=nextterm;
nextterm=t1+t2;
}
getch();
}
OUTPUT :-
Practical No :- 10
Date:

Q.1]Display the first n(n>0) terms of the Tribonacci sequence?

CODING :-
#include<stdio.h>

#include<conio.h>

void main()

int i,n,t1,t2,t3,nextterm;

clrscr();

t1=0;

t2=0;

t3=1;

nextterm=t1+t2+t3;

printf("enter the number of terms:");

scanf("%d",&n);

printf("tribonacci series:%d,%d,%d",t1,t2,t3);

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

printf(",%d",nextterm);

t1=t2;

t2=t3;

t3=nextterm;

nextterm=t1+t2+t3;
}

getch();

OUTPUT :-
Practical No :- 11
Date:
Q.1]Given two positive integer numbers n1 and n2 check if the
numbers are consecutive numbers of the Fibonacci sequence?
CODING :-
#include<stdio.h>

#include<conio.h>

void main()

int a,b,c,next,num,i;

clrscr();

printf("enter a number:");

scanf("%d",&num);

if(num>0)

printf("number is positive");

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

a=0;

b=1;

c=a+b;

while(c<i)

a=b;

b=c;

c=a+b;
}

if(c==i)

printf("\n number %d is a fibonacci term",i);

else

printf("\n number %d is not a fibonacci term",i);

else

printf("number is negative");

getch();

OUTPUT :-
Practical No :- 12
Date:

Q.1]Compute approximate value of π considering first n(n>0) terms

of the Taylor serises for π?

CODING :-
#include<stdio.h>

#include<conio.h>

#include<math.h>

double compute_pi(int n)

double pi=0.0;

int i;

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

long double term=((i%2==0)?-1:1)/(2*i+1.0L);

pi+=term;

return 4*pi;

void main()

int n;

double pi;

clrscr();
printf("enter the number of terms(n>0):");

scanf("%d",&n);

if(n<=0)

printf("error: n must be greater than 0.\n");

else

pi=compute_pi(n);

printf("approximate value of pi using first %d terms:%.10f\n",n,-pi);

getch();

OUTPUT :-
Practical No :- 13
Date:
Q.1]Compute approximate value of ex considering first n(n>0) terms
of the Taylor series for ex ?
CODING :-
#include<stdio.h>

#include<conio.h>

#include<math.h>

long long factorial(int n)

int i;

long long fact =1;

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

fact *= i;

return fact;

double compute_e_to_x(double x,int n)

double result=0.0;

int i;

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

result += pow(x,i)/factorial(i);

}
return result;

void main()

int n;

double x;

clrscr();

printf("enter the value of x : ");

scanf("%lf",&x);

printf("enter the number of terms (n>0) : ");

scanf("%d",&n);

if(n<=0)

printf("error : n must be greater then 0. \n");

else

double result = compute_e_to_x(x,n);

printf("approximate value of e^%.2f using first %d terms : %.10f \


n",x,n,result);

getch();

OUTPUT :-
Practical No :- 14
Date:
Q.1]Compute approximate value of sin(x)/cos(x) considering first n(n>0)
terms of the Taylor series for sin(x)/cos(x)?
CODING :-
#include<stdio.h>

#include<conio.h>

#include<math.h>

long long factorial(int n)

int i;

long long fact =1;

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

fact *= i;

return fact;

double compute_sin_div_cos(double x,int n)

double sin_x=0.0;

double cos_x=1.0;

int i;

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

sin_x += ((i%2==1)? 1 : -1)*pow(x,2*i-1)/factorial(2*i-1);


cos_x += ((i%2==1)? -1 : 1)*pow(x, 2*i)/ factorial(2*i);

return sin_x / cos_x;

void main()

int n;

double x;

clrscr();

printf("enter the value of x : ");

scanf("%lf",&x);

printf("enter the number of terms (n>0) : ");

scanf("%d",&n);

if(n<=0)

printf("error : n must be greater then 0. \n");

else

double result = compute_sin_div_cos(x,n);

printf("approximate value of sin(%.2f)/cos(%.2f) using first %d terms : %.10f \n",x,x,n,result);

getch();

OUTPUT :-

You might also like