Algorithms
Algorithms
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
PROGRAM:
Page 2 of 12 I-BSC/PRACTICALS/C-LANG
Input:
????
Output:
????
I-BSC/PRACTICALS/C-LANG Page 3 of 12
ARMSTRONG NUMBER
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:
Input:
????
Output:
????
Page 4 of 12 I-BSC/PRACTICALS/C-LANG
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:
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
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:
Input:
????
Output:
????
Page 6 of 12 I-BSC/PRACTICALS/C-LANG
MATRIX MULTIPLICATION
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:
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
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:
Input:
????
Output:
????
BUBBLE SORT
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:
Input:
????
Output:
????
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:
Input:
????
Output:
????
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:
Input:
????
Output:
????