sudhir sharma

sudhir sharma

975 Articles Published

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

sudhir sharma
sudhir sharma
Updated on 15-Mar-2026 450 Views

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 More

Write a C program to print " Tutorials Point " without using a semicolon

sudhir sharma
sudhir sharma
Updated on 15-Mar-2026 330 Views

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 More

Ways to paint N paintings such that adjacent paintings don't have same colors in C programming

sudhir sharma
sudhir sharma
Updated on 15-Mar-2026 737 Views

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 More

Surface Area and Volume of Hexagonal Prism in C programming

sudhir sharma
sudhir sharma
Updated on 15-Mar-2026 540 Views

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 More

Superperfect Number in C programming

sudhir sharma
sudhir sharma
Updated on 15-Mar-2026 609 Views

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 More

Super Prime in c programming

sudhir sharma
sudhir sharma
Updated on 15-Mar-2026 2K+ Views

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 More

Sums of ASCII values of each word in a sentence in c programming

sudhir sharma
sudhir sharma
Updated on 15-Mar-2026 493 Views

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 More

Sum triangle from an array in C programming

sudhir sharma
sudhir sharma
Updated on 15-Mar-2026 794 Views

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 More

C Programming for sum of the series 0.6, 0.06, 0.006, 0.0006, ...to n terms

sudhir sharma
sudhir sharma
Updated on 15-Mar-2026 317 Views

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 More

C Programming for Sum of sequence 2, 22, 222, .........

sudhir sharma
sudhir sharma
Updated on 15-Mar-2026 1K+ Views

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
Showing 181–190 of 975 articles
« Prev 1 17 18 19 20 21 98 Next »
Advertisements