Algorithms

You might also like

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 12

I-BSC/PRACTICALS/C-LANG Page 1 of 12

INDEX
1. ROOTS OF QUADRATIC EQUATION
2. ARMSTRONG NUMBER
3. PRINTING PRIME NUMBERS
4. BINOMIAL COEFFICIENT
5. MATRIX MULTIPLICATION
6. STRING PALINDROME
7. BUBBLE SORT
8. DYNAMIC MEMORY ALLOCATION
9. STUDENT RESULT PROCESSING

ROOTS OF QUADRATIC EQUATION

Aim: A program to find the roots of a quadratic equation ax2+bx+c=0


Input Variables: a, b, c are float variables
Output Variables: r1, r2, ip, rp are float variables
Other variables: d is a float variable to store discreminant value
Algorithm:
Step 1: Start
Step 2: Read a, b, c
Step 3: If a = 0 then
Display “Wrong values”
goto step 8
Else
Goto step 4
Endif
Step 4: d = b2 – 4ac
Step 5: [Finding equal roots]
If d = 0 then
Display “Roots are equal”
b
r1=r2=
2a
Display r1, r2
Goto step 8
Endif
Step 6: [Finding unequal roots]
If d > 0 then
Display “Roots are unequal”
b d
r 1=
2a
b d
r2=
2a
Display r1, r2
Goto step 8
Endif
Step 7: [Finding imaginary roots]
If d < 0 then
Display “Roots are imaginary”
d = abs(d)
b
rp =
2a
d
ip =
2a
Display rp+ ip, rp – ip
Goto step 8
Endif
Step 8: Stop

PROGRAM:
Page 2 of 12 I-BSC/PRACTICALS/C-LANG

/* Program to find roots of a quadratic equation */


#include <stdio.h>
#include <conio.h>
#include <math.h>
main( )
{
float a,b,c,d,r1,r2,rp,ip;
clrscr( );
printf("Enter a,b,c values ");
scanf("%f%f%f",&a,&b,&c);
if (a = = 0)
printf("Invalid values");
else
{
d=b*b-4*a*c;
if (d = = 0)
{
printf("Roots are equal");
r1=r2= –b/(2*a);
printf("\n Root1=%5.2f \n Root2=%5.2f",r1,r2);
}
else if (d>0)
{
printf("Roots are unequal ");
r1=(-b-sqrt(d))/(2*a);
r2=(-b+sqrt(d))/(2*a);
printf("\n Root1=%5.2f \n Root2=%5.2f",r1,r2);
}
else if (d<0)
{
printf("Roots are imaginary ");
d=abs(d);
rp=-b/(2*a);
ip=sqrt(d)/(2*a);
printf("\n Root1=%5.2f+i%5.2f",rp,ip);
printf("\n Root2=%5.2f–i%5.2f",rp,ip);
}
}
getch( );
}

Input:

????

Output:

????
I-BSC/PRACTICALS/C-LANG Page 3 of 12
ARMSTRONG NUMBER

Aim: A program to check whether the given number is armstrong or not.


Def: “If the sum of cubes of digits of a number is equal to that number then it is called
Armstrong number”.
Input Variables: ‘n’ is an integer variable.
Output Variables: temp is an integer variable.
Other Variables: d, sum are integer variables.

Algorithm:
Step 1: Start
Step 2: Read n
Step 3: temp = n
Step 4: sum=0
Step 5: Repeat while n>0
d=n%10
sum=sum+d*d*d
n=n/10
Step 6: if sum=temp then
display “Armstrong”
else
display “Not Armstrong”
endif
Step 7: Stop

PROGRAM:

/* Program to check Armstrong number or not */


#include <stdio.h>
#include <conio.h>
main()
{
int n,d,temp,sum=0;
clrscr();
printf("Enter a number:");
scanf("%d",&n);
temp=n;
while (n>0)
{
d=n%10;
sum=sum+d*d*d;
n=n/10;
}
if (sum==temp)
printf("%d is Armstrong",temp);
else
printf("%d is not Armstrong",temp);
getch();
}

Input:

????
Output:

????
Page 4 of 12 I-BSC/PRACTICALS/C-LANG

PRINTING PRIME NUMBERS

Aim: A program to print prime numbers upto ‘n’.


Input Variables: ‘n’ is an integer variable to specify range.
Output Variables: num is an integer variable.
Other Variables: i, check are integer variables.

Algorithm:
Step 1: Start
Step 2: Read n
Step 3: Repeat for num=1 to n do
Step 3.1: Check=1
Step 3.2: Repeat for i=2 to num-1 do
Step 3.2.1: If (num%i = 0) then
Check=0
goto step 3.2.2
Endif
Endfor
Step 3.2.2: If (check=1) then
Display num
Endif
Endfor
Step 4: Stop

PROGRAM:

/* Program to print prime numbers upto 'n' */


#include <stdio.h>
#include <conio.h>
main()
{
int n,num,i,check;
clrscr();
printf("Enter n value:");
scanf("%d",&n);
printf("Prime Numbers are ...\n");
for(num=1; num<=n; num++)
{
check=1;
for(i=2; i<=num-1; i++)
if (num%i==0)
{
check=0;
break;
}
if (check==1)
printf("%d \t",num);
}
}
getch();
}

Input:

????
Output:

????
I-BSC/PRACTICALS/C-LANG Page 5 of 12
BINOMIAL COEFFICIENT
Aim: A program to find binomial coefficient i.e. nc using recursive function
r

Input Variables: n, r are integer variables


Output Variables: ncr is float variable

Algorithm:
Step 1: Start
Step 2: Read n, r
Step 3: If (n>=r) then
ncr = fact(n) / (fact(n-r) * fact(r) ) /* Calling fact( ) function */
Display ncr
Else
Display “Wrong values”
Endif
Step 4: Stop

Algorithm: fact(n)
/* This procedure finds factorial of a number using recursive function */
Step 1: If (n<=1) then
Return(1)
Else
Return(n*fact(n-1))
Endif

PROGRAM:

/* Program to find Binomial co-efficient using recursive function */


#include <stdio.h>
#include <conio.h>
main()
{
long int fact(int);
int n,r;
float ncr;
clrscr();
printf("Enter n,r values ");
scanf("%d%d",&n,&r);
if (n>=r)
{
ncr=(float)fact(n)/(fact(n-r)*fact(r));
printf("Binomial co-efficient = %5.2f",ncr);
}
else
printf("Wrong values");
getch();
}
/* Function to find factorial of a number */
long int fact(int n)
{
if (n<=1)
return(1);
else
return(n*fact(n-1));
}

Input:

????
Output:

????
Page 6 of 12 I-BSC/PRACTICALS/C-LANG

MATRIX MULTIPLICATION

Aim: A program to find multiplication of two matrices.


Input Variables: m, n, p, q are integer variables.
‘a’ and ‘b’ are integer type two-dimensional array variables.
Output Variables: ‘c’ is integer type two-dimensional array variable.
Other Variables: i, j, k are integer variables.

Algorithm:
Step 1: Start
Step 2: Read m, n
Step 3: Read p, q
Step 4: If (n != p) then
Display “Multiplication not possible”
Goto step 9
Else
Goto step 5
Endif
Step 5: [Reading first matrix values in mxn order]
Repeat for i=0 to m-1 do
Repeat for j=0 to n-1 do
Read a[i][j]
Endfor
Endfor
Step 6: [Reading second matrix values in pxq order]
Repeat for i=0 to p-1 do
Repeat for j=0 to q-1 do
Read b[i][j]
Endfor
Endfor
Step 7: [Calculating multiplication of two matrices]
Repeat for i=0 to m-1 do
Repeat for j=0 to q-1 do
c[i][j]=0
Repeat for k=0 to n-1 do
c[i][j] = c[i][j] + a[i][k]*b[k][j]
endfor
Endfor
Endfor
Step 8: [Diplaying resultant matrix of mxq order]
Repeat for i=0 to m-1 do
Repeat for j=0 to q-1 do
Display c[i][j]
Endfor
Endfor
Step 9: Stop

PROGRAM:

/* Program to find multiplication of two matrices */


#include<stdio.h>
#include<conio.h>
main( )
{
int a[10][10],b[10][10],c[10][10];
int m,n,p,q,i,j,k;
clrscr( );
printf("Enter 1st matrix order ");
scanf("%d%d",&m,&n);
printf("Enter 2nd matrix order ");
scanf("%d%d",&p,&q);
if (n!=p)
printf("Matrix multiplication is not possible");
I-BSC/PRACTICALS/C-LANG Page 7 of 12
else
{
printf("Enter 1st matrix values in %dx%d order \n",m,n);
for(i=0;i<=m-1;i++)
for(j=0;j<=n-1;j++)
scanf("%d",&a[i][j]);
printf("Enter 2nd matrix values in %dx%d order \n",p,q);
for(i=0;i<=p-1;i++)
for(j=0;j<=q-1;j++)
scanf("%d",&b[i][j]);

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("Resultant matrix is ...\n");
for(i=0;i<=m-1;i++)
{
for(j=0;j<=q-1;j++)
printf("%3d",c[i][j]);
printf("\n");
}
}
getch( );
}

Input:

????

Output:

????

STRING PALINDROME

Aim: A program to find the given string is palindrome or not


Input Variables: ‘str’ of string (character array)
Oher variables: ‘i’ for looping, ‘len’ for string length, ‘check’ for flag

Algorithm:
Step 1: Start
Step 2: Read str
Step 3: len = strlen(str)
Step 4: check = 1, j=len-1
Step 5: Repeat for i=0 to len/2 do
If ( str[i] != str[j] ) then
Check = 0;
Goto step 6
Endif
j=j-1
Endfor
Step 6: If (check = 1) then
Display “Palindrome”
Else
Display “Not Palindrome”
Endif
Step 7: Stop
Page 8 of 12 I-BSC/PRACTICALS/C-LANG
PROGRAM:

/* Program to check the given string is palindrome or not */


#include<stdio.h>
#include<conio.h>
#include<string.h>
main()
{
char str[30];
int i,j,len,check=1;
clrscr();
printf("Enter a string :");
gets(str);
len=strlen(str);
for(i=0,j=len-1;i<len/2;i++,j--)
{
if (str[i]!=str[j])
{
check=0;
break;
}
}
if (check==1)
printf("%s is palindrome",str);
else
printf("%s is not palindrome",str);
getch();
}

Input:

????

Output:

????

BUBBLE SORT

Aim: A program to arrange a list of values in sorted order.


Input Variables: ‘a’ is one dimensional array type which is used to store ‘n’ values
Output Variables: ‘a’ is also used for output to print sorted values

Algorithm:
Step 1: Start
Step 2: Read n
Step 3: Call reading(a, n)
Step 4: Call sorting(a, n)
Step 5: Call printing(a, n)
Step 6: Stop

Algorithm: reading(a, n)
/* This procedure reads ‘n’ values and stores them into array ‘a’. ‘i’ is a temporary integer
variable */
Step 1: Repeat for i = 1 to n do
Read a[i]
End for
Step 2: Return
I-BSC/PRACTICALS/C-LANG Page 9 of 12
Algorithm: sorting(a, n)

/* This procedure sorts ‘n’ values of array ‘a’ in ascending order. i, j, temp are temporary
integer variables */
Step 1: Repeat Step 2 for i = 1 to n-1 do
Step 2: Repeat for j = 1 to n – 1 do
If (a[j] > a[j+1]) then
Temp=a[j]
a[j]=a[j+1]
a[j+1]=temp
Endif
Endfor
Endfor
Step 3: Return

Algorithm: printing(a, n)

/* This procedure displays ‘n’ values of array ‘a’. ‘i’ is a temporary integer variable */
Step 1: Repeat for i = 1 to n do
Display a[i]
End for
Step 2: Return

PROGRAM:

/* Program to arrange a list of values in ascending order */


#include <stdio.h>
#include <conio.h>
main()
{
int a[50],n;
void reading(int a[],int n);
void sorting(int a[],int n);
void printing(int a[],int n);
clrscr();
printf("How many values? ");
scanf("%d",&n);
reading(a,n);
sorting(a,n);
printing(a,n);
getch();
}
/* Function to read values into an array */
void reading(int a[],int n)
{
int i;
printf("Enter %d values \n",n);
for(i=1;i<=n;i++)
scanf("%d",&a[i]);
}
/* Function to sort values of an array */
void sorting(int a[],int n)
{
int i,j,temp;
for(i=1;i<=n-1;i++)
for(j=1;j<=n-1;j++)
if (a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
Page 10 of 12 I-BSC/PRACTICALS/C-LANG
/* Function to display sorted values */
void printing(int a[],int n)
{
int i;
printf("Sorted values are ...\n");
for(i=1;i<=n;i++)
printf("%d \n",a[i]);
}

Input:

????

Output:

????

DYNAMIC MEMORY ALLOCATION

Aim: A program to search a value in the list of values using Dynamic Memory Allocation
Input Variables: ‘a’ is pointer variable to store ‘n’ values. ‘num’ is integer variable
Other Variables: ‘pos’ and ‘i' are integer variables

Algorithm:
Step 1: Start
Step 2: pos = 0
Step 3: Read n
Step 4: a=(int *)malloc(n*sizeof(int))
Step 5: Repeat for i=0 to n-1 do
Read a+i
endfor
Step 6: Read num
Step 7: Repeat for i=0 to n-1 do
if (*(a+i)==num) then
pos=i+1;
goto step 8;
endif
Step 8: If (pos = = 0) then
Display “number not found”
Else
Display pos
Endif
Step 9:Stop

PROGRAM:

/* Program to use Dynamic Memory Allocation */


#include <stdio.h>
#include <conio.h>
main()
{
int *a,n,num,pos=0,i;
clrscr();
printf("How many values? ");
scanf("%d",&n);
a=(int *)malloc(n*sizeof(int));
printf("Enter %d values \n",n);
for(i=0;i<=n-1;i++)
scanf("%d",a+i);
printf("Enter a number to search ");
scanf("%d",&num);
I-BSC/PRACTICALS/C-LANG Page 11 of 12
for(i=0;i<=n-1;i++)
{
if (*(a+i)==num)
{
pos=i+1;
break;
}
}
if (pos==0)
printf("%d not found",num);
else
printf("%d is found at position %d",num,pos);
getch();
}

Input:

????

Output:

????

STUDENT RESULT PROCESSING

Aim: A program to find result of students in a class.


Input Variables: ‘s’ is a student type array variable which is used to store ‘n’ students
details.
Here, ‘student’ is a user-defined data type with the following fields.
sno, snm, m1, m2, m3, tot, avg, result
Output Variables: Input variable ‘s’ is output variable
Other Variables: ‘i’ is an integer.

Algorithm:
Step 1: Start
Step 2: Read n
Step 3: [reading student details]
Repeat for i=1 to n do
Read s[i].sno, s[i].snm, s[i].m1, s[i].m2, s[i].m3
Endfor
Step 4: [Finding total, average and result of students]
Repeat for i=1 to n do
s[i].tot = s[i].m1+ s[i].m2+ s[i].m3
s[i].avg = s[i].tot / 3.0
if (s[i].m1>=35 and s[i].m2>=35 and s[i].m3>=35) then
s[i].result = “Pass”
Else
s[i].result=”Fail”
Endif
Endfor
Step 5: [Displaying student details]
Repeat for i=1 to n do
Display s[i].sno, s[i].snm, s[i].m1, s[i].m2, s[i].m3, s[i].tot, s[i].avg, s[i].result
Endfor
Step 6: Stop
Page 12 of 12 I-BSC/PRACTICALS/C-LANG
PROGRAM:

/* Program to find result of students in a class */


#include <stdio.h>
#include <conio.h>
#include <string.h>
struct student
{
int sno;
char snm[30];
int m1,m2,m3,tot;
float avg;
char result[5];
};
typedef struct student stud;
main()
{
stud s[40];
int i,n;
clrscr();
printf("How many students? ");
scanf("%d",&n);
/* Reading student details */
for(i=1;i<=n;i++)
{
printf("Enter details for student %d \n",i);
printf("Student number ");
scanf("%d",&s[i].sno);
fflush(stdin);
printf("Student name ");
gets(s[i].snm);
printf("Marks in 3 subjects ");
scanf("%d%d%d",&s[i].m1,&s[i].m2,&s[i].m3);
}
/* finding total, average and result of students */
for(i=1;i<=n;i++)
{
s[i].tot=s[i].m1+s[i].m2+s[i].m3;
s[i].avg=s[i].tot/3.0;
if (s[i].m1>=35 && s[i].m2>=35 && s[i].m3>=35)
strcpy(s[i].result,"Pass");
else
strcpy(s[i].result,"Fail");
}
printf("\n sno student name m1 m2 m3 tot avg result ");
for(i=1;i<=n;i++)
{
printf("\n%4d %-30s %4d %4d %4d %4d %5.2f %-5s",
s[i].sno,s[i].snm,s[i].m1,s[i].m2,s[i].m3,s[i].tot,s[i].avg,s[i].result);
}
getch( );
}

Input:

????

Output:

????

You might also like