Found 1339 Articles for C

Print the kth common factor of two numbers

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

331 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

211 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

369 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

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

244 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

Print prime numbers with prime sum of digits in an array

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

640 Views

Given with an array of elements and the task is to print those numbers whose digit sum is also prime and return -1 is not such digit exists in an arrayInput: arr[]={2, 4, 3, 19, 25, 6, 11, 12, 18, 7} Output : 2, 3, 25, 11, 12, 7Here, the given output is generated as it contains those additive numbers whose sum is also prime like − 2, 3, 7 are prime but 25(2+5=7), 11(1+1=2), 12(1+2=3) are also prime whereas numbers like 19(1+9=10) are not prime.AlgorithmSTART Step 1 -> Take array of int with values Step 2 -> declare start ... Read More

Print an array with numbers having 1, 2 and 3 as a digit in ascending order

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

715 Views

Here, the task is to print those number in an array having 1, 2 and 3 as digits in their numbers and if their is no such number than the output must be -1Input : arr[] = {320, 123, 124, 125, 14532, 126, 340, 123400, 100032, 13, 32, 3123, 1100} Output : 123 3123 14532 100032 123400 Since the array have values with digits 1, 2 and 3 it wouldn’t return -1 and print 5 values that Contain 1, 2 and 3 in their respective numbers.AlgorithmSTART Step 1 -> Declare array with elements of int type as arr Step ... Read More

Print matrix in diagonal pattern

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

446 Views

Given a 2d array of n*n and the task is to find the antispiral arrangement of the given matrixInput : arr[4][4]={1,2,3,4,    5,6,7,8,    9,10,11,12    13,14,15,16} Output : 1 6 11 16 4 7 10 13AlgorithmSTART Step 1 -> declare start variables as r=4, c=4, i and j Step 2 -> initialize array as mat[r][c] with elements Step 3 -> Loop For i=0 and i print Step 5 -> Loop For i=0 and i

Print matrix in antispiral form

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

411 Views

Given a 2d array of n*n and the task is to find the antispiral arrangement of the given matrixInput : arr[4][4]={1,2,3,4,    5,6,7,8,    9,10,11,12    13,14,15,16} Output: 10 11 7 6 5 9 13 14 15 16 12 8 4 3 2 1For this, stack can be used where the transpose of a matrix can be pushed inside stack and poped reverselyAlgorithmSTART STEP 1 -> declare stack vector element as stk and variables as int r=4, c=4, i, j, rs=0 and cs=0 Step 2 -> store matrix elements in 2-3 array Step 3 -> Loop For i=0 and o

Advertisements