Professional Documents
Culture Documents
CP Labprorams
CP Labprorams
Week 1
a. To find the sum of individual digits of a positive integer
b. To generate the first n terms of the Fibonacci sequence
c. To generate all the prime numbers between 1 and n
Week 2
a. To calculate the Sum=1- X2/2! + X4/4! – X6/6! + X8/8! – X10/10!
b. To find the roots of the Quadratic equation
Week 3
a. i) To find the factorial of a given integer using Recursive and Non-
Recursive
Functions
ii) To find the GCD (greatest common divisor) of two given integers
• Using Non-Recursive
• Using Recursive
iii) To Solve Towers of Hanoi Problem
Week 4
a. To find the distance traveled at regular intervals of given ‘u’ and ‘a’
b. To perform the operation on two integer operands using an operator
Week 5
a. To find both the largest and smallest number in a list of integers
b.
i) Addition of Two Matrices
ii) Multiplication of Two Matrices
Week 6
a. Using functions
i) To insert a sub-string in to given main string from a
given position
ii) To delete n characters from a given position in a given
string
b. To determine if the given string is a palindrome or not
Week 7
a. To display the position or index in the string S where the string T
begins
b. To count the lines, words and characters in a given text
Week 8
a. To generate Pascal’s triangle
b. To construct a pyramid of numbers
Week 9
• To compute the sum of this geometric expression by reading x and n
1+x+x2+x3+..............................+xn
Week 10
a. To find the 2’s complement of a binary number
b. To convert a Roman numeral to its decimal equivalent
Week 11
• Using functions
i) Reading a complex number
ii) Writing a complex number
iii) Addition of two complex numbers
iv) Multiplication of two complex numbers
Week 12
a. To copy one file to another
b. To reverse the first n characters in a file
Week 13
• On Singly linked list
i) Creation ii) Insertion iii) Deletion iv) Traversal
Week 14
• On doubly linked list
i) Creation ii) Insertion iii) Deletion iv) Traversal in both ways
Week 15
• Implementing Stack(its operations) using
i) Arrays ii) Pointers
Week 16
• Implementing Queue(its operations) using
i) Arrays ii) Pointers
Week 17
• Using Stack operations
i) Converting infix expression into postfix expression
ii) Evaluating the postfix expression
Week 18
• Using functions
i) Creating a binary Tree of integers
ii) Traversing the above binary tree in preorder, in order and post
order
Week 19
i) Linear search
• Using Non-Recursive function
• Using Recursive function
ii) Binary search
• Using Non-Recursive function
• Using Recursive function
Week 20
i) Bubble sort
ii) Quick sort
Week 21
i) Insertion sort
ii) Merge sort
Week 22
• Implementing the Lagrange interpolation and Newton-Gregory
forward interpolation
Week 23
• Implementing the Linear regression and polynomial regression
Week 24
• Implementing Trapezoidal and Simpson methods
Write a C program to find the sum of individual digits of a
positive integer
ALGORITHM:
Step 1:
Start
Step 2:
Accept i, num, sum=0, rem, n, s=0, number
Step 3:
Read the number
Step 4:
Store the number in num
Step 5:
while(num>0)
begin
rem=num%10;
sum=sum+rem;
num=num/10;
end
Step 6: Assign nsum
Step 7: if(sum>10)
begin
while(sum>0)
begin
rem=sum%10;
s=s+rem;
sum=sum/10;
end
Write s
end
else
write sum
Step 8: Stop
FLOWCHART
PROGRAM
#include<stdio.h>
#include<conio.h>
void main()
{
int i,num,sum=0,rem,n,s=0,number;
clrscr();
printf("Enter any positive integer:");
scanf("%d",&num);
number=num;
while(num>0)
{
rem=num%10;
sum=sum+rem;
num=num/10;
}
n=sum;
if(sum>10)
{
while(sum>0)
{
rem=sum%10;
s=s+rem;
sum=sum/10;
}
printf("sum of individual digits of %d is %d which is again %d",number,n,s);
}
else
printf("sum of individual digits of %d is %d",number,sum);
getch();
}
OUTPUT:
Enter any positive integer: 145
sum of individual digits of 145 is 10 which is again 1
Step 1:
Start
Step 2:
Declare first=0,second=1,third,n,i
Step 3:
Read the no. of terms(n)
Step 4:
for(i=1;i<=n-2;i++)
begin
thirdfirst+second;
Write third
Firstsecond;
Secondthird;
end
Step 5: Stop
FLOWCHART
PROGRAM
OUTPUT:
Enter the no. of terms: 10
The fibonacci sequence upto 10 terms are:
0 1 1 2 3 5 8 13 21 34
ALGORITHM:
Step 1: Start
Step 2: Declare i, j, n, count
Step 3: Read the number(n)
Step 4: for(i=1;i<=n;i++)
begin
count=0;
for(j=1;j<=i;j++)
begin
if(i%j==0)
count++;
end
end
if(count<=2)
Print i
Step 5: Stop
FLOWCHART
PROGRAM
FLOWCHART
PROGRAM
PROGRAM
int rec_fact(int n)
{
return (n>=1) ? n* rec_fact(n-1) : 1;
}
int nonrec_fact(int n)
{
int fact=1;
int i;
for(i=1;i<=n;i++)
{
fact*=i;
}
return fact;
}
OUTPUT
Enter any integer: 0
Factorial of 0 is 1
PROGRAM
PROGRAM
AIM: To print the all the moves to solve the Towers of Hanoi problem
Solution:
Let
Disk A Disk B Disk C Disk D
PROGRAM
ALGORITHM
Step 1: Start
Step 2: Declare u,t,a,ch,s variables
Step 3: Read u and a values
Step 4: Read the ch value (time Range chosen)
Step 5: switch(ch)
Step 6:
if ch==1 then
Read t (time value in range 0-40)
break;
if ch==2 then
Read t (time value in range 41-80)
break;
if ch==3 then
Read t (time value in range 81-120)
break;
if ch==4 then
Read t (time value in range 121-160)
break;
if ch==5 then
Read t (time value in range 161-200)
break;
else
Write “Enter value from 1 to 5 only"
exit(0);
Step 7: s=u*t + (o.5*a*t*t)
Step 8: Write s
Step 9: Write “Do you want to continue?(y or n)”
Step 10: Read ch1 (answer)
Step 11: if ch1 is ‘y’ or ‘Y’
goto step 3
else
goto step 12
Step 12: Stop
FLOW CHART
PROGRAM
/* s=ut+½at2 */
#include<stdio.h>
#include<conio.h>
void main()
{ int u,t,a,ch; double s; char ch1; clrscr();
do
{
printf("\nEnter values of u and a:");
scanf("%d%d",&u,&a);
printf("\nChoose the time range:\n");
printf("1. 0-40 \n2. 41-80 \n3.81-120 \n4.121-160 \n5.161-200 \n");
scanf("%d",&ch);
switch(ch)
{
case 1: printf("\nEnter time value in range 0-40: ");
scanf("%d",&t); break;
case 2: printf("\nEnter time value in range 41-80: ");
scanf("%d",&t); break;
case 3: printf("\nEnter time value in range 81-120: ");
scanf("%d",&t); break;
case 4: printf("\nEnter time value in range 121-160: ");
scanf("%d",&t); break;
case 5: printf("\nEnter time value in range 161-200: ");
scanf("%d",&t); break;
default: printf("\nEnter value from 1 to 5 only");
exit(0);
}
s=u*t+(0.5*a*t*t);
printf("\nDistance in mts: %lf mts",s);
printf("\nDo u want to change values of 'u' and 'a'? ('Y' or 'N'):");
fflush(stdin);
scanf("%c",&ch1);
}while(ch1=='y'||ch1=='Y');
getch();
}
OUTPUT:
Enter values of u and a: 4 2
Choose the time range:
1. 0-40
2. 41-80
3. 81-120
4. 121-160
5. 161-200
2
Enter time value in the range of 41-80: 50
Distance in mts: 2700.000000 mts
Do u want to change values of ‘u’ and ‘a’? (‘Y’ or ‘N’): N
PROGRAM
/* C program which takes two integer operands and one operator
from the user, performs the operation and then prints the result */
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b; float res; char op,ans; clrscr();
printf("\nEnter any two numbers: ");
scanf("%d%d",&a,&b);
do
{
printf("\nEnter any operator(+,-,*,/,%): ");
fflush(stdin);
scanf("%c",&op);
switch(op)
{
case '+':
res=a+b;
break;
case '-':
res=a-b;
break;
case '*':
res=a*b;
break;
case '/':
res=a/b;
break;
case '%':
res=a%b;
break;
default:
printf("\nWrong selection of operator");
exit(0);
}
printf("%f",res);
printf("\nDo u want to select another operator?('y' or 'n'): ");
fflush(stdin);
scanf("%c",&ans);
}while(ans=='Y'||ans=='y');
getch();
}
OUTPUT:
Enter any two numbers: 25 7
Enter any operator(+,-,*,/,%): *
175.000000
Do u want to select another operator?(‘y’ or ‘n’): n
ALGORITHM
Step 1: Start
Step 2: Declare n,a[10],i,max,min variables
Step 3: Assign max=0
Step 4: Read the n (no. of integers to be entered) values
Step 5: for(i=0;i<n;i++)
a[i] Read n integers
Step 6: for(i=0;i<n;i++)
begin
if(a[i]>max)
maxa[i];
end
Step 7: min max
Step 8: for(i=0;i<n;i++)
begin
if(a[i]<min)
min=a[i];
end
Step 9: Write max and min
Step 10: Stop
FLOW CHART
PROGRAM
PROGRAM
OUTPUT:
Enter No. of rows and columns: 2 2
Enter 4 elements for the first 2X2 matrix: 1 2 3 4
Enter 4 elements for the second 2X2 matrix: 1 2 3 4
First Matrix is:
12
34
Second Matrix is:
12
34
Sum of first and second matrices is:
24
68
Write a C program to find the multiplication of two matrices
AIM: To calculate the multiplication of two matrices and to print the result
ALGORITHM
Step 1: Start
Step 2: Declare a[10][10],b[10][10],c[10][10],i,m1,m2,j,n1,n2,k variables
Step 3: Read m1 and n1values (no. of rows and columns for the first matrix)
Step 4: for(i=0;i<m1;i++)
begin
for(j=0;j<n1;j++)
Read a[i][j] values (elements for the first matrix)
end
Step 5: Read m2 and n2 values (no. of rows and columns for the second matrix)
Step 6: if m1 equals n2 then
for(i=0;i<m2;i++)
begin
for(j=0;j<n2;j++)
Read b[i][j] values (elements for the second matrix)
end
Step 7: for(i=0;i<m1;i++)
begin
for(j=0;j<n2;j++)
begin
c[i][j]=0;
for(k=0;k<n1;k++)
begin
c[i][j]+=a[i][k]*b[k][j];
end
end
end
Step 8: if m1 not equals n2 then write no. of rows in the first matrix
must be same as the no. of columns in the second matrix
Step 9: Stop
FLOW CHART
PROGRAM
OUTPUT:
Enter No. of rows and columns: 2 2
Enter 4 elements for the first 2X2 matrix: 1 2 3 4
Enter 4 elements for the second 2X2 matrix: 1 2 3 4
First Matrix is:
12
34
Second Matrix is:
12
34
Sum of first and second matrices is:
24
68
Product of first and second matrices is:
7 10
15 22
PROGRAM
PROGRAM
/* To delete n characters from a given position in a given string */
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char mstr[80],str[80];
int i,j=0,k,n;
clrscr();
printf("\nEnter a string: ");
gets(mstr);
printf("\nEnter starting position number of the deletion: ");
scanf("%d",&k);
printf("\nEnter number of characters to be deleted: ");
scanf("%d",&n);
for(i=0;i<k-1;i++)
str[j++]=mstr[i];
for(i=k+n-1;mstr[i]!='\0';i++)
str[j++]=mstr[i];
str[j]='\0';
printf("\nString after deletion: %s",str);
getch();
}
PROGRAM
PROGRAM
/* Searching for a given string and displaying position */
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char s[20],t[20];
int len1,len2,p;
clrscr();
printf("\nEnter the main string: ");
gets(s);
len1=strlen(s);
printf("\nEnter the search string: ");
gets(t);
len2=strlen(t);
p=findpos(len1,len2,s,t);
if(p!=-1)
printf("\nThe string %s is found at position %d",t,p);
else
printf("\nThe string %s is not found in the main string %s",t,s);
getch();
}
int findpos(int l1,int l2, char s1[],char t1[])
{
int i,j=0,flag=0;
for(i=0;i<l1&&j<l2;i++)
{
if(s1[i]!=t1[j])
flag=1;
else
{
flag=0;
j++;
}
}
if(flag==0)
return(i-l2+1);
else
return(-1);
}
PROGRAM
/* To count the number of lines, words, and characters in a given text */
#include<stdio.h>
#include<conio.h>
void main()
{
char text[100],ch;
int i,c,sp,nchar=0,wds=0,lines=0;
clrscr();
printf("\nEnter the text: ");
while(1)
{
c=0;
while((ch=getchar())!='\n')
{
text[c]=ch;
c++;
}
text[c]='\0';
if(text[0]=='\0')
break;
else
{
wds++;
sp=0;
for(i=0;text[i]!='\0';i++)
if(text[i]==' '||text[i]=='\t')
{
sp++;
wds++;
}
}
lines++;
nchar=nchar+strlen(text)-sp;
}
printf("\nNo. of lines=%d",lines);
printf("\nNo. of words=%d",wds);
printf("\nNo. of characters=%d",nchar);
getch();
}
PROGRAM
/* To generate pascal's triangle */
#include<stdio.h>
#include<conio.h>
void main()
{
int a,i,j,k,n;
clrscr();
printf("\nEnter no. of rows: ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
for(k=1;k<n-i;k++)
printf(" ");
for(j=0;j<=i;j++)
{
if((j==0)||(i==0))
a=1;
else
a=((a*(i-j+1))/j);
printf("%4d",a);
}
printf("\n");
}
getch();
}
PROGRAM
/* program to generate pyramid of numbers */
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,k,n;
clrscr();
printf("\nEnter no. of rows: ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(k=1;k<=n-i;k++)
printf(" ");
for(j=1;j<=i;j++)
printf("%4d",i);
printf("\n");
}
getch();
}
PROGRAM
/* program for geometric progression */
#include<stdio.h>
#include<conio.h>
void main()
{
int x,n,y,i,sum;
clrscr();
label1:
printf("\nEnter the values of x and n: ");
scanf("%d%d",&x,&n);
if(x<0||n<0)
{
printf("\n x and n cannot be negative,enter again");
goto label1;
}
else
{
sum=1;
y=1;
for(i=0;i<n;i++)
{
y=y*x;
sum=sum+y;
}
}
printf("x=%d n=%d sum=%d",x,n,sum);
getch();
}