Documente Academic
Documente Profesional
Documente Cultură
a)Write a C program to find the sum of the individual digits of a positive integer.
Flow Chart:
start
Read n
n != 0
False
True
r = n%10
n=n/10
sum=sum+r
Write sum
stop
Algorithm:
Step 1: start
Step 2: read n
Step 3: initialize the sum=0
Step 4: if n<0 goto Step 7
Step 5: if n!=0 goto Step 6 else goto step 7
Step 6: store n%10 value in p
Add r value to sum
Assign n/10 value to n
Goto Step 5
Step 7: print the output
Step 8:stop
Program
#include<stdio.h>
#include<conio.h>
void main()
{
int n,sum=0,r;
clrscr();
printf("\nEnter the positive integer : ");
scanf("%d",&n);
if(n < 0)
printf("\nThe given number is not positive");
else
{
while( n != 0 )
{
r = n % 10;
n = n / 10;
sum = sum + r;
}
printf("\nSum of individual digits is : %d",sum);
}
getch();
}
Output:
Enter the positive integer : 123
Sum of individual digits is : 6
Read n
false
n==1 || n==2
true
Write a,b
Write a,b
false
for(i=2;i<n;i++)
true
c=a+b
;
a=b;
b=c;
Write c
stop
Algorithm:
Step 1: start
Step 2: initialize the a=0, b=1
Step 3: read n
Step 4: if n== 1 print a go to step 7. else goto step 5
Step 5: if n== 2 print a, b go to step 7 else print a,b
Step 6: initialize i=2
i) if i< n do as follows. If not goto step 7
c=a+b
a=b
b=c
print c
increment i value
goto step 6(i)
Step 7: stop
Program
#include<stdio.h>
#include<conio.h>
void main()
{
int a=0,b=1,c,n,i;
clrscr();
printf("\nEnter the value of n : ");
scanf("%d",&n);
if(n == 1 || n==2)
printf("\n%d %d",a,b);
else
{
printf("\n%d %d",a,b);
for(i = 2; i < n; i++)
{
c = a + b;
printf(" %d",c);
a = b;
b = c;
}
}
getch();
}
Output:
Enter the value of n : 5
01123
c)Write a C program to print all the prime numbers between 1 and n, where n is a value
supplied by the user.
start
Flow Chart:
Read n
for(i=1;i<=n;i++)
false
true
Count =
0
false
true
false
i%j ==0
true
false
Count
=count+1
count ==2
true
Write i
stop
Algorithm:
Step 1: start
Step 2: read n
Step 3: initialize i=1, count=0
Step 4: if i<=n goto step 5
if not goto step 10
Step 5: initialize j=1
Step 6: if j<=1 do as follow. If no goto step 7
i)if i%j==0 increment count
ii) increment j
iii) goto Step 6
Step 7: if count== 2 print i
Step 8: increment i
Step 9: goto step 4
Step 10: stop
Program
#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,count,j;
clrscr();
printf("\nEnter the number: ");
scanf("%d",&n);
printf("\nThe prime numbers below %d are :",n);
for(i = 1; i <= n; i++)
{
count = 0;
for(j = 1; j <= i; j++)
{
if(i % j == 0)
count++;
}
if( count == 2 )
printf("\t%d",i);
}
getch();
}
Output:
Enter the number: 11
The prime numbers below 11 are : 2 3 5 7 11
Week-2
a)Write a C program to find sum of following series: 1 - x2/2! + x4/4! - x6/6! + x8/8! x10/10!
Flow Chart:
start
Read x
false
For(i=0;i<=10;i=i+2)
true
fact=
false
For(j=1;j<=i;j++)
true
Fact = fact
s = s+((-1 * sign) * pow(x,i)
/ fact
Sign=-
false sign==-1
Write s
true
sign=
stop
Algorithm
Step 1: Start
Step 2: read x
Step 3: initialize i=0 with increment of 2
Step 4: If i<=10 do as follows else goto step 8
Fact=1
Step 5: Initialize j=1 with increment of 1
If j<=i do as follows else goto step 6
Fact=fact*j
Step 6: s = s+((-1 * sign) * pow(x,i) / fact
Step 7: if sign==-1 then
sign = 1
else sign=-1
goto step 3
Step 8: print s
Step 9: Stop
Program
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int x,i,j,sign=-1;
long int fact = 1;
double s = 0.00;
clrscr();
printf("Enter the value of x:\t");
scanf("%d",&x);
for(i=0; i <=10; i=i+2)
{
fact = 1;
for(j=1; j<=i; j++) /*for finding factorial*/
fact = fact * j;
s = s + ((-1*sign) * pow(x,i)) / fact;
if(sign == -1)
sign = 1;
else
sign = -1;
}
printf("\nThe result is: %lf",s);
getch();
}
Output:
Enter the value of x: 3
The result is: -0.991049
false
d >0
true
r1=-b+sqrt(b*b-4*a*c)/
(2*a)
r2=-b-sqrt(b*b-4*a*c)/
Write r1,r2
d == 0
false
true
r1=-b/
(2*a)
r2=-b-/
Write r1,r2
Roots imaginary
Stop
Algorithm:
Step 1: start
Step 2: read the a,b,c value
Step 3: if b*b-4ac>0 then
Root 1= (-b+ sqrt(b*b-4*a*c))/2*a
Root 2= (-b-sqrt(b*b-4*a*c))/2*a
Step 4: if b*b-4ac=0 then
Root1 = Root2 = -b/(2*a)
Step 5: Otherwise Print Imaginary roots. Goto step 7.
Step 6: print roots
Step 7: stop
Program
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
float a,b,c,r1,r2,d;
clrscr();
printf("\n Enter values of a,b,c: ");
scanf("%f %f %f",&a,&b,&c);
if(a == 0)
printf("Entered value of a should not be zero");
else
{
d = b*b - 4*a*c;
if(d > 0)
{
r1 = -b + sqrt(b*b-4*a*c)/(2*a);
r2 = -b - sqrt(b*b-4*a*c)/(2*a);
printf(" roots are real and unequal\n ");
printf("root1 = %f\n root2 = %f",r1,r2);
}
else
{
if(d == 0)
{
r1 = -b/(2*a);
r2 = -b/(2*a);
printf("\n roots are real and equal");
printf("\nroot1=%f\n root2=%f\n",r1,r2);
}
else
printf("Roots are imaginary ");
}
}
getch();
}
Output:
Enter values of a,b,c: 1 2 1
Roots are real and equal
Root1: -1.000000
Root2: -1.000000
10
Week-3
a)The total distance travelled by vehicle in 't' seconds is given bydistance= ut+1/2at^2 where
'u' and 'a' are the initial velocity(m/sec)and acceleration (m/sec^2).Write C program to find
the distance travelled at regular intervals of time given the values of 'u' and 'a'.The program
should provide the flexibility to the user to select his own time intervals and repeat the
calculation for different value of 'u'and'a'.
Flow Chart:
start
Read interval
for(counter=1;counter<=interval;counter++)
false
true
Read time,vel,accl
stop
Algorithm
Step 1: Start
Step 2: read interval
Step 3: initialize counter=1 with increment of 1
If counter<=interval do as follows else goto step 4
Read time,vel,accl
Dis = dis + (vel *time+(accl * pow(time,2))/2
Print dis
Step 4: Stop
11
Program
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int interval, counter,time;
float accl, dis=0, vel;
clrscr();
printf("\n\tEnter the number of time intervals: ");
scanf("%d",&interval);
for(counter = 1; counter <= interval; counter++)
{
printf("\n\tEnter time at T%d(sec) : ",counter);
scanf("%d",&time);
printf("\tEnter the velocity at %d sec(m/sec) : ",time);
scanf("%f",&vel);
printf("\tEnter the acceleration at %d sec(m/sec^2): ",time);
scanf("%f",&accl);
dis = dis + ( vel*time + (accl*pow(time,2))/2 );
printf("\nDistance travelled by vehicle in %d intervals of time is %f",counter,dis);
}
getch();
}
Output:
Enter the number of time intervals: 2
Enter time at T1(sec):12
Enter the velocity at 12 sec(m/sec) :23
Enter the acceleration at 12 sec(m/sec^2):2.3
Distance travelled by vehicle in 1 intervals of time is 441.600006
Enter time at T2(sec):20
Enter the velocity at 20 sec(m/sec) :23
Enter the acceleration at 20 sec(m/sec^2):3.1
Distance travelled by vehicle in 2 intervals of time is 1561.599976
12
b)Write a C program which takes two integer operands and one operator from the user,
performs the operation and then prints the result.
Program
#include<stdio.h>
#include<conio.h>
void main()
{
char op;
int a,b;
clrscr();
printf("Enter an arithmetic operator:");
scanf("%c",&op);
printf("\nEnter two operands:");
scanf("%d%d",&a,&b);
switch(op)
{
case '+':
printf("\nSum of %d & %d is %d", a, b, a+b);
break;
case '-':
printf("\nDifference of %d & %d is %d",a,b,a-b);
break;
case '*':
printf("\nProduct of %d & %d is %d",a,b,a*b);
break;
case '/':
printf("\nQuotient of %d & %d is %d",a,b,a/b);
break;
case '%':
printf("\nRemainder of %d & %d is %d",a,b,a%b);
break;
default:
printf("\nInvalid arithmetic operator");
break;
}
getch();
}
Output:
Enter an arithmetic operator: +
Enter two operands: 5 6
Sum of 5 & 6 is 11
13
Week-4
a)Write C programs that uses both recursive and non-recursive functions to find the factorial
of a given integer.
Flow Chart
start
Read n
Read ch
True
Ch >0
false
rec_fact(n)
nonrec_fact
(n)
stop
14
Program
#include<stdio.h>
#include<conio.h>
long int rec_fact(int);
long int nonrec_fact(int);
void main()
{
int n,ch;
long int fact;
clrscr();
printf("\n\n\nEnter the number to find factorial :");
scanf("%d",&n);
printf("\nEnter 1 for recursive OR 0 for non-recursive :");
scanf("%d",&ch);
if(ch)
fact = rec_fact(n);
else
fact = nonrec_fact(n);
printf("factorial of %d is %ld",n,fact);
getch();
}
long int non_recfact(int n)
{
long int f=1;
if((n==0)||(n==1))
return 1;
else
while(n >= 2)
{
f = f * n;
n = n - 1;
}
return f;
}
long int rec_fact(int n)
{
long int f;
if((n==0) || (n==1))
return (1);
else
f = n * rec_fact(n-1);
return f;
}
Output:
Enter the number to find factorial: 4
Enter 1 for recursive OR 0 for non-recursive : 1
factorial of 4 is 24
15
b)Write a C program that uses both recursive and non-recursive functions to find GCD
(Greatest Common Divisor) of two given integers.
Program
#include<stdio.h>
#include<conio.h>
int gcdrecursive(int,int);
int gcdnonrecursive(int,int);
void main()
{
int a,b,ch,gcd;
clrscr();
printf("Enter the two numbers whose GCD is to be found:");
scanf("%d%d",&a,&b);
printf("\nEnter 1 for recursive 0 for nonrecursive:");
scanf("%d",&ch);
if(ch)
gcd=gcdrecursive(a,b);
else
gcd=gcdnonrecursive(a,b);
printf("\nGCD of %d and %d is %d",a,b,gcd);
getch();
}
int gcdrecursive(int m,int n)
{
if(n > m)
return gcdrecursive(n,m);
if(n == 0)
return m;
else
return gcdrecursive(n,m%n);
}
int gcdnonrecursive(int m,int n)
{
int remainder;
do
{
remainder=m%n;
m=n;
n=remainder;
}while(remainder!=0);
return m;
}
Output:
Enter the two numbers whose GCD is to be found: 10 50
Enter 1 for recursive 0 for nonrecursive 1
GCD of 10 and 50 is 10
16
Week-5
a)Write a C program to find the largest number in a list of integers.
Flow Chart:
start
read n
for(i=0;i<n;i++)
false
true
read a[i]
max =
for(i=1;i<n;i++)
true
false
a[i]>max
true
max =
write max
stop
Algorithm
Step 1: start
Step 2: read n
Step 3: initialize i=0
Step 4: if i<n do as follows. If not goto step 5
Read a[i]
Increment i
Goto step 4
Step 5: max=a[0]
Step 6: initialize i=0
Step 7: if i<n do as follows. If not goto step 8
If a[i]>max
Assign max=a[i]
Increment i goto Step 7
Step 8: print max
Step 9: stop
17
false
Program
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10],i,n,max;
clrscr();
printf("\nEnter the array size:\n");
scanf("%d",&n);
printf("\nEnter the elements of array:\n");
for(i = 0;i < n;i++)
scanf("\n%d",&a[i]);
max = a[0];
for(i = 1; i < n; i++)
{
if( a[i] > max)
max = a[i];
}
printf("\nmaximum value is:%d\n",max);
getch();
}
Output:
Enter the array size: 5
Enter the elements of array: 5 2 6 9 4
maximum value is 9
18
21
Week-6
a)Write a C Program that uses functions to perform the following operations
i) To insert a sub-string into given main string from a given position
ii) To delete n characters from a given position in a given string.
Program
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <string.h>
void main()
{
char s1[100],s2[100];
int len,p,n,choice;
void insert(char *,char *,int);
void del(char *,int,int);
clrscr();
printf("1.Insert a substring");
printf("\n2.Delete n characters");
printf("\n3.EXIT");
printf("\nEnter your option : ");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("Enter First String : ");
fflush(stdin);
gets(s1);
len = strlen(s1);
printf("Enter the position where the sub-string has to be inserted: ");
scanf("%d",&p);
if(len < p)
printf("\nThe position is not present in the 1st string ");
else
{
printf("\nEnter the second string(sub-string) :");
fflush(stdin);
gets(s2);
insert(s1,s2,p);
}
break;
case 2:
printf("\nEnter the string :");
fflush(stdin);
gets(s1);
len = strlen(s1);
printf("\nEnter the position from where to delete :");
scanf("%d",&p);
printf("\nEnter the no. of characters to delete from %d position :",p);
scanf("%d",&n);
if(len <= p)
22
true
false
false
*p1 == *p2
true
p1 +
+
p2+
True
*p1 == *p2
false
stop
24
Algorithm
Step 1: Start
Step 2: read s1
Step 3: len = strlen(s1)
p1 = s1
P2=&s1[len-1]
Step 4: initialize i=0 with increment by 1
If i<len/2 do as follows else goto step 5
If *p1==*p2 then
P1++
P2++
Else goto step 4
Step 5: if *p1==*p2 then
Print palindrome
Else
Print not palindrome
Step 6: Stop
Program
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char s1[50],len,*p1=NULL,*p2=NULL;
int i;
clrscr();
printf("Enter the string :");
fflush(stdin);
gets(s1);
len = strlen(s1);
p1 = s1;
p2 = &s1[len-1];
for(i=0; i<len/2; i++)
{
if(*p1 == *p2)
{
p1++;
p2--;
}
}
if(*p1 == *p2)
printf("\nThe given string is palindrome");
else
printf("\nThe given string is not palindrome");
getch();
}
Output:
Enter the string: madam
The given string is palindrome
25
Week-7
a)Write a C program that displays the position or index in the string Swhere the string T
begins,or -1 if S doesnot contain T.
Flow Chart:
start
Read s, t
f=
strstr(s,t)
True
false
Write position
Write -1
stop
Algorithm
Step 1: start
Step 2: read the string and then displayed
Step 3: read the string to be searched and then displayed
Step 4: searching the string T in string S and then perform the following steps
i. found=strstr(S,T)
ii. if found print the second string is found in the first string at the
position. If not goto step 5
Step 5: print the -1
Step 6: stop
26
Program
#include<stdio.h>
#include<string.h>
#include<conio.h>
void main()
{
char s[30],t[20];
char *f;
clrscr();
puts("Enter the first string: ");
gets(s);
puts("Enter the string to be searched: ");
gets(t);
f=strstr(s,t);
if(f)
printf("SecondString is found in the FirstString at %d position.\n",f-s);
else
printf("-1");
getch();
}
Output:
Enter the first string: hello how
Enter the string to be searched: how
SecondString is found in the FirstString at 6 position
27
b)Write a C program to count the number of lines, words, and characters in a given text
Flow Chart:
start
While (1)
false
true
C=0
While (ch==getchar()!=\n)
false
true
Text[c]=c
h;
C=c+1;
Text[c]=\
0;
Text[0]==\0
true
false
Wds=wds
+1
Sp=0
For(i=0;text[i]!=\0;i++)
false
Text[i]== || text[i]==\t
false
true
Wds=wds
+1
Sp=sp+1
Lines=lines
+1
nchar=nchar+strlen(text)-sp
Algorithm:
Write lines,wds,nchar
28
stop
Step 1: Start
Step 2: Read the text until an empty line
Step 3: Compare each character with newline char \n to count no of lines
Step 4: Compare each character with tab char \t\ or space char to count noof words
Step 5: Compare first character with NULL char \0 to find the end of text
Step 6: No of characters = length of each line of text
Step 7: Print no of lines, no of words, no of chars
Step 8: Stop
Program
#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();
}
Week-8
29
Output
Enter the text: hello
Good morning
How are you
No. of lines=3
No. of words=6
No. of characters=24
start
read n
For(i=1;i<=n;i++)
False
true
For(k=1;k<=n-1;k++)
true
Write
For(j=1;j<=i;j++)
false
true
Write i
Write \n
stop
Algorithm
Step 1: Start
Step 2: read n
Step 3: initialize i=1 with increment by 1
If i<=n do as follows else goto step 7
Step 4: Initialize k=1 with increment by 1
If k<n-1 do as follows else goto step 5
Print
Step 5: initialize j=1 with increment by 1
If j<= i do as follows else goto step 6
Print i
Step 6: print \n
Goto step 3
Step 7: Stop
Program
30
false
#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();
}
Output:
Enter no of rows: 3
1
2
3
2
3
Week-9
31
a)Write a C program to read in two numbers,x and n,and then compute the sum ofthis
geometric progression:1 + x+ x^2 + x^3 +x^4. . +x^n.For example: if n is 3 and x is 5, then
the program computes 1+5+25+125. Printx,n the sum.Perform error checking. For example,
the formula does not makesense for Negative exponents - if n is lessthan 0. Have your
programprint anerror message if n<0, then go back and read in the next pair of numbers
of computing the sum. Are any values of x also illegal?If so, test forthem too.
Start
Flow Chart:
Read x,n
Sum
=1
For(i=1;i<=n;i++)
true
Sum= sum +
pow(x,i)
Write sum
Stop
Algorithm:
Step 1: Start
Step 2: read values of x and n, sum=1, i=1
Step 3: check for n & X
i) if n<=0 || x<=0
ii) print values are not valid
iii) read values of x and n
Step 4: perform the loop operation
i) for(i=1;i<=n;i++) then follows
ii) sum=sum+pow(x,i)
Step 5: print sum
Step 6: Stop
32
false
Program
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int sum,i,x,n;
clrscr();
printf("=========SUM OF GEOMETRIC PROGRESSION========\n");
printf("\nEnter the values for x and n:\n");
scanf("%d %d",&x,&n);
if(n <= 0 || x <= 0)
{
printf("\nValue is not valid\n");
}
else
{
printf("\nValue is valid\n");
sum = 1;
for(i=1;i<=n;i++)
sum = sum+pow(x,i);
printf("\nSum of series = %d\n",sum);
}
getch();
}
Output:
Enter the values for x and n: 1 2
Value is valid
Sum of series = 3
Week-10
33
{
b[i]='0';
c=1;
}
}
else
{
if(c==1 && b[i]=='0')
{
b[i]='1';
c=0;
}
else if(c==1 && b[i]=='1')
{
b[i]='0';
c=1;
}
}
}
b[len]='\0';
printf("\nThe 2's complement is : %s", b);
}
Output
Enter the binary number 11000
The 2's complement is: 01000
Flow Chart:
start
Read rom
len =
strlen(rom)
for(i=0;i<len;i++)
K = a[len1]
rom[i]=='I'
A[i]=1
for(i=len-1;i>0;i--)
rom[i]=='V'
a[i]>a[i-1]
K = k a[i1]
A[i]=5
rom[i]=='X'
A[i]=10
a[i]==a[i-1] || a[i]<a[i-1]
rom[i]=='L'
A[i]=50
K=k+
a[i-1]
rom[i]=='C'
A[i]=10
rom[i]=='D'
A[i]=50
rom[i]=='M'
A[i]=100
Write k
Algorithm
Write invalid
stop
36
Step 1: Start
Step 2: read the roman numerical as string
Step 3: find length of roman numerical
Step 4: for each charcter in the string
i) if(char=I) then decimal=1
ii) if(char=V) then decimal=5
iii) if(char=X) then decimal=10
iv) if(char=L) then decimal=50
v) if(char=C) then decimal=100
vi) if(char=D) then decimal=500
vii) if(char=M) then decimal=1000
viii) otherwise invalid character
Step 5: repeat step 4 until the length of the string
Step 6: k=char[length-1]
Step 7: for each character of decimal string
i) if(a[i]>a[i-1]) then k=k-a[i-1]
ii) else if(a[i]=a[i-1] or a[i]<a[i-1) then
k=k+a[i-1]
Step 8: repate step 7 until the length of decimal string
Step 9: print decimal value
Step 10: Stop
Program
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
void main()
{
int a[10],len,i,j,k;
char rom[10];
clrscr();
printf("\n\nEnter the Roman Numeral:");
scanf("%s",&rom);
len=strlen(rom);
for(i=0;i<len;i++)
{
if(rom[i]=='I')
a[i]=1;
else if(rom[i]=='V')
a[i]=5;
else if(rom[i]=='X')
a[i]=10;
else if(rom[i]=='L')
a[i]=50;
else if(rom[i]=='C')
a[i]=100;
else if(rom[i]=='D')
a[i]=500;
else if(rom[i]=='M')
a[i]=1000;
37
else
{
printf("\nInvalid Value");
getch();
exit(0);
}
}
k=a[len-1];
for(i=len-1;i>0;i--)
{
if(a[i] > a[i-1])
k=k-a[i-1];
else if(a[i]==a[i-1] || a[i]<a[i-1])
k=k+a[i-1];
}
printf("\nIts Decimal Equivalent is:");
printf("%d",k);
getch();
}
Output:
Enter the Roman numeral VII
Its Decimal Equivalent is 7
Week-11
38
{
case 1: w.realpart = w1.realpart + w2.realpart;
w.imgpart = w1.imgpart + w2.imgpart;
break;
case 2:
w.realpart=(w1.realpart * w2.realpart)-(w1.imgpart * w2.imgpart);
w.imgpart=(w1.realpart * w2.imgpart)+(w1.imgpart * w2.realpart);
break;
}
if(w.imgpart > 0)
printf("\nAnswer = %3.3lf + %3.3lf i",w.realpart,w.imgpart);
else
printf("\nAnswer = %3.3lf %3.3lf i",w.realpart,w.imgpart);
}
Output:
MAIN MENU
Select your choice:\n 1 : ADD\n 2 : MULTIPLY\n 0 : EXIT
Enter your choice :1
Enter two Complex Numbers(x+iy)
Real Part of First Number: 2
Imaginary Part of First Number: 2
Real Part of Second Number: 3
Imaginary Part of Second Number: 3
Answer = 5.000 + 5.000i
Week-12
40
a)Write a C program which copies the contents from one file to another.
Flow Chart:
Start
Read file1,file2
fs =
fopen(file1,r)
ft =
fopen(file2,w)
false
While(1)
true
Ch =
fgetc(fs)
Ch ==EOF
true
false
fputc(ch,f
t)
Write successfully copied
Fclose(fs)
Fclose(ft)
Algorithm:
Step 1: Start
Step 2: read file1, file2
Step 3: fs = fopen(file1,r)
Step 4: ft = fopen(file2,w)
Step 5: while(1)
If true then
Ch = fgetc(fs)
Step 6: if ch == EOF then
Print file copied
Fclose(fs)
Fclose(ft) goto step 7
Else
fputc(ch,ft)
goto step 5
step 7: Stop
Program
Stop
41
#include<stdio.h>
#include<conio.h>
void main()
{
FILE *fs,*ft;
char file1[30],file2[30],ch;
clrscr();
printf("\n\nEnter the source file path:\n");
gets(file1);
printf("\nEnter the destination file path :\n");
gets(file2);
fs = fopen(file1,"r");
if(fs==NULL)
{
puts("Source file cannot be opened.");
fclose(fs);
exit(0);
}
ft = fopen(file2,"w");
if (ft==NULL)
{
puts("Target file cannot be opened.");
fclose(ft);
exit(0);
}
while(1)
{
ch=fgetc(fs);
if(ch == EOF)
break;
else
fputc(ch,ft);
}
printf("\n\tThe file is succesfully copied...");
fclose(fs);
fclose(ft);
getch();
}
Output
(Note: The file name and n are specified on the command line).
Flow chart:
start
Read file1
fp =
fopen(file1,r)
Write enter no of characters
Read k
n=
fread(a,1,k,fp)
a[n]=\0
len = strlen(a)
false
For(i=len-1;i>=0;i--)
true
S[j] =
a[i]
Write s[j]
J=j+
1
S[j+1]=
\0
Algorithm
stop
Step 1: Start
Step 2: read file1
Step 3: fp = fopen(file1,r)
Step 4: read k
Step 5: n = fread(a,1,k,fp)
Step 6: a[n]=\0
len = strlen(a)
step 7: initialize i=len-1 decrement by 1
step 8: if i>=0 then do as follows else goto step 9
S[j] = a[i]
Print s[j]
J =j+1 goto step 7
Step 9: S[j+1]=\0
Step 10: stop
Program
43
#include <stdio.h>
#include <conio.h>
#include <string.h>
void main()
{
char file1[50],a[50],s[50],n ;
int j=0,k,i,len;
FILE *fp;
clrscr();
printf("Enter the file path :");
gets(file1);
fp = fopen(file1,"r"); /*FOPEN("FILENAME.TXT","W") */
if(fp == NULL)
{
puts("File cannot be opened.");
exit(0);
}
printf("\nEnter the number of characters to reverse :");
scanf("%d",&k);
n = fread(a,1,k,fp);
a[n]='\0';
len=strlen(a);
for(i=len-1;i>=0;i--)
{
s[j]=a[i];
printf("%c",s[j]);
j=j+1;
}
s[j+1]='\0';
getch();
}
Output
Week-13
44
start
Read file
Fp =
fopen(file,r)
false
While(1)
true
Ch =
getc(fp)
true
Ch == EOF
false
Write ch
Fclose(fp)
stop
Algorithm
Step 1: Start
Step 2: read file
Step 3: Fp = fopen(file,r)
Step 4: while(1)
If true do as follows else goto step 6
Ch = getc(fp)
Step 5: if ch==EOF then goto step 4
Else print ch
Step 6: fclose(fp)
Step 7: Stop
Program
#include<stdio.h>
45
#include<conio.h>
void main()
{
FILE *fp;
char ch,file[50];
clrscr();
printf("Enter the file path :");
gets(file);
fp = fopen(file,"r");
if(fp == NULL)
{
printf("\nThe file cannot be opened");
exit(0);
}
printf("The contents of %s file are\n",file);
while(1)
{
ch=getc(fp);
if(ch==EOF)
break;
printf("%c",ch);
}
fclose(fp);
getch();
}
Output:
46
Flow chart:
start
Read file1,file2,file3
Fp1 = fopen(file1,r)
Fp2 = fopen(file2,r)
Fp3 = fopen(file3,w)
while((ch=fgetc(fp1))!=EOF)
true
false
fputc(ch,fp3);
false
while((ch=fgetc(fp2))!=EOF)
true
fputc(ch,fp3);
fclose(fp1)
fclose(fp2)
fclose(fp3)
stop
Algorithm
Step 1: Start
Step 2: Fp1 = fopen(first.c,r)
Step 3: Fp2 = fopen(second.c,r)
Step 4: Fp3 = fopen(third.c,w)
Step 5: while((ch=fgetc(fp1))!=EOF)
If true fputc(ch,fp3) goto step 5
else goto step 6
Step 6: while((ch=fgetc(fp2))!=EOF)
if true fputc(ch,fp3) goto step 6
else goto step 7
Step 7: Fclose(fp1)
Step 8: Fclose(fp2)
Step 9: Fclose(fp3)
Step 10: Stop
Program
#include<stdio.h>
#include<conio.h>
47
void main()
{
FILE *fp1,*fp2,*fp3;
char ch,file1[30],file2[30],file3[30];
clrscr();
printf("\nEnter first file path :");
gets(file1);
printf("\nEnter second file path :");
gets(file2);
printf("\nEnter third file path :");
gets(file3);
fp1=fopen(file1,"r");
fp2=fopen(file2,"r");
fp3=fopen(file3,"a");
while((ch=fgetc(fp1))!=EOF)
fputc(ch,fp3);
while((ch=fgetc(fp2))!=EOF)
fputc(ch,fp3);
printf("\nFile merged");
fclose(fp1);
fclose(fp2);
fclose(fp3);
getch();
}
Output:
Week-14
a)Write C programs that uses non recursive function to search for a key value in a given
sorted list of integers using binary search
48
Flow Chart
start
Read num
For(i=0;i<num;i++)
true
false
Read list[i]
Bin_nonrec(list,num,ele)
stop
Bin_nonrec(list,num,ele)
Low=0
High=num
false
While(low<=high)
true
mid = (low+high)/2
false
List[mid]==ele
true
Write mid
F=1
List[mid] < ele
false
true
Low = mid +
1
high= mid - 1
ALGORITHM:
BINARY SEARCH
1. Start
F == 0
int low,high,mid, f = 0;
low = 0;
high = num-1;
while(low <= high)
{
mid = (low+high)/2;
if( list[mid] == ele)
{
printf("\nThe element %d is present at position %d in list\n",ele,mid);
f = 1;
break;
}
else if(list[mid] < ele)
low = mid+1;
else
high = mid-1;
}
if( f == 0)
printf("\nThe element %d is not present in the list\n",ele);
}
Output:
Binary Search using Non-Recursion method
Enter the number of elements: 5
Enter the sorted list of elements: 2 5 9 11 15
Elements present in the list are: 2 5 9 11 15
Enter the element you want to search: 11
The element 11 is present at position 3 in list
51
b)Write C programs that uses non recursive function to perform the linear searching operation
for a Key value in a given list of integers
Flow Chart
start
Read num
For(j=0;j<num;j++)
true
false
Read list[j]
linear_nonrec(list,num,ele)
stop
linear_nonrec(list,num,ele)
false
For(j=0;j<num;j++)
true
false
List[j]==ele
true
Write
F=1
F == 0
true
Write NOT PRESENT
ALGORITHM:
1. Start
52
}
Output
Linear Search using Non-Recursion method
Enter the number of elements: 5
Enter the sorted list of elements: 2 5 9 11 15
Elements present in the list are: 2 5 9 11 15
Enter the element you want to search: 11
The element 11 is present at position 3 in list
Week-15 a)Write a C program that implements the selection sort method to sort a given
array of integers.
Flow Chart:
start
54
Read n
For(j=0;j<n;j++)
true
false
Read list[j]
Selection(list,n)
For(j=0;j<n;j++)
false
true
write list[j]
stop
.
Selection(list,n)
false
exit
For(i=0;i<n;i++)
true
swap = 0
k=i
temp = list[i]
For(i=i+1;j<n;j++)
true
false
false
true
k=j
temp = list[j]
swap =1
swap
false
true
Program
#include<conio.h>
#include<stdio.h>
list[k] = list[i];
list[i] = temp;
55
#define MAX 10
void selection(int [], int);
void main()
{
int list[MAX], n, j;
clrscr();
printf("\n\nEnter the number of elements [Maximum 10] \n");
scanf("%d",&n);
printf("\nEnter the elements :");
for(j=0;j<n;j++)
scanf("%d",&list[j]);
printf("\n\nElements in the list before sorting are:\n");
for(j=0;j<n;j++)
printf("%d\t",list[j]);
selection(list,n);
printf("\n\nElements in the list after sorting are:\n");
for(j=0;j<n;j++)
printf("%d\t",list[j]);
getch();
}
void selection(int list[], int n)
{
int i, j, k, swap, temp;
for(i=0;i<n;i++)
{
swap = 0;
k=i;
temp = list[i];
for (j = i+1; j<n; j++)
{
if( list[j] < temp)
{
k=j;
temp = list[j];
swap =1;
}
}
if (swap)
{
Output:
list[k] = list[i];
Enter the number of elements: 5
list[i] = temp;
Enter the elements : 2 5 9 11 15
}
Elements in the list before sorting are: 2 5 9 11 15
}
Elements in the list after sorting are:2 5 9 11 15
}
56
b)Write C programs that implement the bubble sorting method to sort a given list of integers
in ascending order
Flow Chart
start
Read n
For(j=0;j<n;j++)
false
Read list[j]
bubblesort(list,n)
For(j=0;j<n;j++)
true
write list[j]
stop
bubblesort(list,n)
false
For(i=0;i<n-1;i++)
True
false
for(j=0;j<(n-(i+1));j++)
true
false list[j]> list[j+1]
true
exit
temp = list[j]
list[j]=list[j+1]
list[j+1]=temp
57
false
Algorithm:
1. start
2. read the value of n
3. for i= 1 to n increment in steps of 1
Read the value of ith element into array
4. call function to sort (bubblesort(list,n))
5. for i= 1 to n increment in steps of 1
print the value of ith element in the array
6. stop
BUBBLE SORT FUNCTION
1. start
2. initialise n to n
3. for i= 0 to n-1 increment in steps of 1
Begin
4. initialise j to 0
5. for j= 1 to n-i+1 increment in steps of 1
begin
6. if list[j]>list[j+1] goto step 7 otherwise goto step 5
begin
7. assign list[j] to temp
assign list[j+1] to list[j]
assign temp to list[j+1]
end-if
end inner for loop
end for loop
8. stop
Program
#include<conio.h>
#include<stdio.h>
#define MAX 10
void bubblesort(int [], int);
void main()
{
int list[MAX], n, j;
clrscr();
printf("\n\nEnter the number of elements [Maximum 10] \n");
scanf("%d",&n);
printf("\nEnter the elements :");
for(j=0;j<n;j++)
scanf("%d",&list[j]);
printf("\n\nElements in the list before sorting are:\n");
for(j=0;j<n;j++)
printf("%d\t",list[j]);
bubblesort(list,n);
printf("\n\nElements in the list after sorting are:\n");
for(j=0;j<n;j++)
printf("%d\t",list[j]);
getch();
58
}
void bubblesort(int list[], int n)
{
int i,j,temp;
for(i=0;i<n-1;i++)
for(j=0;j<(n-(i+1));j++)
if(list[j] > list[j+1])
{
temp = list[j];
list[j] = list[j+1];
list[j+1] = temp;
}
}
Output:
Enter the number of elements: 5
Enter the elements : 2 5 9 11 15
Elements in the list before sorting are: 2 5 9 11 15
Elements in the list after sorting are: 2 5 9 11 15
59
Week-16
Write a C program that has functions to perform the following operations on Singly Linked
List: i)Creation ii)Traversal
Program
#include<stdio.h>
#include<alloc.h>
#include<conio.h>
struct node
{
int data;
struct node *next;
};
struct node *start,*tail;
int count=0;
void main()
{
void display(void);
void create(void);
int ch;
start = tail = NULL;
clrscr();
while(1)
{
printf("\n 1.Create a Linked List");
printf("\n 2.Display the List");
printf("\n 3.Exit.");
printf("\n Enter the Choice :");
scanf("%d",&ch);
switch(ch)
{
case 1 :
create();
break;
case 2 :
display();
break;
case 3 :
exit(0);
break;
default :
printf("\nEnter the valid choice ");
}
}
}
void create()
{
struct node *temp;
temp=(struct node *) malloc(sizeof(struct node));
printf("\nTotal no of nodes in list %d\n", count);
60
3.Exit
Enter the choice:3
Week-18
Write a C program that implements Queue(its operations) using a singly linked list to display
a given list of integers in same order.
Program
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
void insert(void);
void display(void);
struct node
{
int data;
struct node *next;
}*start=NULL;
void main()
{
int ch;
clrscr();
while(1)
{
printf("\n1.Insert\n");
printf("2.Display\n");
printf("3.Exit\n");
printf("Enter the choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:
insert();
break;
case 2:
display();
break;
case 3:
exit();
break;
default:
printf("\nEnter correct choice");
}
}
}
void insert()
{
int ele;
struct node *newnode,*temp;
printf("\nEnter the element:");
scanf("%d",&ele);
newnode=(struct node *)malloc(sizeof(struct node));
64
newnode->data=ele;
newnode->next=NULL;
if(start == NULL)
start = newnode;
else
{
temp = start;
while(temp->next != NULL)
temp=temp->next;
temp->next=newnode;
}
}
void display()
{
struct node *temp=start;
if(start==NULL)
printf("\nThere are no elements in queue");
else
{
printf("\nThe elements in queue are :");
while(temp != NULL)
{
printf("%d ",temp->data);
temp=temp->next;
}
}
}
Output:
1.Insert
2.Display
3.Exit
Enter the choice:1
Enter the element 10
1.Insert
2.Display
3.Exit
Enter the choice:1
Enter the element 20
1.Insert
2.Display
3.Exit
Enter the choice:1
Enter the element 30
1.Insert
2.Display
3.Exit
Enter the choice:2
The elements in queue are 10 20 30
1.Insert
2.Display
65
3.Exit
Enter the choice:3
Week 19
Write a C program to implement the linear regression algorithm.
Program
#include<stdio.h>
#include<conio.h>
#include<math.h>
main()
{
int n,i;
float sumx,sumxsq,sumy,sumxy,x,y,a0,a1,denom;
clrscr();
printf("enter the n value ");
scanf("%d",&n);
sumx=0;
sumxsq=0;
sumy=0;
sumxy=0;
for(i=0;i<n;i++)
{
scanf("%f%f",&x,&y);
sumx = sumx+x;
sumxsq += pow(x,2);
sumy = sumy+y;
sumxy = sumxy + x * y;
}
denom = n * sumxsq - pow(sumx, 2);
a0 = (sumy * sumxsq - sumx *sumxy)/denom;
a1 = (n * sumxy - sumx * sumy)/denom;
printf("y= %fx + %f",a1,a0);
getch();
}
Output:
Enter the n value 7
1 2
2 5
4 7
5 10
6 12
8 15
9 19
Y = 1.980769x + 0.096154
66
Week 20
Write a C program to implement the polynomial regression algorithm.
Program
#include<stdio.h>
#include<conio.h>
#include<math.h>
main()
{
int n,i,j,k;
float sumx=0,sumxsq=0,sumy=0,sumxy=0,sumx3=0,sumx4=0,sumxsqy=0;
float x,y,a[20][20],u=0.0,b[20];
clrscr();
printf("\nEnter the n value ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%f%f",&x,&y);
sumx += x;
sumxsq += pow(x,2);
sumx3 += pow(x,3);
sumx4 += pow(x,4);
sumy +=y;
sumxy += x * y;
sumxsqy += pow(x,2) * y;
}
a[0][0] = n;
a[0][1] = sumx;
a[0][2] = sumxsq;
a[0][3] = sumy;
a[1][0] = sumx;
a[1][1] = sumxsq;
a[1][2] = sumx3;
a[1][3] = sumxy;
a[2][0] = sumxsq;
a[2][1] = sumx3;
a[2][2] = sumx4;
a[2][3] = sumxsqy;
for(i=0; i<3; i++)
{
for(j=0; j<=3; j++)
printf("%10.2f",a[i][j]);
printf("\n");
}
for(k=0; k<=2; k++)
{
for(i=0;i<=2;i++)
{
if(i!=k)
u=a[i][k]/a[k][k];
for(j = k; j<=3; j++)
a[i][j]=a[i][j] - u * a[k][j];
67
}
}
for(i=0;i<3;i++)
{
b[i] = a[i][3]/a[i][i];
printf("\nx[%d] = %f",i,b[i]);
}
printf("\n");
printf("y= %10.4fx + %10.4fx + %10.4f",b[2],b[1],b[0]);
getch();
}
Output:
Enter the n value 10
-4 21
-3 12
-2 4
-1 1
0 2
1 7
2 15
3 30
4 45
5 67
10.00 5.00
85.00
204.00
5.00 85.00 125.00
513.00
85.00 125.00 1333.00 3193.00
X[0] = 2.030303
X[1] = 2.996970
X[2] = 1.984848
Y = 1.9848x + 2.9979x + 2.0303
68
Week 21
Write a C program to implement the Lagrange interpolation.
Program
#include<stdio.h>
#include<conio.h>
#include<math.h>
main()
{
float y, x[20],f[20],sum=0,pf;
int i,j,n;
clrscr();
printf("enter the value of n ");
scanf("%d",&n);
printf("enter the value to be found ");
scanf("%f",&y);
printf("enter the values of xi's & fi's ");
for(i=0;i<n;i++)
{
printf("Enter x[%d] value :",i);
scanf("%f",&x[i]);
printf("Enter y[%d] value :",i);
scanf("%f",&f[i]);
}
for(i=0;i<n;i++)
{
pf=1;
for(j=0;j<n;j++)
{
if(j!=i)
pf *= (y-x[j])/(x[i]-x[j]);
}
sum += f[i] * pf;
}
printf("\nx = %f",y);
printf("\n sum =%f",sum);
getch();
}
Output
Enter the value of n 4
Enter the value to be found 2.5
Enter the values for xis & fis
1 1
2 8
3 27
4 64
X = 2.500000
69
Sum = 15.625000
Week 22
Write a program to implement the Newton - Gregory forward interpolation.
Program
#include<stdio.h>
#include<conio.h>
#include<math.h>
main()
{
int i,j,n,k,l;
float sumy=1,h,term,p,z,pvalue=1,x[25],y[25],d[25][25],factvalue=1;
clrscr();
printf("enter the value of n: ");
scanf("%d",&n);
printf("\nenter %d values for x, y",n);
for(i=0;i<n;i++)
scanf("%f%f",&x[i],&y[i]);
printf("\n enter z");
scanf("%f",&z);
h = x[1]-x[0];
p = (z-x[0])/h;
for(j=0;j<n-2;j++)
d[i][j]=y[j+1] - y[j];
k=n-2;
for(i=2; i<n; i++)
{
k++;
for(j=0; j<=k; j++)
d[i][j] = d[i-1][j+1] - d[i-1][j];
}
for(l=1; l<n; l++)
{
pvalue *= (p-(l - 1));
factvalue *= 1;
term = pvalue * d[l][0]/factvalue;
sumy += term;
}
printf("\n y value at z = %f is %f", z, sumy);
getch();
}
Output:
Enter n 7
Enter 7 data values for x, y
1921 35
1931 42
1941 58
1951 84
1961 120
1971 165
1981 220
70
Enter z 1925
Y value at z = 1925.000000 is 36.756710
Week 23
Write a C program to implement Trapezoidal method.
Program
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
float h,a,b,n,x[20],y[20],sum=0,integral;
int i;
clrscr();
printf("enter the value ofa,b,n:");
scanf("%f %f %f",&a,&b,&n);
printf("\nenter the values of x:");
for(i=0;i<=(n-1);i++)
scanf("%f",&x[i]);
printf("\nenter the values of y:");
for(i=0;i<=(n-1);i++)
scanf("%f",&y[i]);
h=(b-a)/n;
x[0]=a;
for(i=1;i<=n-1;i++)
{
x[i]=x[i-1]+h;
sum=sum+2*y[i];
}
sum=sum+y[b];
integral=sum*(h/2);
printf("\napproximate integral value is: %f",integral);
getch();
}
Output:
Enter the values of a,b,n
1 2 3
Enter the values of x:
1 2 3
Enter the values of y:
1 2 3
Approximate integral value is 2.166667
71
Week 24
Write a C Program to implement Simpsons method.
Program
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
float h,a,b,n,x[20],y[20],sum=0,integral;
int i;
clrscr();
printf("\nenter the values of a,b,n:");
scanf("%f%f%f",&a,&b,&n);
printf("\nenter the values of x:");
for(i=0;i<=n;i++)
scanf("%f",&x[i]);
printf("\nenter the values of y:");
for(i=0;i<=n;i++)
scanf("%f",&y[i]);
h=(b-a)/n;
a=x[0];
b=x[n];
for(i=0;i<=(n-2);i++)
{
x[i]=x[i]+h;
if(i%2==0)
sum=sum+4*y[i];
else
sum=sum+2*y[i];
}
integral=sum*(h/3);
printf("\nintegral value is %f",integral);
getch();
}
Output:
Enter the values of a, b, n
1 2 3
Enter the value of x
4 5 6 7
Enter the values of y
8 9 1 2
Integral value is 5.555556
72