Found 1339 Articles for C

Sum of squares of first n natural numbers in C Program?

sudhir sharma
Updated on 08-Aug-2019 08:01:53

14K+ Views

The sum of squares of the first n natural numbers is found by adding up all the squares.Input - 5Output - 55Explanation - 12 + 22 + 32 + 42 + 52There are two methods to find the Sum of squares of first n natural numbers −Using Loops − the code loops through the digits until n and find their square, then add this to a sum variable that outputs the sum.Example#include using namespace std; int main() {    int n = 5;    int sum = 0;    for (int i = 1; i >= n; i++)       sum += (i * i);    cout

Write you own Power without using multiplication(*) and division(/) operators in C Program

sudhir sharma
Updated on 08-Aug-2019 07:44:49

480 Views

Power function is calculated using the times multiple i.e. 5n is 5*5*5… n times. For this function, to work properly without using multiplication(*) and division(/) operators we will use nested loops that add the numbers n number of time.Example#include using namespace std; int main() {    int a= 4 , b = 2;    if (b == 0)       cout

Sum of squares of the first n even numbers in C Program

sudhir sharma
Updated on 08-Aug-2019 07:41:42

2K+ Views

The sum of squares of the first n even numbers means that, we first find the square and add all them to give the sum.There are two methods to find the sum of squares of the first n even numberUsing LoopsWe can use loops to iterate from 1 to n increase number by 1 each time find the square and add it to the sum variable −Example#include using namespace std; int main() {    int sum = 0, n =12;    for (int i = 1; i

A C Puzzle in C Programming?

Akansha Kumari
Updated on 18-Jul-2025 16:03:43

1K+ Views

C programming puzzles are small but tricky coding problems, which are designed to challenge and to understand the C programming language in a better way. Problem Statement We are given two numbers, and our task is to combine them in such a way that the second integer is placed before the first integer, resulting in a single combined integer. But here, we are not allowed to use any logical, arithmetic, string-related operations, or any pre-defined functions. Consider the following input/output scenarios to understand the puzzle statement better: Scenario 1 Input: 12, 54 Output: 5412 Explanation: Here, the second ... Read More

A Boolean Array Puzzle in C?

sudhir sharma
Updated on 08-Aug-2019 06:24:27

251 Views

This an array based puzzle that need you to change all the numbers of an array the contains two elements to 0. One element of the array is 0 and other may or may not be 0.To solve this puzzle the program needs to find the non-zero element and change in to 0.Their are the following constraints that are needed to solve the boolean array puzzle −Allowed operation is complement, other operations are not allowed.Loops and conditional statements are not allowed.Direct assignment is also not allowed.PROGRAM TO SOLVE BOOLEAN ARRAY PUZZLE#include using namespace std; void makeZero(int a[2]) {    a[ ... Read More

Abundant Number in C ?

sudhir sharma
Updated on 07-Aug-2019 14:44:39

3K+ Views

An abundant Number (also known as excessive number) is a number in the number theory which itself is smaller than the sum of all its proper divisors. For example, 12 is an abundant Number : divisors 1, 2, 3, 4, 6 , sum =16 >12.The difference between the sum of divisors and the number is called abundance. For above example abundance = 4 => 16 - 12 .To check for abundant number we will find all the factors of the number and add them up. This sum compared with the number show that if the number is abundant or not.PROGRAM ... Read More

A shorthand array notation in C for repeated values?

sudhir sharma
Updated on 07-Aug-2019 14:38:32

175 Views

An array stores number of same data type. For an array there may arise a situation when you need to store 2-3 values that are same i.e. 3,3,3,3 needs to be stored.For this case, the programing language C has made a simple way to create an array with such repeated value to reduce the workload of the programmer.Syntax[startofRepeatingSeq … EndofRepeatingSeq]number Example : For 3 repeated 5 times ; [0 … 4]3Example#include int main() {    int array[10] = {[0 ... 4]3, [6 ... 9]5};    for (int i = 0; i < 10; i++)       printf("%d ", array[i]);    return 0; }Output3 3 3 3 3 0 5 5 5 5

A modified game of Nim in C ?

sudhir sharma
Updated on 07-Aug-2019 14:32:16

671 Views

Modified game of Nim is an optimisation games of arrays. This game predicts the winner based on the starting player and optimal moves.Game Logic − In this game, we are given an array{}, that contains elements. There are generally two players that play the game namly player1 and player2. The aim of both is to make sure that all their numbers are removed from the array. Now, player1 has to remove all the numbers that are divisible by 3 and the player2 has to remove all the numbers that are divisible by 5. The aim is to make sure that ... Read More

A program to check if a binary tree is BST or not in C ?

sudhir sharma
Updated on 07-Aug-2019 14:32:47

387 Views

A binary tree is a tree data structure in which there are two child nodes for each node. The child nodes being two are referred as, left and right child.A BST is a tree structure in which left subtree contains nodes with values lesser than root and right subtree contains nodes with values greater that root.Here, we will check if a binary tree is a BST or not −To check for this we have to check for the BST condition on the binary tree. For a root node check for left child should be less that root, right child should ... Read More

0-1 Knapsack Problem in C?

sudhir sharma
Updated on 07-Aug-2019 14:24:15

26K+ Views

A knapsack is a bag. And the knapsack problem deals with the putting items to the bag based on the value of the items. It aim is to maximise the value inside the bag. In 0-1 Knapsack you can either put the item or discard it, there is no concept of putting some part of item in the knapsack.Sample ProblemValue of items = {20, 25, 40} Weights of items = {25, 20, 30} Capacity of the bag = 50Weight distribution25, 20{1, 2} 20, 30 {2, 3} If we use {1, 3} the weight will be above the max allowed value. ... Read More

Advertisements