K12

Table of a given number

Copy code Algorithm Input Any integer number Output Its table from 1 to 10 Complexity O(1) Table (num) 1 for r=1 to 10 2 print ”num*r” 3 r=r+1 Algorithm Description Initialize the value of r from 1 and increment it up to 10 to print the table of number by multiplying it with the number. […]

By admin | K12
DETAIL

Find whether a number is prime or not

Copy code Algorithm Input Any integer number (up to 32767) Output Is it a prime number or Not Complexity O(n) Prime(num) 1 Set i=2 2 while i #include< conio.h> int main() { int i,num; printf(“Enter a number”); scanf(“%d”,&num); i=2; while(i

By admin | K12
DETAIL

L-R shift operator

Copy code Algorithm Input integer number Output Corresponding number after shifting Complexity O(1) L_R_Shift (a,s,ch) [‘a’ is any input number and s is the shift which needs to be done on the input number and ch takes the choice entered by user either for left shift or right shift] 1 if ch=’l’ OR ch = […]

By admin | K12
DETAIL

Decimal to Binary

Copy code Algorithm Input Any integer number (up to 32767) Output Binary equivalent of input no. Complexity O(n) Dec_to_Bin(A ,num) 1 Set i=0 2 while num>0 3 A[i]=num%2 4 num=num/2 5 i=i+1 6 for n=i-1 to 0 7 print “its binary number is A[n] Algorithm Description let us convert a decimal number 95 to binary […]

By admin | K12
DETAIL

Swap 2 numbers without using third variable

Copy code Algorithm Input Two numbers a and b to be swapped Output Numbers after swapping complexity O(1). Swap(a , b) 1. a=a+b; 2. b=a-b; 3. a=a-b; 4. print a and b Algorithm Description This algorithm takes two integers and swaps their values without using temporary variable Copy code C Code //Swap two numbers without […]

By admin | K12
DETAIL

Reverse digits of a number

Copy code Algorithm Input An integer Output Reverse of number complexity O(d) where d is the number of digits in the number Digitsreverse(num) 1 while (num>0) then 2 digit =num%10; 3 print digit; 4 num= num/10; Algorithm Description This algorithm takes an integer and reverses its digits. It keeps on dividing it by 10 to […]

By admin | K12
DETAIL

Find simple interest

Copy code Algorithm Input P(principal amount), r( Rate of interest) ,t(number of years); Output I(interest) complexity O(1) 1 Simpleinterest(p, r , t) 2 I =p*r*t; 3 Return I Algorithm Description Formula for calculating compound interest: Interest=principal amount *time *rate Where, P = principal amount (initial investment) r = annual nominal interest rate (as a decimal) […]

By admin | K12
DETAIL

Find leap year

Copy code Algorithm Input An year Output Leap year or not complexity O(1). leapyear(year) 1 If (year %4==0 and year%100 !=0) 2 print “it is a leap year”; 3 else 4 if(year%400==0) 5 print “it is a leap year”; 6 else 7 print “it is not a leap year”; Algorithm Description Copy code C Code […]

By admin | K12
DETAIL

LCM of two numbers

Copy code Algorithm Input Two integer a and b Output GCD of a and b. complexity O(d) where d is the number of digits in the smaller number gcd(a, b) 1 if (b = 0) then 2 Return a 3 else 4 Return gcd(b, a mod b) Algorithm Description LCM () function takes two parameter […]

By admin | K12
DETAIL

GCD of two numbers

Copy code Algorithm Input Two integer a and b Output GCD of a and b. complexity O(d) where d is the number of digits in the smaller number gcd(a, b) 1 if (b = 0) then 2 Return a 3 else 4 Return gcd(b, a mod b) Algorithm Description Gcd () function takes two parameter […]

By admin | K12
DETAIL

Find factorial of a number

Copy code Algorithm Input An integer. Output Factorial of given number. complexity O(n) Factorial(num) 1 if (num=0 or num=1) then 2 fact = 1; 3 else 4 for i 1 to n 5 fact=fact*i; 6 print fact Algorithm Description Factorial of a number is multiplying the numbers from 1,2,3…n where n is the number whose […]

By admin | K12
DETAIL

Find compound interest

Copy code Algorithm Input P(Principal Amount), r(Rate of Interest) , n(Number of times interest is calculated in a year) , t(number of years); Output compound interest Complexity O(1) 1 Compoundinterest(p, r , n , t) 2 A = P*(1+(r/n))^nt; 3 CI = A – p; 4 Return CI Algorithm Description Compound interest arises when interest […]

By admin | K12
DETAIL

Convert celsius to farenheit

Copy code Algorithm Input Temperature in Celsius. Output Temperature in Fahrenheit. complexity O(1) CelsiustoFahrenheit(Tc) 1 Tf = (9/5)*Tc+32; 2 print Tf. Algorithm Description Tc = temperature in degrees Celsius, Tf = temperature in degrees Fahrenheit. Copy code C Code //To convert the temperature from Celsius to Fahrenheit //Input : Enter any integer #include< stdio.h> #include< […]

By admin | K12
DETAIL

Validity of a triangle

Copy code Algorithm Input Three angle of triangle Output Valid or invalid complexity O(1) Triangle_valid(angle1, angle2, angle3) 1 if (angle1 = 0 or angle2 = 0 or angle3 = 0) then 2 print triangle is invalid 3 else 4 sum=angle1 + angle2 +angle3; 5 if (sum = 180) then 6 print triangle is valid; 7 […]

By admin | K12
DETAIL

Average of numbers

Copy code Algorithm Input Set of numbers and count of numbers n Output Average of numbers complexity O(n) Average(A) 1 Set n=size of array. 2 Set sum =0; 3 for i=1 to n 4 sum=sum + A[i]; 5 avg = sum/n; 6 print avg; Algorithm Description Algorithm adds up all the elements in the array […]

By admin | K12
DETAIL

X^Y

Copy code Algorithm Input Two integer x and y Output Print x^y complexity O(logn) power(x,y) 1 if (y==0) then 2 return 1; 3 else if(y%2==0) 4 return power(x,y/2)*power(x,y/2); 5 else 6 return x*power(x,y/2)*power(x,y/2); Algorithm Description The algorithm takes advantage of the fact that if n is an even number then xn=xn/2*xn/2 otherwise xn=x*xn/2*xn/2 By this […]

By admin | K12
DETAIL

Sum of n numbers

Copy code Algorithm Input N (The numbers from 1 to N will be added) Output Sum of first n numbers complexity O(1) Sum_Num(N) 1 Sum=N*(N+1)/2; 2 Print sum; Algorithm Description This algorithm adds all number from 1 to N. To add first N number program uses the formula sum=N*(N+1)/2; Copy code C Code //To find […]

By admin | K12
DETAIL

Matrix Substraction

Copy code Algorithm Input Two matrices a and b Output Output matrix c containing elements after subtraction of b from a complexity O(n^2) Matrix-Subtraction(a,b) 1 for i =1 to rows [a] 2 for j =1 to columns[a] 3 Input a[i,j]; 4 Input b[i,j]; 5 C[i, j] = A[i, j] – B[i, j]; 6 Display C[i,j]; […]

By admin | K12
DETAIL

Matrix Multiplication

Copy code Algorithm Input two matrixes. Output Output matrix C. Complexity O(n^3) Matrix-Multiply(A, B) 1 if columns [A] ≠ rows [B] 2 then error “incompatible dimensions” 3 else 4 for i =1 to rows [A] 5 for j = 1 to columns [B] 6 C[i, j] =0 7 for k = 1 to columns [A] […]

By admin | K12
DETAIL

Even and Odd

Copy code Algorithm Input An integer. Output Print number is even or odd. Complexity O(1) Even-odd(n) 1 If (n%2=0) then 2 Print number is even 3 else 4 Print number is odd Algorithm Description Even-odd() function takes an integer and checks whether n is divisible by 2 or not.if its remainder is0 then number is […]

By admin | K12
DETAIL

Division of two numbers

Copy code Algorithm Input Two numbers Output Quotient of two numbers Complexity O(n^2) Division (num1, num2) div=num1/num2; print div; Algorithm Description This function takes two integer num1 and num2 and divides number1 from number2 and assign its value to div which stores quotient after division. Copy code C Code //Division of two numbers //Input : […]

By admin | K12
DETAIL

Decimal to Octal

Copy code Algorithm Input A decimal number Output An octal number complexity O(d)where d is the number of digits in the number Decimal_to_octal(num) 1 while (num>0) then 2 a[i] =num%8; 3 num= num/8; 4 i=i+1; 5 Print a[i] in reverse order Algorithm Description This algorithm takes an integer and converts it to octal number. The […]

By admin | K12
DETAIL

Ascii value of [A to Z and a to z]

Copy code Algorithm Output: ASCII value of [A to Z and a to z] complexity:O(1) ASCII( ) 1 for i=65 to 90 2 print i and character value of i 3 for i=97 to 122 4 print i and character value of i Algorithm Description we have to print the character counterpart of the integer […]

By admin | K12
DETAIL

Addition of two matrices

Copy code Algorithm Input Two matrices a and b Output Output matrix c containing elements after addition of a and b complexity O(n^2) Matrix-Addition(a,b) 1 for i =1 to rows [a] 2 for j =1 to columns[a] 3 Input a[i,j]; 4 Input b[i,j]; 5 C[i, j] = A[i, j] + B[i, j]; 6 Display C[i,j]; […]

By admin | K12
DETAIL