Found 1401 Articles for C

C Program for Rat in a Maze - Backtracking-2?

sudhir sharma
Updated on 20-Aug-2019 10:25:14

3K+ Views

Rat in a maze is also one popular problem that utilizes backtracking. IA maze is a 2D matrix in which some cells are blocked. One of the cells is the source cell, from where we have to start. And another one of them is the destination, where we have to reach. We have to find a path from the source to the destination without moving into any of the blocked cell. A picture of an unsolved maze is shown below.And this is its solution.To solve this puzzle, we first start with the source cell and move in a direction where ... Read More

C Program to Check if count of divisors is even or odd?

sudhir sharma
Updated on 20-Aug-2019 08:34:59

264 Views

Given a number “n” as an input, this program is to find the total number of divisors of n is even or odd. An even number is an integer that is exactly divisible by 2. Example: 0, 8, -24An odd number is an integer that is not exactly divisible by 2. Example: 1, 7, -11, 15Input: 10 Output: EvenExplanationFind all the divisors of the n and then check if the total number of divisors are even or odd. To do this find all divisor and count the number and then divide this number by 2 to check if it is ... Read More

C Program for Program for array rotation?

sudhir sharma
Updated on 20-Aug-2019 08:33:41

3K+ Views

Write a C program to left rotate an array by n position. How to rotate left rotate an array n times in C programming. Logic to rotate an array to left by n position in C program.Input: arr[]=1 2 3 4 5 6 7 8 9 10 N=3 Output: 4 5 6 7 8 9 10 1 2 3ExplanationRead elements in an array say arr.Read number of times to rotate in some variable say N.Left Rotate the given array by 1 for N times. In real left rotation is shifting of array elements to one position left and copying first ... Read More

C Program for Pancake sorting?

sudhir sharma
Updated on 20-Aug-2019 08:31:34

182 Views

This C Program Implements Pancake Sort on Array of Integers.Pancake sorting is a variation of the sorting problem in which the only allowed operation is to reverse the elements of some prefix of the sequence.Pancake sorting is the colloquial term for the mathematical problem of sorting a disordered stack of pancakes in order of size when a spatula can be inserted at any point in the stack and used to flip all pancakes above it. A pancake number is the minimum number of flips required for a given number of pancakesInput:5, 3, 2, 1, 4 Output:1 2 3 4 5ExplanationIt ... Read More

Alternate vowel and consonant string in C/C++?

sudhir sharma
Updated on 20-Aug-2019 08:29:16

285 Views

There is a string given, rearrange characters in the string such that the vowels and consonants occupy the alternate position. If string can’t be rearranged in the above manner, print “not possible”.The order of vowels with respect to each other and the order of consonants with respect to each other should be maintained.Input: abce Output: abecExplanationFind the number of vowels and consonants in the string.If the difference between no. of vowels and consonants are more than one, return “Not Possible”.If there is a condition that more vowels are present in the string than consonants, print the first vowel first and ... Read More

C/C++ Program for Finding the Number Occurring Odd Number of Times?

sudhir sharma
Updated on 20-Aug-2019 08:26:21

314 Views

A C++ program to find a number which occurs odd number of times in a given array of positive integers. In this array, all numbers occur even number of times.Input: arr[] = {5, 7, 8, 8, 5, 8, 8, 7, 7} Output: 7ExplanationUse two loops in which the outer loop traversed all elements one by one and inner loop counts the number of occurrences of the element traversed by the outer loop.Example#include using namespace std; int Odd(int arr[], int n){    for (int i = 0; i < n; i++) {       int ctr = 0;   ... Read More

C/C++ Program to Find the sum of Series with the n-th term as n^2 – (n-1)^2

sudhir sharma
Updated on 20-Aug-2019 08:24:03

220 Views

There are many types of series in mathematics which can be solved easily in C programming. This program is to find the sum of following of series in C program.Tn = n2 - (n-1)2Find the sum of all of the terms of series as Sn mod (109 + 7) and, Sn = T1 + T2 + T3 + T4 + ...... + TnInput: 229137999 Output: 218194447ExplanationTn can be expressed as 2n-1 to get itAs we know ,=> Tn = n2 - (n-1)2 =>Tn = n2 - (1 + n2 - 2n) =>Tn = n2 - 1 - n2 + 2n ... Read More

C/C++ Tokens?

sudhir sharma
Updated on 20-Aug-2019 08:13:18

8K+ Views

C++ Tokens are the smallest individual units of a program.C++ is the superset of C and so most constructs of C are legal in C++ with their meaning and usage unchanged. So tokens, expressions, and data types are similar to that of C.Following are the C++ tokens : (most of c++ tokens are basically similar to the C tokens)KeywordsIdentifiersConstantsVariablesOperatorsKeywordsKeywords are reserved words which have fixed meaning, and its meaning cannot be changed. The meaning and working of these keywords are already known to the compiler. C++ has more numbers of keyword than C, and those extra ones have special working ... Read More

Add n binary strings?

sudhir sharma
Updated on 20-Aug-2019 08:08:41

232 Views

In this program we have to add binary numbers given. there are n binary numbers and we have to add them all to give one binary number as an output.For this we will use binary addition logic and add all the terms from 1 to N one by one to gain the result.Input: "1011", "10", "1001" Output: 10110ExplanationThe easier method is by converting binary string to its decimal equivalent, then add them and convert into binary again. Here we will do the addition manually. We will use one helper function to add two binary strings. That function will be used ... Read More

Array Manipulation and Sum using C/C++

Arnab Chakraborty
Updated on 20-Aug-2019 07:45:13

2K+ Views

Here we will see one problem, suppose one array is given. There are n elements. Another value S is also given. We have to find an element K in the array, such that, if all elements which are greater than K, are made equal to K, then the sum of all elements of the final array becomes equal to S. If that is not possible, then return -1.Suppose the elements are {12, 6, 3, 7, 8}, and sum value is 15, the output is 3. The final array is {3, 3, 3, 3, 3}, Sum of array elements is S ... Read More

Advertisements