Found 1401 Articles for C

Print the given pattern recursively

Sunidhi Bansal
Updated on 30-Jul-2019 22:30:26

4K+ Views

Here, as per the given problem pattern needs to be displayed using recursive approach.Recursive function is the one that calls itself n number of times. There can be ‘n’ number of recursive function in a program. The problem working with recursive function is their complexity.AlgorithmSTART Step 1 -> function int printpattern(int n)    If n>0       Printpattern(n-1)       Print *    End IF End Step 2 -> function int pattern(int n)    If n>0       pattern(n-1)    End IF    Printpattern(n)    Print End STOPExample#include int printpattern(int n) {    if(n>0) { ... Read More

Print values of ‘a’ in equation (a+b) <= n and a+b is divisible by x

Sunidhi Bansal
Updated on 30-Jul-2019 22:30:26

63 Views

Given with equation program must find the value of ‘a’ where a+b Declare start variables b=10, x=9, n=40 and flag=0, divisible Step 2 -> Loop For divisible = (b / x + 1 ) * x and divisible = 1       Print divisible-1       Set flag=1    End END STOPExample#include int main(int argc, char const *argv[]) {    int b=10, x=9, n=40, flag = 0;    int divisible;    for (divisible = (b / x + 1 ) * x ; divisible = 1) {          printf("%d ", divisible - b );          flag = 1;       }    }    return 0; }Outputif we run above program then it will generate following output8 17 26

Print n smallest elements from given array in their original order

Sunidhi Bansal
Updated on 30-Jul-2019 22:30:26

191 Views

Given with array of let’s say k elements the program must find the n smallest elements amongst them in their appearing order.Input : arr[] = {1, 2, 4, 3, 6, 7, 8}, k=3 Ouput : 1, 2, 3 Input k is 3 it means 3 shortest elements among the set needs to be displayed in original order like 1 than 2 and than 3AlgorithmSTART Step 1 -> start variables as int i, max, pos, j, k=4 and size for array size Step 2 -> Loop For i=k and i=0 and j--       If arr[j]>max         ... Read More

Print n terms of Newman-Conway Sequence

Sunidhi Bansal
Updated on 30-Jul-2019 22:30:26

290 Views

Newman-Conway Sequence is used for generating following integer sequence.1 1 2 2 3 4 4 4 5 6 7 7 8 8 8 8 9 10 11 12Formula used for generating Newman-Conway sequence for n numbers is −P(n) = P(P(n - 1)) + P(n - P(n - 1)) Where, p(1) =p(2) =1AlgorithmSTART Step 1 -> Input variable n(e.g. 20) Step 2 -> start variables as i, p[n+1], p[1]=1, p[2]=1 Step 3 -> Loop For i=3 and i End Loop For STOPExample#include int main() {    int n = 20,i;    int p[n + 1];    p[1] = 1;    p[2] = 1;    printf("Newman-Conway Sequence is :");    printf("%d %d ",p[1],p[2]);    for (i = 3; i

Print the kth common factor of two numbers

Sunidhi Bansal
Updated on 30-Jul-2019 22:30:26

252 Views

Given with two numbers x and y the output should contain their kth common factor.Input: x=9 y=18 k=1 Output : k common factor = 2 Factors of 9 : 1, 3, 9 Factors of 18 : 1, 2, 3, 6, 9, 18 Greatest Common Factor : 9AlgorithmSTART Step 1 -: take input as x and y lets say 3 and 21 and k as 1 Step 2 -: declare start variables as int i,num,count=1 Step 3 -: IF x

Print N lines of numbers such that every pair among numbers has a GCD K

Sunidhi Bansal
Updated on 30-Jul-2019 22:30:26

129 Views

GCDGCD stands for Greatest Common Divisor of two or more integers excluding 0Like, to find the greatest common divisor of 48 and 18048 = 2 × 2 × 2 × 2 × 3180 = 2 × 2 × 3 × 3 × 5Greatest common divisor = 2 × 2 × 3 = 12.In the given problem, N lines should be printed with elements have GCD as specifiedInput : N=2 GCD=2 Ouput : 2-4-6-10 14-16-18-22AlgorithmSTART Step 1 -> take input n(e.g. 2) and k(e.g. 2) as int values and i Step 2-> Loop For i to 0 and i end loop ... Read More

Print the nearest prime number formed by adding prime numbers to N

Sunidhi Bansal
Updated on 30-Jul-2019 22:30:26

271 Views

As per the question, the task is to find the nearest prime number by adding the prime number starting from 2 if the number N is not Prime.Input: N=6 Output: 11ExplanationSince 6 is not prime add first prime to 6 i.e. 2 which will result to 8 now 8 is also not prime now add next prime after 2 which is 3 which will give 8+3 = 11. Hence 11 is a prime number output will be 11.AlgorithmSTART Step 1- > declare num=15, i = num/2 Step 2 -> Loop For k=2 and k

Print Reverse a linked list using Stack

Sunidhi Bansal
Updated on 30-Jul-2019 22:30:26

1K+ Views

Given with a linked list program must print the list starting from the end till the front using the stack data structureInput : 10 -> 5 -> 3 -> 1 -> 7 -> 9 Output: 9 -> 7 -> 1 -> 3 -> 5 -> 10Here the user can use the approach of poping elements from the stack pointing top at the stack[0] location and than going till stack[n] elementAlgorithmSTART Step 1 -> create structure Linked_list    Declare int data    Declare struct linked_list *next End Step 2 -> declare int stack[30], top = -1 Step 3 -> declare struct ... Read More

Print Postorder traversal from given Inorder and Preorder traversals

Sunidhi Bansal
Updated on 30-Jul-2019 22:30:26

915 Views

Given with inorder and preorder of a tree program must find the postroder traversal and print the sameInput: Inorder traversal in[] = {4, 2, 5, 1, 3, 6} Preorder traversal pre[] = {1, 2, 4, 5, 3, 6} Output: Postorder traversal post[] = {4, 5, 2, 6, 3, 1}AlgorithmSTART Step 1 -> declare function as find_value(int p, int in_order[], int n)    Loop For i=0 and i declare function as postorder(int pre_order[], int in_order[], int n)    Declare int variable as root = find_value(pre_order[0], in_order, n)    IF root!=0       Call postorder(pre_order+1, in_order, root)    End    IF ... Read More

Print numbers having first and last bits as the only set bits

Sunidhi Bansal
Updated on 30-Jul-2019 22:30:26

148 Views

The task is to print the given n number which have exactly two set bits that neither less than 2 nor more than 2.Set bits in computer language are the one that have value 1 and unset bits have value as 0Input: value of num=5 Output: 1 3 5    As 1 is equivalent to 1 in binary       3 is equivalent to 11 in binary       5 is equivalent to 101 in binaryAlgorithmSTART Step 1 -> declare variable as unsigned int num=5 and int i=1 Step 2 -> print i Step 3 -> Loop For i=3 and i

Advertisements