Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Articles by Sunidhi Bansal
Page 15 of 81
Print the nearest prime number formed by adding prime numbers to N
Given a number N, if it is not prime, the task is to find the nearest prime number by repeatedly adding prime numbers (starting from 2) to N until we get a prime result. Input: N=6 Output: 11 Explanation Since 6 is not prime, we add the first prime number 2 to get 6+2=8. Since 8 is also not prime, we add the next prime number 3 to get 8+3=11. Since 11 is prime, the output is 11. Syntax int isPrime(int n); int findNearestPrime(int n); Algorithm Check if ...
Read MorePrint Reverse a linked list using Stack
Given a linked list, the program must print the list starting from the end till the front using the stack data structure. This approach leverages the LIFO (Last In First Out) property of stacks to reverse the order of elements. Input : 10 -> 5 -> 3 -> 1 -> 7 -> 9 Output: 9 -> 7 -> 1 -> 3 -> 5 -> 10 Syntax struct Node { int data; struct Node* next; }; // Stack operations void push_to_stack(int stack[], int* top, int value); ...
Read MorePrint Postorder traversal from given Inorder and Preorder traversals
Given inorder and preorder traversals of a binary tree, we can construct the postorder traversal without actually building the tree. This approach uses the property that preorder gives us the root first, inorder helps us divide left and right subtrees, and postorder processes left subtree, then right subtree, and finally the root. Syntax void postorder(int pre_order[], int in_order[], int n); int find_value(int element, int in_order[], int n); Algorithm 1. Find the root element (first element of preorder) in inorder array 2. Recursively process left subtree (elements before root in inorder) 3. Recursively process ...
Read MorePrint numbers having first and last bits as the only set bits
The task is to print numbers that have exactly two set bits: one at the first (rightmost) position and one at the last (leftmost) position. These numbers follow the pattern where only the most significant bit and the least significant bit are set to 1. Set bits are binary digits with value 1, while unset bits have value 0. Numbers with first and last bits set follow the pattern: 1, 3 (11), 5 (101), 9 (1001), 17 (10001), etc. Syntax // Check if number has only first and last bits set if (!(n-1 & n-2)) ...
Read MorePrint prime numbers with prime sum of digits in an array
Given an array of elements, the task is to print those numbers that are prime and whose digit sum is also prime. This problem combines prime number checking with digit sum calculation. Input: arr[]={2, 4, 3, 19, 25, 6, 11, 12, 18, 7} Output: 2, 3, 25, 11, 12, 7 The output contains numbers where both the number itself and its digit sum are prime. For example − 2, 3, 7 are prime numbers, while 25(2+5=7), 11(1+1=2), 12(1+2=3) have prime digit sums. Numbers like 19(1+9=10) are excluded because 10 is not prime. Syntax ...
Read MorePrint an array with numbers having 1, 2 and 3 as a digit in ascending order
In this problem, we need to find and print all numbers from an array that contain the digits 1, 2, and 3. The numbers should be printed in ascending order. If no such numbers exist, we return -1. Syntax // Check if number contains digits 1, 2, and 3 int containsAllDigits(int num); // Sort and filter array elements void printNumbersWithDigits(int arr[], int n); Algorithm The approach involves the following steps − Step 1: Sort the array in ascending order Step 2: For each number, convert it to string format Step 3: ...
Read MorePrint matrix in antispiral form
In C programming, printing a matrix in antispiral form means traversing the matrix in a spiral order (clockwise from outside to inside) and then displaying the elements in reverse order. This creates an "antispiral" pattern where we start from the center and work outward. Syntax // Using array to store spiral elements int spiral[n*n]; // Traverse in spiral order: right → down → left → up // Print in reverse order Algorithm The approach involves two main steps − Spiral Traversal: Traverse the matrix in spiral order (right → down → left ...
Read MorePrint numbers in descending order along with their frequencies
Given an array of integer elements, the task is to arrange the elements in descending order and find their frequencies. This approach works when the input array is already sorted in ascending order. Input : arr[]={1, 1, 1, 2, 2, 2, 3, 3, 4, 5, 6, 7, 7} Output : 7 occurs: 2 6 occurs: 1 5 occurs: 1 4 occurs: 1 ...
Read MorePrint prime numbers from 1 to N in reverse order
In this article, we will learn how to print prime numbers from 1 to N in reverse order. A prime number is a number greater than 1 that has no positive divisors other than 1 and itself. Our goal is to find all prime numbers in the given range from 1 to N and then print them in reverse order. For example, given N = 20, the prime numbers between 1 and 20 are: 2, 3, 5, 7, 11, 13, 17, 19. The output should be the prime numbers printed in reverse order: 19, 17, 13, 11, 7, 5, ...
Read MorePrint first N terms of series (0.25, 0.5, 0.75, ...) in fraction representation
In C programming, printing a series like (0.25, 0.5, 0.75, ...) in fraction representation involves converting decimal values to their equivalent fractions. This series represents multiples of 0.25, which can be expressed as fractions with denominator 4. Syntax for (i = 0; i < n; i++) { // Calculate numerator and denominator // Print in fraction form: numerator/denominator } Algorithm START Step 1 → Declare variables: int num, den, i, n Step 2 → Input number in n Step 3 → Loop from i = ...
Read More