Introduction
Basic Programs Generally speaking, in order to call your software developer or software engineer, you need to start learning what the basic software development needs and what the coding is.
Which means you need to start programming for sure first!
You can now go and learn about data structures and algorithms after you are able to understand and solve basic programming problems such as addition, subraction etc. using computer programming.
Many Fresher Programmer usually ask me : –
Should I need to learn Basic Program/ Algorithms for Programming?
Let’s get some things straight. ‘Yes’ you should need to learn Basic Algorithms, Programming is all about applying logic and reason to solving programming problems.
An algorithm represents your solution to a programming problem or Application Development.
Basic Program/Algorithms Every Programmer Must Should Know to improve programming logics.
The algorithm is not the code of the computer. Algorithm is just the instructions that give you a clear idea to write computer code.
Examples Of An Basic Program / Algorithms In C Programming.
- Program to add two numbers.
- A Program to find largest of three number.
- Program Logic to find all roots of a quadratic equation
- A program to find factorial of a number
- Program for Fibonacci numbers.
1. Program to add two numbers
Write a Algorithm to add 2 numbers entered by the Users.
Basic Algorithm to add 2 given number
Step 1: Start Step 2: Declare variables num1, num2 and sum. Step 3: Read values num1 and num2. Step 4: Add num1 and num2 and assign the result to sum. sum←num1+num2 Step 5: Display sum Step 6: Stop
c Basic Program to add 2 numbers
#include<stdio.h> int main() { int a, b, c; printf("Enter two numbers to add\n"); scanf("%d%d", &a, &b); c = a + b; printf("Sum of the numbers = %d\n", c); return 0; }
Output
Enter two numbers to add 4 5 Sum of the numbers = 9
2. Program to find largest of three number.
Algorithm to find largest of three number
Step 1: Start Step 2: Declare variables a,b and c. Step 3: Read variables a,b and c. Step 4: If a>b If a>c Display a is the largest number. Else Display c is the largest number. Else If b>c Display b is the largest number. Else Display c is the greatest number. Step 5: Stop
1. Read 3 numbers and store it in the variables a, b and c respectively.
2. Check out step 4 We check if the a is greater than b.
3. If it is greater, then check a if it is greater than c.
4. If it is, then print the output as “a is the greatest among three”.
5. Otherwise print the ouput as “c is the greatest among three”.
6. If the a is not greater than b, then check if b is greater than c.
7. If it is, then print the output as “b is the greatest among three”.
8. Otherwise print the output as “c is the greatest among three”.
C program to find the biggest of three numbers
/* * C program to find the biggest of three numbers */ #include <stdio.h> void main() { int a, b, c; printf("Enter the values of a, b and c\n"); scanf("%d %d %d", &a, &b, &c); if (a > b) { if (a > c) { printf(" a is the greatest among three \n"); } else { printf("c is the greatest among three \n"); } } else if (b > c) printf("b is the greatest among three \n"); else printf("c is the greatest among three \n"); }
Output
Output:1 Enter the values of a, b and c 6 8 10 c is the greatest among three Output:2 Enter the values of a, b and c 10 100 99 b is the greatest among three
3. Program Logic to find all roots of a quadratic equation
Write a C program to find all roots of a quadratic equation using if else. How to find all roots of a quadratic equation using if else in C programming. Logic to find roots of quadratic equation in C programming.
Algorithm to find root of quadratic equation
Step 1: Start Step 2: Declare variables a, b, c, D, x1, x2, rp and ip; Step 3: Calculate discriminant D←b2-4ac Step 4: If D≥0 r1←(-b+√D)/2a r2←(-b-√D)/2a Display r1 and r2 as roots. Else Calculate real part and imaginary part rp←b/2a ip←√(-D)/2a Display rp+j(ip) and rp-j(ip) as roots Step 5: Stop
C Program to find root of quadratic equation.
/** * C program to find all roots of a quadratic equation */ #include <stdio.h> #include <math.h> /* Used for sqrt() */ int main() { float a, b, c; float root1, root2, imaginary; float discriminant; printf("Enter values of a, b, c of quadratic equation (aX^2 + bX + c): "); scanf("%f%f%f", &a, &b, &c); /* Find discriminant of the equation */ discriminant = (b * b) - (4 * a * c); /* Find the nature of discriminant */ if(discriminant > 0) { root1 = (-b + sqrt(discriminant)) / (2*a); root2 = (-b - sqrt(discriminant)) / (2*a); printf("Two distinct and real roots exists: %.2f and %.2f", root1, root2); } else if(discriminant == 0) { root1 = root2 = -b / (2 * a); printf("Two equal and real roots exists: %.2f and %.2f", root1, root2); } else if(discriminant < 0) { root1 = root2 = -b / (2 * a); imaginary = sqrt(-discriminant) / (2 * a); printf("Two distinct complex roots exists: %.2f + i%.2f and %.2f - i%.2f", root1, imaginary, root2, imaginary); } return 0; }
output
Enter values of a, b, c of quadratic equation (aX^2 + bX + c): 8 -4 -2 Two distinct and real roots exists: 0.81 and -0.31
4. Program to find factorial of a number
Factorial program in C programming language: Factorial is represented using ‘!’, so five factorial will be written as (5!), n factorial as (n!). Also,
n! = n*(n-1)*(n-2)*(n-3)…3.2.1
i.e 5*4*3*2*1
zero factorial is defined as one i.e., 0! = 1.
Algorithm to solve factotial of a given number
Step 1: Start Step 2: Declare variables n,factorial and i. Step 3: Initialize variables factorial←1 i←1 Step 4: Read value of n Step 5: Repeat the steps until i=n 5.1: factorial←factorial*i 5.2: i←i+1 Step 6: Display factorial Step 7: Stop
Program to solve factotial of a given number
#include <stdio.h> int main() { int i, n, fact = 1; printf("Enter a number to calculate its factorial\n"); scanf("%d", &n); for (i = 1; i <= n; i++) { fact = fact * i; } printf("Factorial of %d = %d\n", n, fact); return 0; }
Output
Enter a number to calculate its factorial 6 Factorial of 6 = 720
5 . Program for Fibonacci Series in C
In case of fibonacci series, For example 0, 1, 1, 2, 3, 5, 8, 13, 21 etc.
The first two numbers of fibonacci series are 0 and 1
next number is the sum of previous two numbers.
.Here 0+1 = 1 which is the third number, as we continue 1+1 = 2 which is forth number, and 1+2=3 and so on
Algorithm to display fibonacci series
Step 1: Start Step 2: Declare variables n1,n2,n3,number and i. Step 3: Initialize variables n1=0 n2=1 i←1 Step 4: Read value of number Step 5: Repeat the steps until i<number 5.1: n3=n1+n2 5.2: Disply n3 5.3: n1=n2 5.4: n2=n3 Step 6: Stop
Basic C Program to display fibonacci series
#include<stdio.h> int main() { int n1=0,n2=1,n3,i,number; printf("Enter the number of elements:"); scanf("%d",&number); printf("\n%d %d",n1,n2);//printing 0 and 1 for(i=2;i<number;++i)//loop starts from 2 because 0 and 1 are already printed { n3=n1+n2; printf(" %d",n3); n1=n2; n2=n3; } return 0; }
Output
Enter the number of elements:15 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377
Referals website
https://www.javatpoint.com/c-programming-language-tutorial
https://www.programiz.com/article/algorithm-programming
https://www.programmingsimplified.com/c-program-find-factorial