Adsense 728 * 15

Search

Custom Search

Program to Find GCD or HCF of Two Numbers using Recursion - C, C++ Code

Write a program to find GCD or HCF of  two numbers. In this tutorial, we are going to write following programs.


  • Write a C program to find GCD or HCF of two numbers.
  • Write a C++ program to find GCD or HCF of two numbers.


In this example, our program will take two input integer and then compute it's GCD or HCF using recursion. If you don't know about recursion then read this awesome post on recursion vs iteration.





What is GCD (Greatest Common Divisior) or HCF (Highest Common Factor) ?


GCD or HCF of two  numbers is the largest number which divides both the number without leaving any remainder.

Binary Search program in C, C++

Let's take an example -

Take two numbers 63 and 21.

Divisible of 63 is 3, 7, 9, 21 ,63

Divisible of 21 is 3, 7, 21

Highest Divisible of both the numbers without leaving any remainder is 21.

Check whether number is Armstrong or not

C++ Program to Find GCD of Two Numbers using Recurison


#include <iostream>
using namespace std;

int gcd(int a, int b) {
 
    // If b is zero
    if(b == 0){
       return a;
    }
 
    return gcd(b, a%b);
}

int main() {
 
     int num1, num2, result;
 
     cout << "Enter two numbers \n";
     cin  >> num1 >> num2;
 
     result = gcd(num1, num2);
 
     cout << "GCD of two numbers is " << result;
 
     return 0;
}



C Program to Find GCD of Two Numbers using Recursion


#include <stdio.h>

int gcd (int a, int b) {

      /* If b is equal to zero. */

      if(b == 0) {
          return a;
      }
      
      // Recursive call
      
      gcd(b, a%b);
}

int main(void) {

  int a, b, g;
 
  printf("Enter two numbers \n");
  scanf("%d %d",&a, &b);
 
  g =  gcd(a, b);
 
  printf("GCD of two numbers %d %d is %d",a,b,g);
 
  return 0;
}



Output :


Enter two numbers  :   15    25

GCD of two numbers is  :  5


Program to Find Largest of Three Numbers - C, C++ Code


  • Write a program to find largest of three numbers.
  • C program to find largest of three numbers.
  • C++ program to find largest of three numbers.

In this program, We'll take three input numbers and print largest among three numbers. To solve this problem, first let's write an algorithm to find largest number among three numbers.

Program to Find Largest of Three Numbers


Algorithm to Find Largest of Three Numbers



 1. Declare three variable a ,b, c.

 2. Compare a with b and c. If a is greater than b and c than a is greatest among three numbers.

 3. Compare b with a and c. if b is greater than a and c than b is greatest among three numbers.

 4. Compare c with a and b. If c is greater than a and b than c is greatest among three numbers.

Program to find second largest element in an array

Find largest element in an array


C Program to Find Largest of  Three Numbers


We have discussed the algorithm to find largest among three numbers. Let's write a c code to find largest among three input numbers.

#include <stdio.h>
 
int main() {
 
      int a, b, c;
 
      printf("Enter three numbers: ");
      scanf("%d %d %d", &a, &b, &c);
 
      //If a is greater than b and c

      if(a >= b && a >= c){
         printf("Largest number is %d", a);
 
      //if b is greater than a and c

      }else if(b >= a && b >= c){
         printf("Largest number is %d", b);
 
      //If c is greater than a and b

      }else if(c >= a && c >= b){
         printf("Largest number is %d", c);
 
      } 
      return 0;
}

Output -

Enter three numbers :

14
34
23

Largest number is : 34


C Program Find Largest of three Numbers using Ternary Operator


In this programming example, We are going to use conditional operator to print largest of three numbers.
#include <stdio.h>

int main(void) {
 
    int a, b, c, larg;
 
    printf("Enter three numbers \n");
    scanf("%d %d %d", &a , &b , &c);
 
 
    larg =  (a > b && a > c) ? a : b > c ? b : c ;
 
    printf("Largest number is %d ", larg);

 
    return 0;
}


C++ Program Find Largest of three Numbers


We have written a code, Let's write a C++ code to print larges among three numbers.
#include <iostream>
using namespace std;

int main() {
 
    int a, b, c;
 
    cout << "Enter three numbers \n";

    /* Taking input */
    cin >> a >> b >> c;
 
    /* If a is greater than b and c. */

    if (a > b && a > c) {
       cout << "Largest number is " << a;

    /* If b is greater than a and c */
    } else if (b > a && b > c) {
       cout << "Largest number is " << b;

    } else {
       cout << "Largest number is "<< c;

    }
 
     return 0;
}




Programming question on Arrays

Programming question on Linked List

Sorting algorithm and their time complexity

Stack implementation

Programming questions using Recursion

C, C++ interview questions

Programming Books

C Program to Print Even Numbers between 1 to 100 using For and While Loop


  • Write a C program to print even numbers between 1 to 100 using for and while loop.  
  • Write a C program to print even numbers between 1 to N. 


In this tutorial, we are going to write a c program which prints even numbers between 1 to 100. In another example, we write a c code which prints even numbers between 1 to N (N is an input number by a user).

C Program to Print Even Numbers between 1 to 100 using For and While Loop


What is Even Numbers?

An even numbers are those numbers which is exactly divided by 2.

For example - 2, 4 , 6 etc. are even numbers.

C++ program to print even numbers between 1 to 100

Program to print odd and even numbers of an array

Program to check whether number is even or odd

C interview questions with answers

C Program to Print Even Numbers from 1 to N


#include <stdio.h>

int main(void) {
 
   int n;
 
   printf ("Enter a number \n");
   scanf ("%d", &n);
 
   printf ("Even numbers from 1 to %d is : ",n);
 
   for (int i = 1; i <= n; i++) {
  
      if ( i % 2 == 0) {
   
          printf (" %d ", i);
      }
  
  }
 
 return 0;
}


C Program to Print Even Numbers between 1 to 100 using While Loop


#include <stdio.h>

int main() {
 
     int i=1;

     /* Run a loop from 1 to 100 */
     while( i <= 100) {
  
      /* If number is divisible by 2
         then it's an even number
      */
  
      if(i % 2 == 0){
   
        printf(" %d ", i);
      }
  
        /* Increment i. */
  
        i++;
     }
    
    return 0;
}

Output -

2  4  6  8  10  12  14  16  18  20  22  24  26  28  30  32  34  36  38  40  
42  44  46  48  50  52  54  56  58  60  62  64  66  68  70  72  74  76  78  
80  82  84  86  88  90  92  94  96  98  100 

C Program to Print Even Numbers between 1 to 100 using For Loop


#include <stdio.h>

int main() {
 
     /* Run a loop from 1 to 100 */
    for(int i = 1; i <= 100; i++) {
  
      /* If number is divisible by 2
         then it's an even number
      */
        if(i % 2 == 0){
   
           printf(" %d ", i);
        }
    }
    
    return 0;
}

C++ Program to Print Even Numbers between 1 to 100 using For & While Loop


  • Write a C++ program to print even numbers between 1 to 100. 
  • Write a C++ program to print even numbers between 1 to N.


In this tutorial, we are going to write a c++ code which print even numbers between 1 to 100 using for and while loop.

C program to print even numbers between 1 to 100

C++ Program to Print Even Numbers between 1 to 100

C++ Program to Print Even Numbers between 1 to 100 using For Loop


#include <iostream>
using namespace std;

int main() {
 
    int i;
 
    /* Run a loop from 1 to 100. */
 
    for(i = 1; i < = 100; i++){
  
       /* If number is divisible by 2. */
  
      if(i % 2 == 0) {

         cout << i <<" "; 
      }
   }
    return 0;
}


Output : 


2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 
42 44 46 48 50 52 54 56 58 60 62 64 66 68 70
72 74 76 78 80 82 84 86 88 90 92 94 96 98 100

C++ Program to Print Even numbers between 1 to 100 using While Loop


#include <iostream>
using namespace std;

int main() {
 
    /* Initialize i with 1. */
 
    int i=1;
 
   /* If i is less than or equal to 100. */
 
   while( i <= 100){
  
      /* If number is divisible by 2, then print.*/
  
      if(i % 2 == 0){
   
         cout <<i<< " ";
      }
  
     /* Increment i. */
  
     i++;
 }
 
 return 0;
}


C++ Program to Print Even numbers between 1 to N


In above examples, we have written a code to print even numbers between 1 to 100 using for and while loop. In this example, instead of 100 we take a input value from user and print even numbers between 1 to n (where n is input by user).

#include <iostream>
using namespace std;

int main() {
 
    /* Initialize i with 1. */
 
    int i=1, n;

    cout << "Enter a number \n";
    cin >> n;
 
   /* If i is less than or equal to n. */
 
   while( i <= n){
  
      /* If number is divisible by 2
         then it's an even number
      */
  
      if(i % 2 == 0){
   
         cout <<i<< " ";
      }
  
     /* Increment i. */
  
     i++;
 }
 
 return 0;
}


C interview questions with answers

Programming question on strings

Sorting algorithm

C Program to Calculate Power of a Number

Write a c program to calculate power of a number without using inbuilt pow() function.

In this tutorial, we are going to write c program which takes two numbers (base and exponent) as a input from a user and calculate it's result (baseexponent).

For example -

Input number (base) : 2

Input power (exponent) : 3

Result - 23 = 8 (2 * 2 * 2).

To solve this problem take a input number and it's power from a user.  Then run a loop until the count is less than or equal to the enter value of a power and calculate it's result.

C Program to Calculate Power of a Number

C Program to Calculate Power of a Number


#include <stdio.h>

int main(void) {
 
   int num, pow, result = 1, count = 1;
 
   printf("Enter a number \n");
   scanf("%d", &num);
 
   printf("Enter the power of a number\n");
   scanf("%d", &pow);
 
   // If a count is less than pow
 
   while(count <= pow) {
     //Multiply the number and assign to result
     result = result * num;
     count++;
   }
 
   printf("Result = %d", result);
 
   return 0;
}

Output -

Enter a number     2

Enter the power of a number    3

Result = 8

C interview questions with answers

C Program to Calculate Power of a Number using For Loop


#include <stdio.h>

int main(void) {
 
   int num, pow, result = 1;
 
   printf("Enter a number \n");
   scanf("%d", &num);
 
   printf("Enter the power of a number\n");
   scanf("%d", &pow);
 
   /* Using for loop,
      Initialize i to 1
      Termination condition (i is less than equal to pow)
    */
   for(int i = 1; i <= pow; i++) {
      //Multiply the number and assign to result
      result = result * num;
   }
 
   printf("Result = %d", result);
 
   return 0;
}

C program to print factorial of a number

C program to count number of vowels in a string

C program to print fibonacci series

C Program to Print Fibonacci Series using While, Do While & For Loop

Write a C program to print fibonacci series. In this tutorial, We'll write a c program to print fibonacci series using while, do while and for loop.

C Program to Print Fibonacci Series using While, Do While & For Loop

What is Fibonacci series?


In Fibonacci series, The first two numbers are 0 and 1, and each subsequent numbers are the sum of previous two numbers. For example- 0  1  1  2  3  5  8  13 ............


So first two numbers are 0 and 1, next number 1 is the sum of 0 + 1. Similarly, 2 is the sum of 1 + 1 and so on.


Interview Questions For Practice

C Program to Print Fibonacci Series using Recursion


C Program to Print Fibonacci Series using For Loop


#include <stdio.h>

int main()
{
    int i, n, first=0, second=1, third;

    printf("How many elements you want to print in a series \n");
    scanf("%d",&n);
   
   /* First and second number of a Fibonacci series
      It is always 0 and 1
    */
    printf("Fibonacci Series is \n");
    printf(" %d  %d ",first ,second);


    for(i = 2; i < n; i++){
  
     /* Each subsequent number is the sum  
        of previous two numbers
     */

      third = first + second;

      printf(" %d ",third);

      first = second;
      second = third;
  }

 return 0;
}


Output :

How many elements you want to print in a series  6

Fibonacci Series is

0   1   1   2   3   5

C Program to print factorial of a number

C Program to reverse a number

C Program to Print Fibonacci Series using While Loop

We have written a code to print Fibonacci series using for loop. In this example, We are going to write a C program to print Fibonacci series using while loop.

#include <stdio.h>

int main()
{
    int n, first=0, second=1, third;

    printf("How many elements you want to print in a series \n");
    scanf("%d",&n);
   
   /* First and second number of a Fibonacci series
      It is always 0 and 1
    */
    printf("Fibonacci Series is \n");
    printf(" %d  %d ",first ,second);

    /*
     First two elements of a fibonacci series is printed
     start printing number from index 2
     */
     
    int i = 2;
    
    //While loop
    while (i < n){
  
     /* Each subsequent number is the sum  
        of previous two numbers
     */

      third = first + second;

      printf(" %d ",third);

      first = second;
      second = third;
      
      //Increment the value of i
      
      i++;
  }

  return 0;
}

C Program to Print Fibonacci Series using Do While Loop


In this example, we write to code to print Fibonacci series using do while loop.

#include <stdio.h>

int main()
{
    int n, first=0, second=1, third;

    printf("How many elements you want to print in a series \n");
    scanf("%d",&n);
   
   /* First and second number of a Fibonacci series
      It is always 0 and 1
    */
    printf("Fibonacci Series is \n");
    printf(" %d  %d ",first ,second);

    /*
     First two elements of a fibonacci series is printed
     start printing number from index 2
     */
     
    int i = 2;
    
    //Do while loop logic

    do {
    
    /* Each subsequent number is the sum  
        of previous two numbers
     */
      third = first + second;

      printf(" %d ",third);

      first = second;
      second = third;
      
      //Increment the value of i
      i++;
       
    } while (i < n);

 return 0;
}

In this tutorial, I have explained how to print Fibonacci series using multiple code examples. If you have any doubt you can ask them through your comments.

C++ Program to Print Factorial of a Number

Write a C++ Program to print factorial of a number. How to print factorial of a number using C++ code.

Given an input number, we have to write a code to calculate a factorial of a number.

C++ Program to Print Factorial of a Number


What is Factorial?


Factorial of a non-negative number n, is the product of all integers less than or equal to n.

For example - Factorial of 5 is 120.

5! = 5 * 4 * 3 * 2 * 1 = 120

C Program to Print Factorial of a Number

Java Program to Print Factorial of a Number


C++ Program to Print Factorial of a Number


#include<iostream.h>

int main() {

  int num, fact=1;  

  cout << "Enter a number: "; 
  cin  >> num;

  //If number is greater than zero

  if(num > 0) {

    for(int i=1; i <= num; i++){
       fact = fact*i;                  
    }

   cout << "Factorial of a number is: "<< fact;
 
  } else if (num == 0 ){

    cout << "Factorial of 0 is 1";

  } else {

   cout << " Factorial of a negative number doesn't exist";

  } 

  return 0; 

}

C++ Program to Print Factorial of a Number using Function


#include <iostream>

using namespace std;

int factorial(int num) {

     int fact = 1;
 
     //Factorial of a zero is zero
     if (num == 0) {
       return 1;
     } 
 
     for (int i = 1; i <= num; i++) {
       fact = fact*i;
     }
 
    return fact;
}

int main(void) {

    int num;
 
    cout << "Enter a number \n";
    cin  >> num;
 
    if (num >=  0) {
       //Function is called factorial(num)
       cout << "Factorial of a number is " << factorial(num);
  
    } else {
      cout << "Factorial of a negative number doesn't exist";
    }
  
    return 0;
}


C++ Program to Print Factorial of a Number Video Tutorial




Print factorial of a number using recursion


C Program to Print Factorial of a Number


Write a C  program to Print  factorial of a number. In this program, We are going to write a C program which takes an input number and print it's factorial. 

To solve this problem, we are using iterative approach. If you are not familiar with iterative and recursive approach then you can check this awesome tutorial on recursion vs iteration.

Difference between iteration and recursion

Java program to print factorial of a number

C Program to Print Factorial of a Number



What is Factorial?

For any input number n, it's factorial is

factorial = 1 * 2 * 3 * 4 .......n;

Suppose, An input number is 4 then it's factorial is 4 * 3 * 2* 1 = 24

C Program to Print Factorial of a Number using Loop


In this program, we take a input number and using for loop our program calculate and print the factorial of a number.

#include<stdio.h>

int main() 
{

   int number, fact=1, i;  

   printf("Enter a number \n "); 
   scanf("%d",&number);
 
   //If a number is greater than zero
   if(number > 0) {

      for(i = 1; i <= number; i++){
         fact = fact*i;                  
      }

      printf("The factorial of a number is: %d",fact);
 
    } else if (number == 0 ){

        printf("Factorial of 0 is 1");

    } else {

       printf("Factorial of negative number doesn't exist";
 
   }

   return 0;
 }

C Program to Print Factorial of a Number using Function

#include <stdio.h>

  //This method calculate and return factorial
  int factorial(int num) {

     int fact = 1;
 
     //Factorial of a zero is zero
      if (num == 0) {
        return 1;
     } 
 
     for (int i = 1; i <= num; i++) {
        fact = fact*i;
     }
 
     return fact;
  }

  int main(void) {

     int num;
 
     printf ("Enter a number \n");
     scanf  ("%d", &num);
 
     if (num >=  0) {
        //Function is called factorial(num)
        printf ("Factorial of a number is %d", factorial(num));
  
     } else {
        printf ("Factorial of a negative number doesn't exist");
     }
  
     return 0;
  }



Output -

Enter a number 5

Factorial of a number is 120

Print factorial of a number using recursion



C Program to Reverse a Number

Write a C program to reverse a number. In this program, A number is input by a user and our code will reverse this number. For reversing a number, we'll use an iterative approach.

C Program to Reverse a Number


Program to Reverse a number using Recursion


How to Reverse a Number in C



Let's check the logic.

1. Take a input from user.

2.
 while(num!=0) {

          temp = num%10;   // Store the result in temp variable

          rev = (rev*10)+temp;

          num = num/10; 
        
      }
 

Explanation -  


 Suppose, An input number is 243. 

 While loop condition is true, as num is 243 

 rev = 0 * 10 + 3; 
 num = 24; 

 Now num is 24 still while loop condition is true, 

 rev = 3*10 + 4; 
 num = 2; 

 Now num is 2, again while loop is executed, 

 rev = 34 * 10 + 2 
 num = 0; 

 Now number is zero while loop will break


Program to Reverse a String



C Program to Reverse a Number


#include <stdio.h>

int main(void) {
 
    int num, temp, rev=0;

    printf("Enter a number");
    scanf("%d", &num);

 
    while(num != 0){

       temp = num % 10;
       rev  = (rev*10)+temp;
       num  = num/10;

    }

    printf("Reverse of a input number is  %d", rev);


    return 0;
}



Programming question on Arrays

Programming question on Linked List

Sorting algorithm and their time complexity

Stack implementation

Programming questions using Recursion

C, C++ interview questions

Programming Books



C++ Program to Reverse a Number

Write a C++ program to reverse a number. In this tutorial, we are going to write a program which takes an input number from a user and reverse this  number.

C++ Program to Reverse a Number


C program to reverse a number

C++ Program to Reverse a Number


 #include<iostream>
  using namespace std;

  int main()
  {
     int num, temp, rev=0;

     cout << "Enter a number \n";
     cin  >> num;

 
     while(num != 0){

       temp = num%10;
       rev  = (rev*10) + temp;
       num  = num/10;

     }

     cout<<"\n Reverse of a input number is "<< rev;

     return 0;

 }




Output -

Enter a number   243

Reverse of a input number is   342


Explanation -  


 We have input a number 243. 

STEP 1 -  While loop condition is true, as num is 243. Initially the value of rev is equal to 0.

 rev = 0 * 10 + 3; 
 num = 24; 

 STEP 2 -Now num is 24 and rev is 3. In next iteration while loop condition is true, 

 rev = 3*10 + 4; 
 num = 2; 

 STEP 3- Now num is 2 and rev = 34. So again, while loop condition is true, 

 rev = 34 * 10 + 2 
 num = 0; 

 Now number is zero while loop will break the value of rev is 342.