What is an algorithm?
Algorithm is a procedure or step by step process for solving a problem.
- Algorithms can be expressed using different kinds of notations, including pseudocode, flowcharts, programming languages and natural languages.
- Computer algorithm is an instance of logic written in computer software by the programmers.
Let’s learn some of the algorithm. Sample problem definition and algorithm is given below:
1. Problem Definition: Write an algorithm to add given two numbers.
Step 1: Start.
Step 2: Read two numbers A and B.
Step 3: Total = A+B.
Step 4: Display Total.
Step 5: Stop.
Algorithm to C program conversion
Example of Algorithm to C program conversion.
Problem Definition: Write an algorithm to add given two numbers.
Step 1: Start.
Problem Definition: Write an algorithm to add given two numbers.
Step 1: Start.
Step 2: Read two numbers A and B.
Step 3: Total = A+B.
Step 4: Display Total.
Step 5: Stop.
#include<stdio.h>
int main()
{
int A, B, Answer;
printf("Enter number A:");
scanf("%d",&A);
printf("Enter number B:");
scanf("%d",&B);
Answer = A + B;
printf("%d", Answer);
return 0;
}
Output of program:
Enter number A:5
Enter number B:10
15
Enter number A:5
Enter number B:10
15
2. Problem Definition: Write an algorithm for Subtracting two Numbers.
Step 1: Start.
Step 2: Read two numbers A and B.
Step 3: Answer = A - B.
Step 4: Display SUB.
Step 5: Stop.
Example of Algorithm to C program conversion.
Example of Algorithm to C program conversion.
Step 1: Start.
Step 2: Read two numbers A and B.
Step 3: Answer = A - B.
Step 4: Display Answer.
Step 5: Stop
#include<stdio.h>
int main()
{
int A, B, Answer;
printf("Enter number A:");
scanf("%d",&A);
printf("Enter number B:");
scanf("%d",&B);
Answer = A - B;
printf("%d", Answer);
return 0;
}
3. Problem Definition: Write an algorithm to find factorial of a given number.
Step 1: Start
Step 2: Declare variables: N, fact, i.
Step 3: Initialize variables
fact = 1
i = 1
Step 4: Read value of N from user.
Step 5: Repeat following steps until i = N
fact = fact * i
i = i + 1
Step 6: Print fact variable.
Step 7: Stop
C program to find factorial of a given number.
C program to find factorial of a given number. Factorial of number n is defined as follow:
n! = 1 * 2 * 3 * 4 * ....* n
int main()
{
int i, n;
double fact=1;
printf("Enter number (n): ");
scanf("%d",&n);
if (n < 0)
printf("Factorial is not possible for negative number.");
else
{
for(i=1; i<=n; i++)
{
fact = fact * i;
}
printf("Factorial of number %d = %f", n, fact);
}
}
5. Problem Definition: Write an algorithm for dividing given two numbers.
Step 1: Start.
Step 2: Read two numbers A and B.
Step 2: Read two numbers A and B.
Step 3: Answer = A / B.
Step 4: Display Answer.
Step 5: Stop
C programs of known algorithms
Bubble sort algorithm implementation.
#include<stdio.h> int main() { int i, j, n, temp, data[10]; printf("Enter total numbers to be sorted:"); scanf("%d",&n); //This program will be able to sort max 10 numbers. for(i=0; i<n; i++) { printf("Enter data[%d]:",i); scanf("%d",&data[i]); } printf("Unsorted data:"); for(i=0; i<n; i++) printf("%d, ", data[i]); for(i=0; i<n; i++) { for(j=0; j<n-i-1; j++) { if(data[j] > data[j+1]) { temp = data[j]; data[j] = data[j+1]; data[j+1] = temp; } } } printf("\nSorted data:"); for(i=0; i<n; i++) printf("%d, ", data[i]); return 0; } |
Output of the program:
Enter total numbers to be sorted:5
Enter data[0]:1
Enter data[1]:3
Enter data[2]:2
Enter data[3]:4
Enter data[4]:5
Unsorted data:1, 3, 2, 4, 5,
Sorted data:1, 2, 3, 4, 5,
No comments:
Post a Comment