Found 1401 Articles for C

Area of a square inscribed in a circle which is inscribed in an equilateral triangle in C Program?

sudhir sharma
Updated on 13-Aug-2019 10:21:35

113 Views

The program to find the area of a square inscribed in a circle which itself is inscribed in an equilateral triangle. The radius of circle inscribed inside an equilateral is a/(2√3).The diameter of circle is the diagonal of square, d = 2 * r = a/ √3 Area formula for area of square given its diagonals is ½ d2, A = 0.5 * d2 A = (1/2) * (a2) / (3) = (a2/6) Example#include using namespace std; int main() {    float area,a = 10;    area = (a*a) / 6;    cout

C vs BASH Fork bomb?

sudhir sharma
Updated on 08-Aug-2019 08:09:29

205 Views

Fork() bomb is a Dos (Denial Of Service) attack against the linux based system. This calls the Fork() system infinite number of times that fills the memory of the program and intends to harm the system.Bash script for fork bomb:(){ :|: & };:The code explained as :( ) is function definition, { } defines the body of the loop. :|:& create a memory location and does not allow it to get deallocated. This program call itself multiple number of time again and again. Thus calling infinite calls.C Fork bomb is also the same type of Dos but it can be ... Read More

C/C++ Programming to Count trailing zeroes in factorial of a number?

sudhir sharma
Updated on 08-Aug-2019 08:06:48

750 Views

Counting the number of trailing zeroes in a factorial number is done by counting the number of 2s and 5s in the factors of the number. Because 2*5 gives 10 which is a trailing 0 in the factorial of a number.ExampleFactorial of 7 = 5040, the number of trailing 0’s is 1.Based on our logic 7! = 2*3*4*5*6*7, it has 3 2s and 1 5s so the number of trailing 0’s is 1.#include using namespace std; int main() {    int n = 45;    int count = 0;    for (int i = 5; n / i >= 1; i *= 5)       count += n / i;    cout

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

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

11K+ 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

365 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

1K+ 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?

sudhir sharma
Updated on 08-Aug-2019 07:00:46

1K+ Views

In this C programming puzzle you need to merge two numbers. You cannot use any arithmetic, string or other functions.So In This C Puzzle −Input : 12 , 54 Output : 1254Optimum solution to this C programming puzzle is to use the Token-pasting operator define.Define a macros using this ## token-pasting operator gives you the merged value. This operator merges the tokens that are passed to it.PROGRAM TO SOLVE THE C PUZZLE#include #define merge(a, b) b##a int main(void) {    printf("%d ", merge(432 ,23));    return 0; }Output23432

A Boolean Array Puzzle in C?

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

146 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

2K+ 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

129 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

Advertisements