CP Lab Programs Weekwise
CP Lab Programs Weekwise
2. Fibonacci series
/* program to generate fibonacci sequence */
#include <stdio.h>
#include <conio.h>
void main()
{
int n, i, f0,f1,f2;
clrscr();
printf("enter n \n");
scanf("%d",&n);
f0=0; /* first term is zero */
f1=1; /* second term is 1 */
printf("\n Fibonacci sequence \n");
printf("\n %d",f0);
printf("\n %d",f1);
for(i=3;i<=n;i++)
{
f2=f0+f1;/* subsequent number is sum of privious two numbers */
printf("\n %d",f2);
f0=f1;
f1=f2;
}
getch();
}
3. Prime numbers
/* program to generate prime numbers */
#include <stdio.h>
#include <conio.h>
void main()
{
int no,i,j,count;
printf("<---------------PRIME NO. SERIES-------------------->");
printf("\n\n\n\t\t\tINPUT THE VALUE OF N: ");
scanf("%d",&no);
printf("\n\nTHE PRIME NO. SERIES B/W 1 TO %d : \n\n",no);
for(i = 1; i <= no; i++)
{
count = 0;
//THIS LOOP WILL CHECK A NO TO BE PRIME NO. OR NOT.
for(j =1; j<=no ; j++)
{
if(i%j == 0)
count++;
}
if(count == 2)
printf("%3d\t",i);
}
getch();
}
4. Sum series
/* program find sum of series */
#include <stdio.h>
#include <conio.h>
#include <math.h>
void main()
{
int counter, f;
float sum=0,x,power,fact;
clrscr();
printf("<------PROGRAM FOR SUM OF EQ. SERIES------ ------->");
printf("\n\n\tEQUATION SERIES : 1- X^2/2! + X^4/4! - X^6/6! + X^8/8! X^10/10!");
printf("\n\n\n\tENTER VALUE OF X : ");
scanf("%f",&x);
for(counter=0, power=0; power<=10; counter++,power=power+2)
{
fact=1;
//CALC FACTORIAL OF POWER VALUE
for(f=power; f>=1; f--)
fact *= f;
//EQ. FOR SUM SERIES
sum=sum+(pow(-1,counter)*(pow(x,power)/fact));
}
printf("SUM : %f",sum);
getch();
}
if(d<0)
printf("\n Roots are imaginary");
else if(d==0)
{
printf("\n Roots are real and equal");
root1=(-b)/(2.0*a);
root2=root1;
printf("\n*****ROOTS ARE*****\n");
printf("\n root1=%f\n root2=%f",root1,root2);
}
else if(d>0)
{
printf("\n Roots are real and distinct");
root1=-b+sqrt(b*b-4*a*c)/2*a;
root2=-b-sqrt(b*b-4*a*c)/2*a;
printf("\n*****ROOTS ARE*****\n");
printf("\n root1=%f\n root2=%f",root1,root2);
}
getch();
}
dist=(vel*time)+0.5*acc*pow(time,2);
printf("\nThe distance travelled by vehicle at interval %d is %f",i,dist);
totdist= totdist+dist;
}
Printf("The total distance travelled by vehicle in the specified interval time is
%f",total_dist);
getch();
}
break;
case '%':
printf("%.1f % %.1f = %.1f",num1, num2, num1%num2);
break;
default:
/* If operator is other than +, -, * or /, error message is shown */
printf("Error! operator is not correct");
break;
}
getch();
}
}
long int NRfact(int b)
{
int factn=1,i;
if(b==0)
return 1;
else
for(i=1;i<=b;i++)
{
factn=factn*i;
}
return factn;
}
long int Rfact(int a)
{
if(a==0)
return 1;
else
return a*Rfact(a-1);
}
}
/* Recursive Function*/
int RGcd(int a, int b)
{
if(b>a)
return RGcd(b,a);
if(b==0)
return a;
else
return RGcd(b,a%b);
}
10. Largest and smallest from a given list
/* program to find largest and smallest from a given list of integers */
#include<stdio.h>
#include<conio.h>
void main()
{
int A[50],size,i,large,small;
clrscr();
printf("\n Enter the size of the array: ");
scanf("%d",&size);
printf("\n Enter %d elements in to the array: ", size);
for(i=1;i<=size;i++)
scanf("%d",&A[i]);
large=A[1];
for(i=1;i<=size;i++)
{
if(large<A[i])
large=A[i];
}
printf("\n Largest element: %d",large);
small=A[1];
for(i=1;i<=size;i++)
{
if(small>A[i])
small=A[i];
}
printf("\n Smallest element: %d",small);
getch();
}
11. Matrix addition and multiplication using functions