50 Sample Questions
50 Sample Questions
int main()
{
int a = 1, b = 2, c = 3;
// remaining conditions
// c is greatest
else
printf("%d", c);
return 0;
}
Output
3
int main() {
int n = 91;
int cnt = 0;
// If number is less than/equal to 1,
// it is not prime
if (n <= 1)
printf("%d is NOT prime\n", n);
else {
// else it is prime
else
printf("%d is prime", n);
}
return 0;
}
Output
91 is NOT prime
// Driver code
int main()
{
// Principal amount
double principal = 2300;
// Time
double time = 4;
Output
Compound Interest is : 714.830823
int main()
{
int x = 10;
int y = 20;
Output
x: 10 , y: 20
x: 20 , y: 10
int main()
{
int N = 102301;
int ans = 0;
int i = 0;
while (N != 0) {
// Condition to change value
if (N % 10 == 0)
ans = ans + 1 * pow(10, i);
else
ans = ans + (N % 10) * pow(10, i);
N = N / 10;
i++;
}
printf("%d", ans);
return 0;
}
Output:
112311
int main()
{
int N = 11011;
printf("%d", ans);
return 0;
}
Output
27
int main()
{
leap_year(2000);
leap_year(2002);
leap_year(2008);
return 0;
}
Output
2000 is a leap year.
2002 is not a leap year.
2008 is a leap year.
// Recursive call
return N * factorial(N - 1);
}
int main()
{
int n;
n = 13;
factorial_iteration(n);
n = 9;
printf("Factorial of %d using recursion:%d\n", n,
factorial(n));
return 0;
}
Output
Factorial of 13 is 6227020800
Factorial of 9 using recursion:362880
// Driver Program
int main()
{
int x = 120;
if (isArmstrong(x) == 1)
printf("True\n");
else
printf("False\n");
x = 1634;
if (isArmstrong(x) == 1)
printf("True\n");
else
printf("False\n");
return 0;
}
Output
False
True
int d = (b * b) - (4 * a * c);
double sqrt_val = sqrt(abs(d));
if (d > 0) {
printf("Roots are real and different \n");
printf("%f\n%f", (double)(-b + sqrt_val) / (2 * a),
(double)(-b - sqrt_val) / (2 * a));
}
else if (d == 0) {
printf("Roots are real and same \n");
printf("%f", -(double)b / (2 * a));
}
else // d < 0
{
printf("Roots are complex \n");
printf("%f + i%f\n%f - i%f", -(double)b / (2 * a),
sqrt_val / (2 * a), -(double)b / (2 * a),
sqrt_val / (2 * a));
}
}
// Driver code
int main()
{
int a = 1, b = -16, c = 1;
// Function call
find_roots(a, b, c);
return 0;
}
Output:
Roots are real and different
15.937254
0.062746
// Iterative approach
int reverse_iteration(int N)
{
int ans = 0;
while (N != 0) {
return ans;
}
// recursive approach
int reverse(int n, int ans)
{
if (n == 0)
return ans;
N = reverse_iteration(N);
printf("%d after reverse using iteration\n", N);
int ans = 0;
ans = reverse(N, ans);
printf("%d after again reverse using recursion", ans);
return 0;
}
Output
Initial number:15942
24951 after reverse using iteration
15942 after again reverse using recursion
int main()
{
int N = 13431;
int M = 12345;
// Function call
check_palindrome(N);
check_palindrome(M);
return 0;
}
Output
13431 is palindrome
12345 is not a palindrome
13. Write a C Program to check if two numbers are equal without using the
bitwise operator.
C
// C Program for checking numbers
// are equal using bitwise operator
#include <stdio.h>
int main()
{
int x = 1;
int y = 2;
// Using XOR
// XOR of two equal numbers is 0
if (!(x ^ y))
printf(" %d is equal to %d ", x, y);
else
printf(" %d is not equal to %d ", x, y);
return 0;
}
Output
1 is not equal to 2
Output
GCD of 98 and 56 is 14
// Otherwise
else
return LCM(Num1, Num2, K + 1);
}
// If K exceeds minimum
else
return Num1 * Num2;
}
int main()
{
// Given N & M
int N = 12, M = 9;
// Function Call
int ans = LCM(N, M, 2);
printf("%d", ans);
return 0;
}
Output
36
int main()
{
int a = 55, b = 23;
return 0;
}
Output
max = 55
min = 23
Output
1 2 3 4 5 6 7 8 9 10
int main()
{
printf("Area is %f", findArea(5));
return 0;
}
Output
Area is 78.550000
int main()
{
int N = 5;
// Outer Loop for number of rows
for (int i = 1; i <= N; i++) {
Output
*
***
*****
*******
*********
int main()
{
int n = 5;
return 0;
}
Output
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
int main()
{
int n = 5;
generateNthrow(n);
return 0;
}
Output
1,5 ,10 ,10 ,5 ,1
int main()
{
int arr[] = { 1, 2, 3, 4, 5 };
// Function Call
reverse(arr, 5);
return 0;
}
Output
54321
int flag = 0;
while (i < n - 1 && arr[i] == arr[i + 1]) {
flag = 1;
i++;
}
if (flag)
printf("%d ", (arr[i - 1]));
}
return;
}
int main()
{
int arr[] = { 1, 3, 4, 1, 2, 3, 5, 5 };
Sort(arr,n);
findRepeating(arr,n);
return 0;
}
Output
135
int main()
{
int arr[] = { 15, 14, 35, 2, 11, 83 };
int len = sizeof(arr) / sizeof(arr[0]);
// Function call
find_small_large(arr, len);
return 0;
}
Output
Smallest: 2 and Largest: 83
// Print array
void printArray(int arr[], int n)
{
int i;
for (i = 0; i < n; i++)
printf("%d ", arr[i]);
}
// Rotate array
void Rotate(int arr[], int k, int N)
{
int i, j, a, temp;
k = k % N;
temp = arr[i];
j = i;
while (1) {
a = j + k;
if (a >= N)
a = a - N;
if (a == i)
break;
arr[j] = arr[a];
j = a;
}
arr[j] = temp;
}
}
int main()
{
int arr[] = { 1, 2, 3, 4, 5 };
// Rotating array
Rotate(arr, 2, 5);
// Printing array
printArray(arr, 5);
return 0;
}
Output
34512
26. Write a Program to sort First half in Ascending order and the Second in
Descending order.
C
// C Program for Sorting
// First half in Ascending order
// and Second Descending order
#include <stdio.h>
int main()
{
int arr[] = { 11, 23, 42, 16, 83, 73, 59 };
int N = sizeof(arr) / sizeof(arr[0]);
Sort_asc_desc(arr, N);
return 0;
}
Output
11 16 23 83 73 59 42
// driver code
int main()
{
int arr[] = { 1, 2, 3 };
int n = sizeof(arr) / sizeof(arr[0]);
Output
63415230
28. Write a Program to Find if there is any subarray with a sum equal to 0.
C
// C Program to check 0 sum
// subarray possible
#include <stdio.h>
int main()
{
// array
int arr[] = { -2, 2, 1, 1, 8 };
int n = sizeof(arr) / sizeof(arr[0]);
if (sum == 0) {
flag = 1;
printf(
"True subarray with 0 sum is possible");
break;
}
}
}
if (flag == 0)
printf("No such condition");
}
Output
True subarray with 0 sum is possible
int main()
{
int a[] = { -2, -3, 4, -1, -2, 1, 5, -3 };
int n = sizeof(a) / sizeof(a[0]);
if (max_ending_here < 0) {
max_ending_here = 0;
s = i + 1;
}
}
printf("Maximum contiguous sum is %d\n", max_so_far);
printf("Starting index %d Ending index %d", start, end);
return 0;
}
Output
Maximum contiguous sum is 7
Starting index 2 Ending index 6
int main()
{
int M = 3;
int N = 4;
int A[3][4] = { { 1, 1, 1, 1 },
{ 2, 2, 2, 2 },
{ 3, 3, 3, 3 } };
transpose(N, M, A, B);
return 0;
}
Output
Result matrix is
123
123
123
123
int main()
{
int n = 4;
int arr[4][4] = { { 1, 2, 3, 4 },
{ 5, 6, 7, 8 },
{ 9, 10, 11, 12 },
{ 13, 14, 15, 16 } };
Output
Orignal Matrix:
1234
5678
9 10 11 12
13 14 15 16
Matrix after rotation:
13 9 5 1
14 10 6 2
15 11 7 3
16 12 8 4
int main()
{
int arr[4][4] = { { 1, 5, 9, 13 },
{ 2, 6, 10, 14 },
{ 3, 7, 11, 15 },
{ 4, 8, 12, 16 } };
int m = 4, n = 4;
int i, l = 0, right = m - 1, begin = 0, end = n - 1;
return 0;
}
Output
1 5 9 13 14 15 16 12 8 4 3 2 6 10 11 7
int main()
{
int ans = 0;
// iterate through all the number
for (int i = 0; s[i] != '\0'; i++) {
int ele = s[i] - 48;
if (ele <= 9)
ans += ele;
}
return 0;
}
Output
23
return length(s, i + 1) + 1;
}
int main()
{
char s[] = "GeeksforGeeks";
Output
length using strlen:13
length using iteration:13
length using recursion:13
if (flag == 0)
printf("%s is a palindrome\n", str);
}
int main()
{
char str[] = { "GeekeeG" };
char str2[] = { "GeeksforGeeks" };
check_palindrome(str);
return 0;
}
Output
GeekeeG is a palindrome
Checking GeeksforGeeks using recursive approach
Not a Palindrome
return ceilIndex;
}
int isFinished = 0;
while (!isFinished) {
printf("%s \n", str);
int i;
for (i = size - 2; i >= 0; --i)
if (str[i] < str[i + 1])
break;
if (i == -1)
isFinished = 1;
else {
int ceilIndex
= findCeil(str, str[i], i + 1, size - 1);
swap(&str[i], &str[ceilIndex]);
qsort(str + i + 1, size - i - 1, sizeof(str[0]),
compare);
}
}
}
int main()
{
char str[] = "123";
sortedPermutations(str);
return 0;
}
Output
123
132
213
231
312
321
37. Write a program to calculate the Power of a Number using Recursion in
C.
C
// C program to calculate the Power of a Number using
// Recursion
#include <stdio.h>
return power(a, b - 1) * a;
}
int main()
{
int a = 4, b = 5;
printf("%d", ans);
return 0;
}
Output
1024
int fibonacci(int n)
{
if (n <= 1)
return n;
return fibonacci(n - 1) + fibonacci(n - 2);
}
int fibonacci_iteration(int n)
{
if (n <= 1)
return 1;
return arr[n];
}
int main()
{
int n = 9;
printf("Fibonacci using recursion of %d:%d\n", n,
fibonacci(n));
n = 11;
printf("Fibonacci using iteration of %d:%d", n,
fibonacci_iteration(n));
return 0;
}
Output
Fibonacci using recursion of 9:34
Fibonacci using iteration of 11:144
39. Write a Program to find the HCF of two Numbers using Recursion.
C
// C program to find
// GCD of two numbers
#include <stdio.h>
// Recursive function to
// Calculate and return gcd of a and b
int gcd(int a, int b)
{
// Everything divides 0
if (a == 0)
return b;
if (b == 0)
return a;
// base case
if (a == b)
return a;
// a is greater
if (a > b)
return gcd(a - b, b);
return gcd(a, b - a);
}
int main()
{
int a = 192, b = 36;
printf("GCD of %d and %d is %d ", a, b, gcd(a, b));
return 0;
}
Output
GCD of 192 and 36 is 12
int main()
{
char a[] = "Geeks for Geeks";
printf("Orignal string:%s\n", a);
reverse_iteration(a);
printf("Reverse the string(iteration):%s\n", a);
return 0;
}
Output
Orignal string:Geeks for Geeks
Reverse the string(iteration):skeeG rof skeeG
Using recursion for reverse:Geeks for Geeks
// Function Call
int result = search(arr, N, x);
if (result == -1) {
printf("Element is not present in array");
}
else {
printf("Element is present at index %d", result);
}
return 0;
}
Output
Element is present at index 4
return -1;
}
int main()
{
int arr[] = { 11, 14, 19, 23, 40 };
int n = sizeof(arr) / sizeof(arr[0]);
int x = 40;
int result = binarySearch(arr, 0, n - 1, x);
if (result == -1) {
printf("Element is not present in array");
}
else {
printf("Element is present at index %d", result);
}
return 0;
}
Output
Element is present at index 4
43. Write a C Program to sort arrays using Bubble, Selection, and Insertion
Sort.
C
// C Program to implement
// Sorting Algorithms
#include <stdio.h>
int main()
{
int arr1[] = { 9, 4, 3, 11, 1, 5 };
int arr2[] = { 4, 3, 9, 1, 5, 11 };
int arr3[] = { 5, 1, 11, 3, 4, 9 };
int n = 6;
// sort array
bubble_sort(arr1, n);
// printing array
printf("Sorted array using Bubble sort: ");
for (int i = 0; i < n; i++)
printf("%d ", arr1[i]);
printf("\n");
// sort array
insertionSort(arr2, n);
// printing array
printf("Sorted array using Insertion Sort: ");
for (int i = 0; i < n; i++)
printf("%d ", arr2[i]);
printf("\n");
printf("Non-Sorted array: ");
for (int i = 0; i < n; i++)
printf("%d ", arr3[i]);
printf("\n");
// sort array
selectionSort(arr3, n);
// printing array
printf("Sorted array using Selection Sort: ");
for (int i = 0; i < n; i++)
printf("%d ", arr3[i]);
printf("\n");
return 0;
}
Output
Non-Sorted array: 9 4 3 11 1 5
Sorted array using Bubble sort: 1 3 4 5 9 11
Non-Sorted array: 4 3 9 1 5 11
Sorted array using Insertion Sort: 1 3 4 5 9 11
Non-Sorted array: 5 1 11 3 4 9
Sorted array using Selection Sort: 1 3 4 5 9 11
int main()
{
int arr[] = { 23, 9, 13, 15, 6, 7 };
int n = sizeof(arr) / sizeof(arr[0]);
mergeSort(arr, 0, n - 1);
return 0;
}
Output
Given array:23 9 13 15 6 7
Sorted array :6 7 9 13 15 23
return (i + 1);
}
printf("Unsorted Array:");
printArray(arr, n);
quickSort(arr, 0, n - 1);
return 0;
}
Output
Unsorted Array:28 7 20 1 10 3 6
Sorted array :1 3 6 7 10 20 28
// Driver code
int main()
{
int n = 5;
int arr[] = { 13, 22, 7, 12, 4 };
sort(n, arr);
return 0;
}
Output
4 7 12 13 22
// Driver code
int main()
{
int n = 3;
// Create the student's structure variable
// with n Student's records
struct Student student[n];
student[1].roll_number = 2;
student[1].name = "Geeks2";
student[1].age = 11;
student[2].roll_number = 3;
student[2].name = "Geeks3";
student[2].age = 13;
return 0;
}
Output
Student Records:
return (add);
}
int main()
{
return 0;
}
Output
x = 4 + 5i
y = 7 + 11i
sum = 11 + 16i
49. Write a C Program to add Two Distance Given as Input in Feet and
Inches
C
// C program for calculating sum of
// Distance in intches and feet
#include "stdio.h"
int x;
int main()
{
struct InchFeet arr[]
= { { 11, 5.1 }, { 13, 4.5 }, { 6, 8.1 } };
findSum(arr, N);
return 0;
}
Output
Feet Sum: 31
Inch Sum: 5.70
/* Driver code*/
int main()
{
/* Start with the empty list */
struct Node* head = NULL;
push(&head, 10);
push(&head, 14);
push(&head, 19);
push(&head, 25);
Output
Given linked list
25 19 14 10
Reversed linked list
10 14 19 25