Sie sind auf Seite 1von 23

COMPUTER PROGRAMMING LAB

Week 1:

WriteaaCCprogram
programtotofind
findthe
theroots
rootsofofaaquadratic
quadraticequation.
equation.
1.1.a)a)Write
AIM: To find the roots of a quadratic equation.
DESCRIPTION: roots of quadratic equation are
ALGORITHM:
Step 1: Start
Step 2: Read a,b,c
Step 3: calculate disc = b*b-4*a*c
Step 4: if(disc>0)
Begin
Step 5: root1=(-b+sqrt(disc))/(2*a)
Step 6: root2=(-b-sqrt(disc))/(2*a)
Step 7: Print Root1 , Root2
End
Step 8: else if(disc=0)
Begin
Step 9: root1= -b/(2*a)
Step 10: root2=root1;
Step 11: Print Root1 , Root2
End
Step 12: else
Step 13: Print Roots are imaginary
Step 14: Stop

FLOW CHART

PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()

{
float a,b,c;
float disc,root1,root2;
clrscr();
printf("ENTER VALUES FOR a,b,c\n");
scanf("%f%f%f",&a,&b,&c);
//find discriminent
disc=(b*b-4*a*c);
/*Find the distinct roots*/
if(disc>0)
{
printf("THE ROOTS ARE REAL & UNEQUAL:\n");
root1=(-b+sqrt(disc))/(2*a);
root2=(-b-sqrt(disc))/(2*a);
printf("Root1=%f\n",root1);
printf("Root2=%f\n",root2);
}
else if(disc==0) //Find the equal roots
{
printf("THE ROOTS ARE REAL AND EQUAL:\n");
root1=root2= -b /(2*a);
printf("Root1=%f Root2=%f\n",root1,root2);
}
else
{

//Find complex roots


printf("roots are imaginary");

}
getch();
}
OUTPUT:
ENTER VALUES FOR a, b, c
1
4
4
THE ROOTS ARE REAL AND EQUAl :
Root1= -2.000 Root2= -2.000

WriteaaCCprogram
programtotoimplement
implementcalculator
calculatortotoperform
performarithmetic
arithmetic
11b)b)Write
operationsusing
usingSwitch
SwitchStatement.
Statement.
operations
AIM:
To perform arithmetic operations using switch statement.

Description:
Implementing calculator operations like +,-,*,/,%
ALGORITHM:
Step 1: read a,b
Step 2: read ch
Step 3: switch (ch)
begin
Step 4: case +:
begin
calculate res = a+b
print res
break;
end
case -:
begin
calculate res = a-b
print res
break;
end
case *:
begin
calculate res = a*b
print res
break;
end
case /:
begin
calculate d = a/b
print d
break;
end
case %:
begin
calculate res = a%b
print res
break;
end
default: print invalid choice
end
end
Step 5: stop
PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
{
char ch;

int a,b,res;
float d;
clrscr();
printf("Enter a,b values: \n");
scanf("%d%d",&a,&b);
printf("Enter any operator like +,-,*,/,% \n");
flushall();
scanf("%c",&ch);
switch(ch)
{
case '+': res=a+b;
printf("Addition of two numbers:%d",res);
break;
case '-': res=a-b;
printf("Substraction of two numbers:%d",res);
break;
case '*': res=a*b;
printf("Multiplication of two numbers:%d",res);
break;
case '/': d=(float)a/b;
printf("Division of two numbers:%f",d);
break;
case '%': res=a%b;
printf("Modulo division of two numbers:%d",res);
break;
default: printf("Invalid choice");
break;
}
getch();
}
Output:
RUN1:
Enter a,b values:
12
5
Enter any operator like +,-,*,/,%
%
Modulo division of two numbers:2
RUN2:
Enter a,b values:
12
5
Enter any operator like +,-,*, /, %

+
Addition of two numbers: 17

WEEK 2:
Write
a CWrite
ProgramatoCfind
sum of individual
2. a)
Program
to finddigits
sumofofa number.
individual

digitsofofaa
2. a) Write a C Program to find sum of individual digits
number.
number.

Algorithm
Aim: To calculate sum of individual digits of positive number
Description: We can find sum of individual digits of positive number only. Give input as only positive
number.
Algorithm:
Step1: start
Step2: sum 0
Step3: read n
Step5: while(n!=0) do
begin
rem n % 10
sum sum + rem
n n/10
end while
Step6: print sum
Step7: stop
PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
{
int n,rem,sum=0;
clrscr();
printf("Enter a positive integer \n");
scanf("%d",&n);
while(n!=0)
{
rem=n%10;
sum=sum+rem;
n=n/10;
}
printf("The sum of individual digits of a positive integer is..%d",sum);
getch();
}
Output
Enter a positive integer
167
The sum of individual digits of a positive integer is..14

WriteaaCCProgram
Programtotogenerate
generateFibonacci
Fibonaccisequence
sequenceup
uptoton.n.
2.2.b)b)Write

Aim: To find Fibonacci series up to n


Discription: Fibonacci sequence is set of numbers that starts with a 0 or 1, followed by 1, and proceeds
based on rule that the subsequent number is sum of previous two numbers.
Algorithm:
Step 1: start
Step 2: read n
Step 3: first 0
Step 4: second 1
Step 5: print first, second
Step 6: for i 3 to n do
begin
next first+second
print next
first second
second next
end
Step 7: stop

PROGRAM
/* program to generate fibonacci sequence */
#include <stdio.h>

#include <conio.h>
void main()
{
int n,i,first,second,next;
clrscr();
printf("Enter n-terms \n");
scanf("%d",&n);
first=0;
/* first term is zero */
second=1; /* second term is 1 */
printf("Fibonacci sequence: \n");
printf("%d\t",first);
printf("%d\t",second);
for(i=3;i<=n;i++)
{
next=first+second;
printf("%d\t",next);
first=second;
second=next;
}
getch();

/* subsequent number is sum of previous two numbers */

}
OUTPUT
Enter n-terms
8
Fibonacci sequence:
0
1
1

13

WEEK 3:

Writea aCCProgram
Programtotogenerate
generateprime
primenumbers
numbersbetween
between11and
andn.n.
3.3. a)a)Write

Aim: To find prime numbers between 1 to n


Description: Print the numbers which are divided by 1 and itself (A prime number can be divided evenly
only by 1 and itself).
Algorithm:
Step1: start
Step2: read n
Step3: for i=1 to n repeat
Step3.1: counter0
Step3.2: for j=1 to n repeat
Step3.2.1: if i%j=0 then countercounter+1
Step3.3: if counter=2 then print i
Step4: stop

PROGRAM:

#include<stdio.h>
#include<conio.h>
void main()
{
int i,n,j,counter;
clrscr();
printf("Enter n value: \n");
scanf("%d",&n);
printf("Prime numbers between 1 and %d are:\n",n);
for(i=1;i<=n;i++)
{
counter=0;
for(j=1;j<=i;j++)
{
if(i%j==0)
counter++;
}
if(counter==2)
{
printf("%d\t",i) ;
}
}
getch();
}
Output:
Enter n value:
10
Prime numbers between 1 and 10 are:
2
3
5
7

2
4
6
8
Writea aCCprogram
programtotocalculate
calculatethe
theSum=1-x
Sum=1-x2/2!
/2!+x
+x4/4!-x
/4!-x6/6!+x
/6!+x8/8!-x
/8!-x1010
/10!
3.3. b)b)Write
/10!

Aim: To find the sum of series is 1-x2/2! +x4/4!-x6/6!+x8/8!-x10/10!

Alogrithm for main program:


Step1:start
Step2:s 0
Step3:read x;
Step4:for i=1,n=0 to 10 repeat
ss+(pow(-1,n)*pow(x,i)/fact(i))
step5:print s
step6:stop
Alogrithm for sub program:
Step1:start
Step2:y 1
Step3:read x;
Step4: while(x!=0) do
begin

yy*x
xx-1
end while
step5:return y
step6:stop

Program:

#include<stdio.h>
#include<math.h>
long fact(int);
void main()
{
int x,i,n;
float s=0 ;
clrscr();
printf("Enter x value:/n");
scanf("%d",&x);
for(i=0,n=0;i<=10;i=i+2,n++)
{
s=s+(pow(-1,n)*pow(x,i)/fact(i));
}
printf("Sum is %f",s);
getch();
}
long fact(int x)
{
long int y=1;
while(x!=0)
{
y=y*x;
x--;
}
return y;
}
Output:
Enter x value:
2
Sum is -0.0416155

WEEK 6:

WriteaaCCprogram
programtotofind
findboth
boththe
thelargest
largestand
andsmallest
smallest
6.6. a)a)Write
numberininaalist
listofofintegers.
integers.
number
AIM:
To find the largest and smallest number in a list of integers.
ALGORITHM:
Step 1: start
Step 2: read n
Step 3: for i from 0 to n-1
Read a[i]
i++ goto step 3
Step 4: small=a[0], large=a[0]
Step 5:for i from 0 to n-1 do following
if a[i]<small
Assign small=a[i]
if a[i]>large
Assign large=a[i]
Increment i goto Step 5
Step 6: print small, large
Step 7: stop

Program:

FLOW CHART:

#include<stdio.h>
#include<conio.h>
void main()
{
int a[20],n,i ;
int large,small;
clrscr();
printf("enter n value");
scanf("%d",&n);
printf("enter %d elements",n);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
//to find large and small values
small=a[0];
large=a[0];
for(i=0;i<n;i++)
{
if(a[i]>large)
large=a[i];
if(a[i]<small)
small=a[i];
}
printf("largest=%d\n",large);
printf("smallest=%d",small);
getch() ;
}
INPUT:
Enter n value:10
enter 10 elements of array
7
10
9
8
OUTPUT:
largest = 10
smallest = 1

WriteaaCCprogram
programtotoperform
performmultiplication
multiplicationofoftwo
twomatrices.
matrices.
6.6.b)b)Write
AIM:
To perform multiplication of two matrices.
ALGORITHM:
Step 1: Start
Step 2: read m,n
Step 3:for i from 0 to m-1 and j from 0 to n-1
Step 4:Read a[i][j]
Step 5:increment i and j ,goto step 3
Step 6:read p,q
Step 7:for i from 0 to p-1 and j from 0 to q-1
Step 8:Read b[i][j]
Step 9:increment i and j ,goto step 7
Step 10:for i from 0 to m-1 and j from 0 to q-1
Step 11: initialize 0 to c[i][j]
Step 12: for k from 0 to n-1
Step 13:calculate c[i][j]=c[i][j]+a[i][k]*b[k][j]
Step 14:increment k goto step 12
Step 15:increment i,j ,goto step 10
Step 16: for i from 0 to m-1 and j from 0 to q-1
Step 17:Print c[i][j]
Step 18:Increment i and j ,goto step 16
Step 19: Stop

Program :
#include<stdio.h>
#include<conio.h>
void main()

{
int a[20][20],b[20][20],c[20][20];
int m,n,p,q,i,j,k,sum;
clrscr();
printf("enter matrix size of A:\n");
scanf("%d%d",&m,&n);
printf("enter elements of first matrix\n") ;
for(i=0;i<=m-1;i++)
{
for(j=0;j<=n-1;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("enter size of matrix B:\n");
scanf("%d%d",&p,&q) ;
printf("enter elements of matrix b:\n");
for(i=0;i<=p-1;i++)
{
for(j=0;j<=q-1;j++)
{
scanf("%d",&b[i][j]);
}
}
//matrix multiplication
if(n!=p)
{
printf("matrix multiplication not possible enter correct size:");
}
else
{
for(i=0;i<=m-1;i++)
{
for(j=0;j<=q-1;j++)
{
c[i][j]=0;
for(k=0;k<=n-1;k++)
{
c[i][j]= c[i][j]+a[i][k]*b[k][j];
}
}
}
printf("multiplication of two matrices a and b are:\n");
for(i=0;i<=m-1;i++)
{
for(j=0;j<=q-1;j++)

{
printf("%d\t",c[i][j]);
}
printf("\n");
}
}
//comment : to close else
getch();
}
// comment: close main
Output:
Run 1:
enter matrix size of A:2 2
enter elements of first matrix
1 2
3 4
enter size of matrix B: 3 3
1
2
3

enter elements of matrix b:


1 1
2 2
3 3
matrix multiplication not possible enter correct size.
Run 2:
enter matrix size of A:2 2
enter elements of first matrix
1 2
3 4

1
2

enter size of matrix B: 2 2


enter elements of matrix b:
1
2
multiplication of two matrices a and b are:
5 5
11 11
Week7:

7.a)Write
WriteaaCCprogram
programtotofind
findthe
thefactorial
factorialofofaagiven
giveninteger
integerusing
using
7.a)
non-recursion
and
recursion
function.
non-recursion and recursion function.
AIM:
To find the factorial of a given number using non-recursion and recursion function.
ALGORITHM:
Step 1: Start
Step 2: Read n

Step 3: Call nonrecfact(n) and store value in result1


Step 4: call recfact(n) and store value in reult2
Step 4: Print result1 and result2
Step 5: Stop
Sub program: nonrecfact( n)
Step 1: while n!=0 goto Step 2
Step 2: y=y*n; n-Step 3: return y
Step 4: return to main program
Sub program: recfact(n)
Step 1: if n= = 0 return 1 to main program if not goto step 2
Step 2: return n*fact(n-1) to main program

PROGRAM:
#include<stdio.h>
#include<conio.h>
int nonrecfact(int);
int recfact(int);
void main()
{
int n , i , result1, result2;
clrscr();
printf("ENTER A VALUE FOR n:\n");
scanf("%d",&n);
result1=nonrecfact(n);
result2=recfact(n);

printf("THE FACTORIAL OF A GIVEN NUM using NonRecussive IS..%d\n",result1);


printf("THE FACTORIAL OF A GIVEN NUM using Recurssive IS..%d",result2);
getch();
}
//sub function for non recursive factorial
int nonrecfact(int n)
{
int i,y=1;
for(i=1;i<=n;i++)
{
y=y*i;
}
return y;
}
//sub function for recursive factorial
int recfact(int n)
{
if(n= =0)
return 1;
else
{
return n*recfact(n-1);
}
}
Output :
Enter a value for n
5
THE FACTORIAL OF A GIVEN NUM using NonRecussive IS..120
THE FACTORIAL OF A GIVEN NUM using Recurssive IS..120

7.b)Write
WriteaaCCprogram
programtotofind
findthe
theGCD
GCDofoftwo
twogiven
givenintegers
integersby
by
7.b)
Usingthe
theNon-recursion
Non-recursionand
andrecursion
recursionfunction
function
Using
Aim: To find the Gcd of two given integers by using the recursive function
Algorithm:
Main program:
Step 1: start
Step 2: read m,n
Step 3: call the sub program nonrec(m,n) and store value in result1
Step 4: call the sub program rec(m,n) and store value in result2
Step 4: print result1 and result2 value
Step5: stop
Sub program: GCD(m,n)
Step 1: while n= =0 goto step 5
Step 2: rem=m%n
Step 3: m=n
Step 4: n=rem goto step 1

Step 5: returm m to main function


Sub program: GCD(m,n)
Step 1: if n= = 0 return m to the main program else goto step 2
Step 2: return GCD (n, m%n) to the main program

PROGRAM:
#include<stdio.h>
#include<conio.h>
int nonrecgcd(int m,int n);
int recgcd(int m,int n);
void main()
{
int m,n,result1,result2;
clrscr();
printf("Enter the two numbers whose gcd is to be found:");
scanf("%d%d",&m,&n);
result1=nonrecgcd(m,n);
result2=recgcd(m,n);
printf("GCD of m,n using Non recursion is %d\n", result1);
printf("GCD of m,n using recursion is %d", result2);
getch();
}
//subfunction for Non recusion GCD
int nonrecgcd(int m,int n)
{
int rem;
while(n!=0)
{

rem=m%n;
m=n;
n=rem;
}
return m;
}
//subfunction for recusion GCD
int recgcd(int m,int n)
{
if(n==0)
return m;
else
return recgcd(n,m%n);
}
OUTPUT:
Enter the two numbers whose gcd is to be found:
5
25
GCD of m,n using Non recursion is 5
GCD of m,n using recursion is 5

Das könnte Ihnen auch gefallen