Posts

C Program to Implement a Stack Using Array

Image
Write a C program to implement a stack using array. In this tutorial, You are going to learn the implement of stack data structure using an array.

What is a Stack Data Structure?

A Stack is a data structure in which insertion and deletion can be done from one end only. That's why it is also called LIFO(Last In First Out).

In stack, Insertion and deletion operation is known as push (Insertion) and pop (Deletion). While inserting and deleting an element in a stack we need to check some conditions.

For insertion, we need to check whether a memory is available, if memory is not available then it is known as stack overflow. Similarly, while deleting (pop operation) an element, if no element is present in a stack, then it is known as stack underflow.





Reverse a String using Stack

C Program to Implement a Stack using Array
Let's write a c code to implement stack push and pop operation using an array.


#include <stdio.h> #define MAX 50 int top=-1; int arr[MAX]; void push(int ite…

Write a Program to Reverse a String Using Stack

Write a C, C++ program to reverse a string using Stack data structure. In this question, A string is input by a user and our program will reverse a string using a Stack.

Stack Data Structure
Stack is an abstract data type in which push (insertion) and pop (deletion) operations can be done at one end.  Stack is a LIFO (Last in First Out) data structure, which means element which is inserted at last must be the first one to be removed.

Some important articles regarding Stack data structure and it's implementation.


Stack program in C using an Array

Implement Stack data structure

Stack implementation using Linked List

MCQ on stack and queue for practice

Reverse a string using for and while loop


Program to Reverse a String Using Stack Data Structure
#include <stdio.h> #include <string.h> #define max 100 int top,stack[max]; void push(char x){ // Push(Inserting Element in stack) operation if(top==max-1){ printf("stack overflow"); } else…

C Program to Calculate the Sum of First N Natural Numbers

Image
Write a C program to calculate the sum of first n natural numbers. Given an input n, We have to write a code to print the sum of first n natural number (1 to n).

There are multiple approaches to solve this problem.

* We can solve this problem using loop.
* Other approach is to use mathematical formula to calculate the sum of first n natural number.
* We can also use recursion to find the sum of first n natural numbers using recursion.

Suppose if user has entered a number 10. So, we have to calculate the sum of first 10 numbers ( 1 to 10). The Sum of first 10 numbers is 55.

Program to Calculate Sum of Digits of a Number

How to Calculate the Sum of first N Natural Numbers
1. First approach is to use for loop to calculate the sum of first n natural numbers.

for(i=1; i<=num; i++){ sum +=i; }

Program to Count Number of Words in a Sentence by Taking Input from User

2. Another approach is to use mathematical formula n(n+1)/2 to calculate the sum of first n natural numbers.

Supp…

Java Program to Reverse an Array using Recursion

Image
Write a java program to reverse an array using recursion. Given an input array, we have to write a java code to reverse an array using recursion. In this tutorial, We are going to solve this problem using recursion.

Recursion is an important programming concept, if you are not familiar with recursion concept then you can check this tutorial on recursion.

Difference between recursion and iteration

Recursion objective questions for practice

Java program to reverse an array using iteration





Java Program to Reverse an Array using Recursion
Let's write a java code to reverse an array using recursion.


import java.util.Scanner; public class ReverseArrayRecursion { public static void reverse(int arr[], int start, int end) { int temp; //If start index is greater than end index if(start >= end) return; //Logic to swap values temp = arr[start]; arr[start] = arr[end]; arr[end] = temp; //Recursively call reverse(arr, start+1,…

Java Program to Check Perfect Number

Image
Write a java program to check whether a number is perfect or not. Given an input number, we have to write a java code which check whether an input number is perfect number or not.

Before solving this problem, let's first understand what is a perfect number.

What is Perfect Number?
A perfect number is a positive integer that is equal to the sum of it's positive divisors  excluding the number itself.

For example -

6  is a perfect number. It's positive divisor is 1, 2 and 3 and sum of it's positive divisor is 6 (1 + 2 + 3).

Similarly, 28 is a perfect number. Sum of the positive divisor is 28 (1 + 2 + 4 + 7 + 14).

10 is not a perfect number. Sum of the positive divisor of 10 is 8 ( 1 + 2 + 5).




Java Program to Convert Binary to Decimal Number

Java program to find second smallest number in an array

Java Programming Questions with Answers




Java Program to Check Perfect Number or Not
We have discussed what is a perfect number? Let's write a java code which takes an input numbe…

C++ Program to Insert an Element in an Array

Image
Write a c++ program to insert an element in an array. In this program, we are going to write a c++ code which takes an input array, position and value of an element and insert it an array.


C program to insert an element in an array





C++ Program to Insert an Element in an Array
#include <iostream> using namespace std; int main() { int arr[100], size, pos, val, i, temp; cout << "Enter the size of an array (MAX 100) \n"; cin >> size; // Create an input array cout << "Enter an elements in an array \n"; //Input the value of an array for(i = 0; i < size; i++) { cin >> arr[i]; } // Take a position of a new element cout << "Enter a position\n"; cin >> pos; //Input value of an element to be inserted cout << "Enter a value to insert\n"; cin >> val; //Shift element by one position for(i = size; i >= pos; i--) { arr[i] = a…

C Program to Insert an Element in an Array

Image
Write a c program to insert an element in an array at specified position.  This question can also be asked as, Write a c program to insert an element at a given position.

In this program, we are going to write a code which takes an input array, position and value of a new element from a user and insert it at a specified position.

For example -

Input array - {4, 3, 1, 5}

Input position - 3

New element - 7

After inserting an element - {4, 3, 7, 1, 5}




C Program to delete an element from an array

Programming questions on Arrays

Program to print smallest element of an array


Logic of a Inserting an Element in an Array

i) Take an array, position and new element as an input from a user.

ii) Move an existing element one position up until  the position is reached where a new element is need to be inserted.



C Program to Insert an Element in an Array
Let's write a c code to insert an element in an array. First, we take an input array, position and new element from a user.


#include <stdio.h&g…