0% found this document useful (0 votes)
71 views12 pages

CP Lab Programs Weekwise

The document provides a list of 39 computer programming lab programs organized weekwise. Some of the key programs included are: 1. Programs to calculate sums of digit numbers, Fibonacci series, prime numbers, roots of quadratic equations, distance traveled, and arithmetic calculations. 2. Programs involving strings like inserting, deleting characters, and checking palindromes. 3. Programs for file handling tasks like copying, reversing characters, and merging files. 4. Sorting and searching algorithms like linear search, binary search, selection sort, and bubble sort. 5. Linked lists, stacks, queues and interpolation methods for numerical analysis.

Uploaded by

Ashok Oruganti
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
71 views12 pages

CP Lab Programs Weekwise

The document provides a list of 39 computer programming lab programs organized weekwise. Some of the key programs included are: 1. Programs to calculate sums of digit numbers, Fibonacci series, prime numbers, roots of quadratic equations, distance traveled, and arithmetic calculations. 2. Programs involving strings like inserting, deleting characters, and checking palindromes. 3. Programs for file handling tasks like copying, reversing characters, and merging files. 4. Sorting and searching algorithms like linear search, binary search, selection sort, and bubble sort. 5. Linked lists, stacks, queues and interpolation methods for numerical analysis.

Uploaded by

Ashok Oruganti
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

CP Lab Programs Weekwise

1. Sum of individual digits of a number


2. Fibonacci series
3. Prime Numbers
4. Sum series
5. Quadratic roots
6. Distance travelled by vehicle
7. Arithmetic calculator
8. Factorial using recursion & non-recursion
9. GCD using recursion & non-recursion
10. Largest & smallest from given list
11. Matrix addition & multiplication using functions
12. Inserting a string
13. Deleting n char from a string
14. Palindrome of string
15. Searching a for string
16. Counting lines, words, chars
17. Pascals triangle
18. Pyramid of numbers
19. Geometric progression
20. 2s complement
21. Roman to decimal
22. Complex number arithmetic
23. Copy one file to another
24. Reverse first n chars from a file
25. Display contents of a file
26. Merge two files
27. Linear search
28. Binary search
29. Selection sort
30. Bubble sort
31. Single linked list
32. Stack using
33. Queue
34. linear regression algorithm.
35. polynomial regression algorithm.
36. Lagrange interpolation.
37. Newton- Gregory forward interpolation.
38. Trapezoidal method.
39. Simpson method.

1. Sum of individual digits of a number

/* SUM OF THE INDIVIDUAL DIGITS OF A NUMBER */


#include <stdio.h>
#include <conio.h>
void main()
{
int num,sum=0,rem;
clrscr();
HH:
printf("enter a number\n");
scanf("%d",&num);
if(num<=0)
{
printf("number is negative or zero");
goto HH;
}
while(num!=0)
{
rem=num % 10;
sum=sum+rem;
num=num/10;
}
printf(" Sum of the individual digits of number = %d",sum);
getch();
}

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();
}

5. Roots of quadratic equation


/* Program to find roots of quadratic equation */
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
float a,b,c,d,root1,root2;
clrscr();
printf("\n Enter values of a,b,c for finding roots of a quadratic eq:\n");
scanf("%f%f%f",&a,&b,&c);
/*checking condition*/
d=b*b-4*a*c;

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();
}

6. Distance travelled by vehicle


/* Total distance travelled by vehicle at regular intervals */
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int T,i,time;
float acc,dist,vel,totdist=0;
clrscr();
printf("\nEnter the total no.of intervals to calculate the distance value");
scanf("%d",&T);
for(i=1;i<=T;i++)
{
printf("\nEnter the time(sec) travelled at interval %d",i);
scanf("%d",&time);
printf("\nEnter the current velocity of vehicle at %d interval",i);
scanf("%d",&vel);
printf("\n Enter the acceleration of vehicle at %d interval",i);
scanf("%d",&acc);

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();
}

7. Arithmetic calculator using switch case


/* simple calculator sing switch...case statement in C programming. */
#include <stdio.h>
#include <conio.h>
void main()
{
char op;
float num1,num2;
clrscr();
printf("Enter operator either + or - or * or divide : ");
scanf("%c",&op);
printf("Enter two operands: ");
scanf("%f%f",&num1,&num2);
switch(op)
{
case '+':
printf("%.1f + %.1f = %.1f",num1, num2, num1+num2);
break;
case '-':
printf("%.1f - %.1f = %.1f",num1, num2, num1-num2);
break;
case '*':
printf("%.1f * %.1f = %.1f",num1, num2, num1*num2);
break;
case '/':
printf("%.1f / %.1f = %.1f",num1, num2, num1/num2);

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();
}

8. Factorial using recursive and non-recursive functions


/* program to find factorial of number using recursive and non-recursive functions */
#include <stdio.h>
#include <conio.h>
void main()
{
long int Rfact(int );
long int NRfact(int);
int n;
long int f1,f2;
clrscr();
printf("\n enter an integer no:");
scanf("%d",&n);
f1=NRfact(n);
printf(" factorial of %d = %ld \n", n, f1);
f2=Rfact(n);
printf("non Recursive Fact of %d=%ld",n,f2);
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);
}

9. GCD using recursive and non-recursive functions


/* program to find GCD of two integers using non-recursive & recursive functions */
#include<stdio.h>
#include<conio.h>
#include<math.h>
int RGcd(int a, int b);
int NRGcd(int a,int b);
void main()
{
int m,n,g1,g2;
clrscr();
printf("Enter the two numbers whose GCD is to be found: ");
scanf("%d%d",&m,&n);
g1=NRGcd(m,n)
printf("GCD of %d and %d Using Non-Recursive Function is %d\n",m,n,g1);
g2=RGcd(m,n);
printf("GCD of %d and %d Using Recursive Function is %d\n",m,n,g2));
getch();
}
/* Non-Recursive Function */
int NRGcd(int a,int b)
{
int r,gcd;
do
{
r=a%b;
if(r==0)
gcd=b;
a=b;
b=r;
}while(b!=0)
return gcd;

}
/* 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

You might also like