Adsense 728 * 15

Search

Custom Search

Java Program to Reverse a String using Stack

Write a java program to reverse a string using stack. Given an input string, we have to write a java code to reverse a string using stack data structure.

Before solving this problem, let's understand what is stack?

Stack

A stack is a linear data structure in which insert and delete operations are allowed only at one end. It is also known as LIFO (Last In First Out). In stack, insert operation is called as push and delete operation is called as pop.

Important points about stack -

i) It is an ordered list of similar data type.

ii) It follow LIFO principle (Last in First out).

iii) In stack, push() function is used to insert new elements into the Stack and pop() is used to delete an element from the stack.

iv) Stack is said to be in Overflow state when no memory is available for new element and is said to be in Underflow when it is empty and try to pop an element from stack.

Stack program in c using array


Java Program to Reverse a String using Stack


we have understood the concept of stack, let's write a java code to reverse a string using stack.

import java.util.Scanner;
import java.util.Stack;

public class ReverseStringUsingStack {

     public static void main(String[] args) {
        
        Scanner in = new Scanner(System.in);
       
        System.out.println("Enter a string to reverse");

        //Input string
        String str = in.nextLine();
  
       //Stack declaration
       Stack<Character> stack = new Stack<>();
  
       //Traverse and push each character in stack
       for(int i = 0; i < str.length(); i++) {
          stack.push(str.charAt(i));
       }
  
       //Pop character, which reverse a string
       while(!stack.empty()) {
         System.out.println(stack.pop());
       }

    }

}





MCQ on stack and queue

C program to reverse a string using stack


C Program to Print 1 to 100 Numbers without using Loop

Write a C program to print 1 to 100 numbers without using loop. In this tutorial, we going to learn how to write a C code to print 1 to 100 number without using loop (for, while etc).

How do we print numbers from 1 to 100 without using loop? Writing c program to print 1 to 100 numbers using loop is very simple. This problem can be solved using recursion.

MCQ on recursion for practice.


C Program to Print 1 to 100 Numbers without using Loop



Let's write a code to print 1 to 100 numbers without using loop.

#include <stdio.h>

void printNumbers(int num) {
 
  //If num is less than or equal to 100
  if(num <= 100) {
      
      //print number  
      printf("%d ", num);

      //Recursively call
      printNumbers(num+1);
   } 
}

int main(void) {
 
    int n = 1;
 
    //Call method to print number
    printNumbers(n);
 
    return 0;
}



In the above program, we created a function printNumbers(int num) and passed 1 in an argument from main method.

Another way of writing this program.

#include <stdio.h>

void printNumbers(int num) {
 
  //If num is less greater than or equal to 1
  if(num >= 1) {

    printf("%d ", num);

    /*Recursively call,
      Decrement the value by 1 
    */
    printNumbers(num-1);
  } 
}

int main(void) {
 
  //Call method to print number
  printNumbers(100);
 
  return 0;
}



C interview questions with answers

Java Program to Find HCF of Two Numbers using Recursion

Write a program to find HCF of two numbers using recursion in Java. Given two numbers, we have to write a Java code to print HCF (Highest Common Factor) of two numbers.

Before start writing a code, let's first understand what is HCF?

What is HCF?


HCF (Highest Common Factor) of two numbers a and b is the largest positive integers that divide both the numbers (a and b).

For example - Suppose the value of a is 12 and the value of b is 15.

Factors of 12 is 1, 2, 3, 4, 6, 12

Factors of 15 is 1, 3, 5, 15

Common factors of 12 and 15 is 1, 3

Highest common factor of 12 and 15 is 3.

Program to Find HCF or GCD of two numbers using Recursion in C, C++

Recursion

Java Program to Find HCF of Two Numbers using Recursion



public class HCF {

  public static int calculateHCF(int a, int b) {

     if (a == b) {
        return a;
     } else if (a == 0) {
        return b;
     } else if (b == 0) {
        return a;
     } else if (a > b) {
       //Recursively call
       return calculateHCF(a % b, b);
     } else {
       //Recursively call
       return calculateHCF(a, b % a);
     }

  }


  public static void main(String[] args) {

     Scanner in = new Scanner(System.in);

     System.out.println("Enter first number");
     int a = in.nextInt();

     System.out.println("Enter second number");
     int b = in.nextInt();

     System.out.println("HCF of two numbers is " + calculateGCD(a, b));

  }

}



Java Program to FInd HCF of Two Numbers using Recursion


Java program to count number of digits in a number

Java program to print factorial of a number

Programming questions on Recursion

Program to Find LCM of Two Numbers - C, C++ Code

Write a program to find LCM of two numbers in C, C++. Given two numbers a and b, we have to write a code to find LCM of a and b.

In this tutorial, we cover following topics.


  • What is LCM?
  • Program to find LCM of two numbers in C++
  • Program to find LCM of two numbers in C


What is LCM (Least Common Multiple)?


The LCM of two numbers is the smallest number that is divisible by both numbers.

For example -

LCM(8, 16) - Least common multiple of 8 and 16 is 16.

Multiples of 8 are :

8, 16, 24, 32 .......

Multiples of 16 are :

16, 32, 48, 64 .......

Common multiples of 8 and 16 are :

16, 32, 64 ..........

So, the least common multiple (LCM) of 8 and 16 are 16.


C, C++ Program to Find LCM of Two Numbers



Formula to calculate LCM(a, b) = (a * b) / GCD(a, b)

In my previous post, i have already discussed and explained the program to find GCD of two numbers using Recursion.

Program to Find LCM of Two Numbers in C++


#include <iostream>
using namespace std;

int gcd(int a, int b) {
 
    if(b == 0){
       return a;
    }
 
    return gcd(b , a%b);
}

int lcm(int a, int b) {
 
    return (a*b)/gcd(a, b); 
}

int main() {
 
    int a, b, result;
 
    // Input
    cout << "Enter two numbers \n";
    cin  >> a >> b;
 
    // Method call to calculate LCM
    result = lcm(a, b);
 
    cout << " LCM of " << a << " and " << b << " is " << result;
    return 0;
}

Program to Find LCM of Two Numbers in C


#include <stdio.h>

int gcd(int a, int b) {
 
    if(b == 0){
      return a;
    }
 
    return gcd(b , a%b);
}

int lcm(int a, int b) {
 
    return (a*b)/gcd(a, b); 
}

int main() {
 
    int a, b, result;
 
    printf("Enter two numbers \n");
    scanf( "%d%d", &a, &b);
 
    result = lcm(a, b);
 
    printf("LCM of %d and %d is %d", a, b, result);
 
    return 0;
}




Output :

Enter two numbers :   3,   5

LCM of 3 and 5 is  15

Find Greatest of three Numbers - C, C++ Code

Write a C, C++ program to find the greatest of three numbers. Given three input numbers, we have to write a code to find greatest of three numbers.


In this tutorial, we cover following topics.

  • Algorithm to find greatest of three numbers.
  • C Program to find greatest of three numbers.
  • C++ program to find greatest of three numbers.

Algorithm to Find Greatest of three Numbers

1. In first step, let's declare three variables to hold the values of three input numbers.

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

3. Similarly, 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.

C interview questions

Find Greatest of three Numbers in C++


We have discussed the algorithm to find greatest of three numbers. Let's write a C++ code to print the greatest number.

#include <iostream>
using namespace std;

int main() {

    int a,b,c;

    cout << "Enter three numbers";
    cin  >> a >> b >> c;

    /*check whether a is greater than b and c*/

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

       cout << "Greatest number is "<<b;
   
    } else {
 
     cout << "Greatest number is "<<c;

  }

   return 0;
 
}


Find  Greatest of three Numbers in C

#include <stdio.h>

int main(){
    
    int a, b, c;
    
    printf("Enter three numbers ");
    scanf("%d %d %d", &a, &b, &c);
    
    if (a > b && a > c) {
        printf("%d is Greatest",a);
    }
    else if (b > a && b > c) {
        printf("%d is Greatest",b);
    }
    else {
        printf("%d is Greatest",c);
    }
    
    return 0;
}


Output :

Enter three numbers :  8  2   1

8 is Greatest

Program to find second largest element in an array

Find largest element in an array

Find sum of fibonacci series in c, c++

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