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 sudhir sharma
Page 19 of 98
Write a function that returns 2 for input 1 and returns 1 for 2 in C programming
A function that returns 2 for input 1 and 1 for input 2 can be implemented using various approaches. This is a common programming exercise that demonstrates different logical and mathematical techniques for value swapping between two specific numbers. Syntax int functionName(int x); Method 1: Using Conditional Statement The simplest approach uses an if-else statement to check the input value and return the corresponding output − #include int reverseif(int x) { if (x == 1) return 2; ...
Read MoreWrite a C program to print " Tutorials Point " without using a semicolon
In C programming, semicolons are statement terminators that mark the end of each statement. However, it's possible to print text without using semicolons by leveraging control structures and the return value of printf(). Syntax int printf(const char *format, ...); The printf() function returns an integer representing the number of characters printed. This return value can be used in conditional expressions within control structures that don't require semicolons. Method 1: Using if Statement The if statement can evaluate the return value of printf() without requiring a semicolon − #include int ...
Read MoreWays to paint N paintings such that adjacent paintings don't have same colors in C programming
In this problem, you are given N paintings and we have M colors to paint them with. We need to find the number of ways to paint the paintings such that no two adjacent paintings have the same color. The program's output can have very large values, so we calculate the answer in standard modulo 109 + 7. Syntax long long paintWays(int n, int m); long long power(long long base, long long exp, long long mod); Mathematical Formula The formula to find the number of ways is − Ways = m ...
Read MoreSurface Area and Volume of Hexagonal Prism in C programming
The surface area of any figure is the total area that its surface covers. A hexagonal prism is a three-dimensional figure that has a hexagon at both its ends. In mathematics, a hexagonal prism is defined as a three-dimensional figure with 8 faces, 18 edges, and 12 vertices. It consists of two hexagonal bases connected by rectangular faces. a ...
Read MoreSuperperfect Number in C programming
A superperfect number is a positive integer that satisfies a specific mathematical condition involving the sum of divisors function. This concept was introduced by D. Suryanarayana in 1969 as a generalization of perfect numbers. Syntax sig(sig(n)) = 2n Where sig(n) is the divisor summatory function that calculates the sum of all positive divisors of n (including 1 and n itself). Example 1: Checking if 16 is Superperfect Let's verify that 16 is a superperfect number by calculating sig(sig(16)) and comparing it with 2×16 − sig(16) = 1 + 2 + 4 ...
Read MoreSuper Prime in c programming
A super-prime number is a prime number that occupies a prime position in the sequence of all prime numbers. For example, if we list all prime numbers as 2, 3, 5, 7, 11, 13, ..., then the super-primes are the primes at positions 2, 3, 5, 7, 11, etc. So 3 (at position 2), 5 (at position 3), and 11 (at position 5) are super-primes. Syntax void superPrimes(int n); bool sieveOfEratosthenes(int n, bool isPrime[]); Example: Finding Super-Primes Less Than 50 This program uses the Sieve of Eratosthenes to find all primes, then identifies super-primes ...
Read MoreSums of ASCII values of each word in a sentence in c programming
In C programming, calculating the sum of ASCII values of each word in a sentence involves finding the ASCII value of each character and adding them up word by word. This technique is useful for text processing and string analysis tasks. Syntax int asciiValue = (int)character; // or simply int asciiValue = character; Example Here's how to calculate the sum of ASCII values for each word in a sentence − #include #include void calculateWordSums(char str[]) { int wordSum = 0; int ...
Read MoreSum triangle from an array in C programming
The sum triangle from an array is a triangle that is made by decreasing the number of elements of the array one by one. The new array that is formed contains integers that are the sum of adjacent integers of the existing array. This procedure continues until only one element remains in the array. Let's take an example to explain the concept better − Array = [3, 5, 7, 8, 9] Output [106] [47, 59] [20, 27, 32] [8, 12, 15, 17] [3, 5, 7, 8, 9] Explanation For ...
Read MoreC Programming for sum of the series 0.6, 0.06, 0.006, 0.0006, ...to n terms
The given series 0.6, 0.06, 0.006, 0.0006, ... is a geometric progression where each term is obtained by dividing the previous term by 10. To find the sum of this series, we apply the formula for the sum of a geometric progression where the common ratio r < 1 (r = 0.1 in our case). Syntax Sum = a * (1 - r^n) / (1 - r) where a = first term, r = common ratio, n = number of terms Mathematical Formula For the given series − First term (a) = ...
Read MoreC Programming for Sum of sequence 2, 22, 222, .........
Given is a sequence: 2, 22, 222, 2222… and we need to find the sum of this sequence. We need to derive a mathematical formula to calculate the sum efficiently. Syntax sum = 2 * (pow(10, n) - 1 - (9 * n)) / 81; Mathematical Derivation The formula derivation follows these steps − sum = [2 + 22 + 222 + 2222 + ...] sum = 2 * [1 + 11 + 111 + 1111 + ...] sum = 2/9 * [9 + 99 + 999 + 9999 + ...] sum ...
Read More