Sunday, December 15, 2019

Write a program to find gcd of two numbers using recursion.

About: 
The process in which a function calls itself is called recursion and the corresponding function is called as recursive function.
For example: In below program, gcd() is used for calculating gcd of n1 and n2 and we are calling the same function again and again until the value of n2 is equal to 0.

Program:
#include <stdio.h>
int gcd(int n1, int n2) {
if (n2 != 0)
return gcd(n2, n1 % n2);
else
return n1;
}
void main() {
int n1, n2;
printf("Enter two positive integers: ");
scanf("%d %d", &n1, &n2);
printf("G.C.D of %d and %d is %d.", n1, n2, gcd(n1, n2));
}

Write a program to find gcd of two numbers.

About:
The HCF or GCD of two integers is the largest integer that can exactly divide both numbers (without a remainder).
For example:81 and 153
The gcd(81,153)=9.

Program:
#include<stdio.h>
void main()
{
int n1,n2,gcd;
printf("Enter any two no:");
scanf("%d%d",&n1,&n2);
for(int i=1;i<=n1&&i<=n2;++i){
if(n1%i==0&&n2%i==0)
gcd=i;
}
printf("The gcd of %d and %d=%d",n1,n2,gcd);
}

Write a program to check Armstrong Number or not.

About:
A positive integer is called an Armstrong number (of order n) if
abcd...=an+bn+cn+dn+.....
In the case of an Armstrong number of 3 digits, the sum of cubes of each digit is equal to the number itself. For example, 153 is an Armstrong number because
153=1*1*1+5*5*5+3*3*3

Program:
#include<stdio.h>
void main()
{
int a,n,s=0,l;
printf("Enter any three-digit no:");
scanf("%d",&n);
int b=n;
while(n){
a=n%10;
s=s+(a*a*a);
n=n/10;
}
if(b==s)
printf("Given number is Armstrong");
else
printf("Not Armstrong number");
}

Write a program for Fibonacci series.

For example:0 1 1 2 3 5 8 13......

Program:
#include <stdio.h>
void main()
{
    int s=0,n,a,b;
    a=0,b=1;
    printf("enter any no:");
    scanf("%d",&n);
    printf("Fibonacci series:\n");
    printf("%d",a);
    printf("\t%d",b);
    printf("\t");
    for(int i=0;i<n;++i){
        s=a+b;
        a=b;
        b=s;
        printf("%d",s);
        printf("\t");
    }
}

Write a program to find gcd of two numbers using recursion.

About:  The process in which a function calls itself is called recursion and the corresponding function is called as recursive function. ...