C program and algorithm to find GCD and LCM

Algorithm
1. Start
2. read two numbers a,b
3. set n=b,d=a
4. r=a%b
5. while r!=0
   5.1 set n=d,d=r
   5.2 r=n%d
6. set gsc=d
7. lcm=(a*b)/gcd
8. display gcd and lcm
9. stop

Program

#include<stdio.h>
void main()
{
int a,b,n,d,r,gcd,lcm;
printf("Enter two numbers\n");
scanf("%d%d",&a,&b);
n=b;
d=a;
r=a%b;
while(r!=0)
{
n=d;
d=r;
r=n%d;
}
gcd=d;
lcm=(a*b)/gcd;
printf("GCD of %d and %d is %d\n",a,b,d);
printf("LCM is %d\n",lcm);
}

Menu driven C program and algorithm to perform palindrome, armstrong, factorial, fibonacci.

Algorithm

1.start
2.do
3.display 1. fibonacci series 2. palindrome 3. armstrong 4. factorial 
4.read choice
5.if choice=1
   5.1 read limit
   5.2 c=1,a=0,b=0
   5.3 while c<=limit
   5.4 display c
   5.5 a=c+b
   5.6 b=c
   5.7 c=a go to step 5.3
6. else if choice=2
   6.1 read number
   6.2 set rev=0,rem=0 and dup=num
   6.3 repeat steps 6.4 to 6.6 until num>0
   6.4 rem=num%10
   6.5 rev=(rev*10)+rem
   6.6 num=num/10
   6.7 after num>0 is false check if rev=dup
   6.8 display number is palindrome
   6.9 else display number isn't palindrome
7. else if choice =3
   7.1 read number
   7.2 set dup=num
   7.3 repeat steps 7.4 to 7.6 until num>0
   7.4 rem=num%10
   7.5 sum=sum+(rem*rem*rem)
   7.6 num=num/10
   7.7 after condition is false check if sum==dup
   7.8 display number is armstrong
   7.9 else number isn't armstrong
8. else if choice =4
   8.1 read number
   8.2 set fact=1,i=2
   8.3 if i<=num
   8.4 fact=fact*i
   8.5 i++ go to step 8.3
   8.6 after condition is false
   8.7 display fact
   8.8 else
9. default display invalid choice
10. stop

Program
# include<stdio.h>
#include<conio.h> //only windows users
void main()
{
clrscr();               //only for windows users
int ch,a,b,c,rem,rev,i,num,dup,sum,fact,lim;
a=0;
b=0;
c=1;
do
{
printf("\n 1. fibonacci");
printf("\n 2. palindrome");
printf("\n 3. armstrong");
printf("\n 4. factorial");
printf("\n  Enter your choice");
scanf("%d",&ch);
switch(ch)
{
case 1:
{
printf("\n enter the limit");
scanf("%d",&lim);
while(c<=lim)
{
printf("\n%d\t",c);
a=c+b;
b=c;
c=a;
}
break;
}
case 2:
{
printf("\n enter a number");
scanf("%d",&num);
rev=0;rem=0;
dup=num;
while(num>0)
{
rem=num%10;
rev=(rev*10)+rem;
num=num/10;
}
if(rev==dup)
printf("\n number is palindrome");
else
printf("\n number is not palindrome");
break;
}
case 3:
{
printf("\n enter the number");
scanf("%d",&num);
rem=0;sum=0;
dup=num;
while(num>0)
{
rem=num%10;
sum=sum+(rem*rem*rem);
num=num/10;
}
if(sum==dup)
printf("\n the number is armstrong");
else
print("\n the number is not armstrong");
break;
}
case 4:
{
printf("\n enter a number");
scanf("%d",&num);
fact=1;
for(i=2;i<=num;i++)
{
fact=fact*i;
}
printf("\n factorial of %d=%d\n",num,fact);
break;
}
default: printf("\n INVALID ENTRY");
}
}while(ch!=5);
getch();             //only for windows users
}


C program and algorithm to print prime numbers upto a limit

Algorithm

  1. start
  2. set i=1,j=1
  3. read limit
  4. if limit =1
  5. display one is neither prime nor composite
  6. else
  7. repeat steps 8 to 14 if i<=limit,count=0
  8. repeat steps 9 to 11 if j<=i
  9. if i%j==0
  10. count++
  11. j+1 go to step 8
  12. if count==2
  13. display i
  14. i+1 go to step 4
  15. else
  16. stop
Program

#include<stdio.h>
#include<conio.h>   //only for windows users
void main()
{
clrscr();   //only for windows users
int lim,i,j,count;
printf("\n enter the limit:\t");
scanf("%d",&lim);
if(lim==1)
{
printf("\n one is neither prime nor composite");
}
else
for(i=1;i<=lim;i++)
{
count=0;
for(j=1;j<=i;j++)
{
if(i%j==0)
count++;
}
if(count==2)
printf("%d",i);
}
getch();   \\only for windows users
}

Output

enter the limit: 10
2 3 5 7

C program and algorithm to print numbers upto a limit

Algorithm

  1. start
  2. read limit
  3. set i=0
  4. if i<=limit
  5. display i
  6. i+1 go to 4
  7. else
  8. stop
Program

#include<stdio.h>
#include<conio.h>   //only for windows users
void main()
{
clrscr();   //only for windows users
int lim,i;
printf("\n enter the limit:\t");
scanf("%d",&lim);
for(i=0;i<=lim;i++)
{
printf("%d\t",i);
}
getch();   \\only for windows users
}

Output

enter the limit: 5
0 1 2 3 4 5

C program and algorithm to generate a pattern

Algorithm


  1. start
  2. input limit
  3. set i=1 and j=1
  4. repeat step 5 to step 8 until i<=limit otherwise go to step 9
  5. repeat step 6 until j<=i otherwise go to step 7
  6. display *
  7. i++
  8. go to step 4
  9. stop
Program

#include<stdio.h>
#include<conio.h>   //only windows users
void main()
{
int lim,i,j;
printf("\n enter the limit:\t");
scanf("%d",&lim);
for(i=1;i<=lim;i++)
{
printf("\n");
for(j=1;j<=i;j++)
{
printf("*");
}
}
getch();           //only windows users
}

Output

enter the limit: 4
*
**
***
****




C program and algorithm to check whether a number is armstrong or not.

ALGORITHM

  1. Start
  2. read number
  3. set sum=0 and duplicate=number
  4. reminder=number%10
  5. sum=sum+(reminder*reminder*reminder)
  6. number=number/10
  7. repeat steps 4 to 6 until number > 0
  8. if sum = duplicate
  9. display number is armstrong
  10. else 
  11. display number is not armstrong
  12. stop


PROGRAM



#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int n,s,r,dup;
s=0;
printf("\n Enter a number please:\t");
scanf("%d",&n);
dup=n;
while(n>0)
{
r=n%10;
s=s+(r*r*r);       //or you can use pow(r,3) and use the header file math.h
n=n/10;
}
if(dup=s)
printf("\n %d is an armstrong number",dup);
else
printf("\n %d is not an armstrong
number",dup);
getch();
}


LOGIC



An armstrong number: the sum of cube of its digits results in that number itself.

eg: 153 = (1*1*1)+(5*5*5)+(3*3*3) = 1+125+27 =153.


r=n%10:

153%10 gives the reminder, that is the number 3 here. The modulus operator is used here to seperate the digits. The other thing is that " a small number % large number= that small number itself ".
eg: 3%5= 3 (reminder)


n=n/10:

after a modulus operation we will get the last digit of that number now we have to remove the last digit from the input number so we divide it by 10 and what happens is:



153%10=3 (reminder,r=3)
sum=0+(3*3*3) (sum,s=27)
153/10=15 (number,n=15)


the actual answer is 15.3 but since we initialize it as an integer the floating point value isn't considered. So the calculation continues like:

now the value of n is 15 after the division by 10.


15%10=5                      (reminder,r=5)

sum=(3*3*3)+(5*5*5)  (sum,s=152)
15/10=1                         (number,n=1)



and this continues till the number becomes 0 that is the while condition (n>0) evaluates to false.



OUTPUT


Enter a number please: 153

153 is an armstrong number