Problem For Solving
Problem For Solving
4. Write a program in C to read 10 numbers from the keyboard and find their sum
and average.
Test Data :
Input the 10 numbers :
Number-1 :2
...
Number-10 :2
Expected Output :
The sum of 10 no is : 55
The Average is : 5.500000
8. Write a C program to display the n terms of odd natural numbers and their
sum.
Test Data
Input number of terms : 10
Expected Output :
The odd numbers are :1 3 5 7 9 11 13 15 17 19
The Sum of odd Natural Number upto 10 terms : 100
*
**
***
****
10. Write a C program to display a pattern like a right angle triangle with a
number.
1
12
123
1234
11. Write a program in C to make such a pattern like a right angle triangle with a
number which will repeat a number in a row.
1
22
333
4444
12. Write a program in C to make such a pattern like a right angle triangle with
the number increased by 1.
1
23
456
7 8 9 10
*
**
***
****
16. Write a C program to display the sum of n terms of even natural numbers.
Test Data :
Input number of terms : 5
Expected Output :
The even numbers are :2 4 6 8 10
The Sum of even Natural Number upto 5 terms : 30
17. Write a C program to make such a pattern like a pyramid with a number
which will repeat the number in the same row.
1
22
333
4444
18. Write a program in C to find the sum of the series [ 1-X^2/2!+X^4/4!- .........].
Test Data :
Input the Value of x :2
Input the number of terms : 5
Expected Output :
the sum = -0.415873
Number of terms = 5
value of x = 2.000000
19. Write a program in C to display the n terms of a harmonic series and their
sum.
1 + 1/2 + 1/3 + 1/4 + 1/5 ... 1/n terms
Test Data :
Input the number of terms : 5
Expected Output :
1/1 + 1/2 + 1/3 + 1/4 + 1/5 +
Sum of Series upto 5 terms : 2.283334
20. Write a C program to display the pattern as a pyramid using asterisks, with
each row containing an odd number of asterisks.
*
***
*****
1
01
101
0101
10101
23. Write a program in C to find the sum of the series [x - x^3 + x^5 + ......].
Test Data :
Input the value of x :3
Input number of terms : 5
Expected Output :
The sum is : 16.375000
24. Write a program in C to find the sum of the series [ x - x^3 + x^5 + ......].
Test Data :
Input the value of x :2
Input number of terms : 5
Expected Output :
The values of the series:
2
-8
32
-128
512
The sum = 410
25. Write a C program that displays the n terms of square natural numbers and
their sum.
1 4 9 16 ... n Terms
Test Data :
Input the number of terms : 5
Expected Output :
The square natural upto 5 terms are :1 4 9 16 25
The Sum of Square Natural Number upto 5 terms = 55
26. Write a program in C to find the sum of the series 1 +11 + 111 + 1111 + .. n
terms.
Test Data :
Input the number of terms : 5
Expected Output :
1 + 11 + 111 + 1111 + 11111
The Sum is : 12345
28. Write a C program to find the 'Perfect' numbers within a given number of
ranges.
Test Data :
Input the starting range or number : 1
Input the ending range of number : 50
Expected Output :
The Perfect numbers within the given range : 6 28
*
***
*****
*******
*********
*******
*****
***
*
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
34. Write a program in C to find the prime numbers within a range of numbers.
Test Data :
Input starting number of range: 1
Input ending number of range : 50
Expected Output :
The prime number between 1 and 50 are :
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47
35. Write a program in C to display the first n terms of the Fibonacci series.
Fibonacci series 0 1 2 3 5 8 13 .....
Test Data :
Input number of terms to display : 10
Expected Output :
Here is the Fibonacci series upto to 10 terms :
0 1 1 2 3 5 8 13 21 34
36. Write a C program to display a such a pattern for n rows using a number that
starts with 1 and each row will have a 1 as the first and last number.
1
121
12321
40. Write a C program to display the pyramid pattern using the alphabet.
A
ABA
ABCBA
ABCDCBA
41. Write a program in C to convert a decimal number into binary without using
an array.
Test Data :
Input a decimal number: 25
Binary number equivalent to said decimal number is:
0000000000000000000000000001 1001
42. Write a C program to convert a binary number into a decimal number without
using array, function and while loop.
Test Data :
Input a binary number :1010101
Expected Output :
The Binary Number : 1010101
The equivalent Decimal Number : 85
43. Write a C program to find the HCF (Highest Common Factor) of two
numbers.
Test Data :
Input 1st number for HCF: 24
Input 2nd number for HCF: 28
Expected Output :
HCF of 24 and 28 is : 4
44. Write a C program to find the LCM of any two numbers using HCF.
Test Data :
Input 1st number for LCM: 15
Input 2nd number for LCM: 20
Expected Output :
The LCM of 15 and 20 is : 60
46. Write a C program to convert a binary number into a decimal number using
the math function.
Test Data :
Input the binary number :1010100
Expected Output :
The Binary Number : 1010100
The equivalent Decimal Number is : 84
50. Write a program in C to convert a decimal number into octal without using an
array.
Test Data :
Enter a number to convert : 79
Expected Output :
The Octal of 79 is 117.
58. Write a C program to find the length of a string without using the library
function.
Test Data :
Input a string : welcome
Expected Output :
The string contains 7 number of characters.
So, the length of the string welcome is : 7
60. Write a C program that takes user input and counts the number of characters
until the end of the file.
Test Data :
Input characters : w3resource
Expected Output :
Input characters: On Linux systems and OS X EOF is CTRL+D. For Windows
EOF is CTRL+Z. Number of Characters: 10
61. Write a C program that takes input from the user and counts the number of
uppercase and lowercase letters, as well as the number of other characters.
Test Data :
Input characters : w3resource
Expected Output :
Input characters: On Linux systems and OS X EOF is CTRL+D. For Windows
EOF is CTRL+Z. Uppercase letters: 0 Lowercase letters: 9 Other characters: 1
4. Write a program in C to copy the elements of one array into another array.
Test Data :
Input the number of elements to be stored in the array :3
Input 3 elements in the array :
element - 0 : 15
element - 1 : 10
element - 2 : 12
Expected Output :
The elements stored in the first array are :
15 10 12
The elements copied into the second array are :
15 10 12
10. Write a program in C to separate odd and even integers into separate arrays.
Test Data :
Input the number of elements to be stored in the array :5
Input 5 elements in the array :
element - 0 : 25
element - 1 : 47
element - 2 : 42
element - 3 : 56
element - 4 : 32
Expected Output :
The Even elements are :
42 56 32
The Odd elements are :
25 47
12. Write a program in C to sort the elements of the array in descending order.
Test Data :
Input the size of array : 3
Input 3 elements in the array :
element - 0 : 5
element - 1 : 9
element - 2 : 1
Expected Output :
Elements of the array in sorted descending order:
951
13. Write a program in C to insert the values in the array (sorted list).
Test Data :
Insert New value in the sorted array :
-----------------------------------------
Input the size of array : 5
Input 5 elements in the array in ascending order:
element - 0 : 2
element - 1 : 5
element - 2 : 7
element - 3 : 9
element - 4 : 11
Input the value to be inserted : 8
The exist array list is :
2 5 7 9 11
After Insert the list is :
2 5 7 8 9 11
--------------------------------
Process exited after 39.33 seconds with return value 10
Press any key to continue . . .
18. Write a program in C for a 2D array of size 3x3 and print the matrix.
Test Data :
Input elements in the matrix :
element - [0],[0] : 1
element - [0],[1] : 2
element - [0],[2] : 3
element - [1],[0] : 4
element - [1],[1] : 5
element - [1],[2] : 6
element - [2],[0] : 7
element - [2],[1] : 8
element - [2],[2] : 9
Expected Output :
The matrix is :
123
456
789
19. Write a program in C for adding two matrices of the same size.
Test Data :
Input the size of the square matrix (less than 5): 2
Input elements in the first matrix :
element - [0],[0] : 1
element - [0],[1] : 2
element - [1],[0] : 3
element - [1],[1] : 4
Input elements in the second matrix :
element - [0],[0] : 5
element - [0],[1] : 6
element - [1],[0] : 7
element - [1],[1] : 8
Expected Output :
The First matrix is :
12
34
The Second matrix is :
56
78
The Addition of two matrix is :
68
10 12
56
78
The Second matrix is :
12
34
The Subtraction of two matrix is :
44
44
21. Write a program in C for the multiplication of two square matrices.
Test Data :
Input the rows and columns of first matrix : 2 2
Input the rows and columns of second matrix : 2 2
Input elements in the first matrix :
element - [0],[0] : 1
element - [0],[1] : 2
element - [1],[0] : 3
element - [1],[1] : 4
Input elements in the second matrix :
element - [0],[0] : 5
element - [0],[1] : 6
element - [1],[0] : 7
element - [1],[1] : 8
Expected Output :
The First matrix is :
12
34
The Second matrix is :
56
78
The multiplication of two matrix is :
19 22
43 50
12
34
23. Write a program in C to find the sum of the right diagonals of a matrix.
Test Data :
Input the size of the square matrix : 2
Input elements in the first matrix :
element - [0],[0] : 1
element - [0],[1] : 2
element - [1],[0] : 3
element - [1],[1] : 4
Expected Output :
The matrix is :
12
34
Addition of the right Diagonal elements is :5
Elements in array are:
24. Write a program in C to find the sum of the left diagonals of a matrix.
Test Data :
Input the size of the square matrix : 2
Input elements in the first matrix :
element - [0],[0] : 1
element - [0],[1] : 2
element - [1],[0] : 3
element - [1],[1] : 4
Expected Output :
The matrix is :
12
34
Addition of the left Diagonal elements is :5
25. Write a program in C to find the sum of rows and columns of a matrix.
Test Data :
Input the size of the square matrix : 2
Input elements in the first matrix :
element - [0],[0] : 5
element - [0],[1] : 6
element - [1],[0] : 7
element - [1],[1] : 8
Expected Output :
The First matrix is :
The matrix is :
56
78
The sum or rows and columns of the matrix is :
5 6 11
7 8 15
12 14
26. Write a program in C to print or display the lower triangular of a given matrix.
Test Data :
Input the size of the square matrix : 3
Input elements in the first matrix :
element - [0],[0] : 1
element - [0],[1] : 2
element - [0],[2] : 3
element - [1],[0] : 4
element - [1],[1] : 5
element - [1],[2] : 6
element - [2],[0] : 7
element - [2],[1] : 8
element - [2],[2] : 9
Expected Output :
The matrix is :
123
456
789
123
056
009
100
450
789
30. Write a program in C to accept two matrices and check whether they are
equal.
Test Data :
Input Rows and Columns of the 1st matrix :2 2
Input Rows and Columns of the 2nd matrix :2 2
Input elements in the first matrix :
element - [0],[0] : 1
element - [0],[1] : 2
element - [1],[0] : 3
element - [1],[1] : 4
Input elements in the second matrix :
element - [0],[0] : 1
element - [0],[1] : 2
element - [1],[0] : 3
element - [1],[1] : 4
Expected Output :
The first matrix is :
12
34
The second matrix is :
12
34
The Matrices can be compared :
Two matrices are equal.
32. Write a program in C to find a pair with given sum in the array.
Expected Output :
The given array : 6 8 4 -5 7 9
The given sum : 15
Pair of elements can make the given sum by the value of index 0 and 5
34. Write a program in C to find the number occurring odd number of times in an
array.
All numbers occur even number of times except one number which occurs odd
number of times.
Expected Output :
The given array is : 8 3 8 5 4 3 4 3 5
The element odd number of times is : 3
36. Write a program in C to find the missing number in a given array. There are
no duplicates in the list.
Expected Output :
The given array is : 1 3 4 2 5 6 9 8
The missing number is : 7
37. Write a program in C to find the pivot element of a sorted and rotated array
using binary search.
Pivot element is the only element in input array which is smaller than it's previous
element.
A pivot element divided a sorted rotated array into two monotonically increasing
array.
Expected Output :
The given array is : 14 23 7 9 3 6 18 22 16 36
The Pivot Element in the array is : 3
38. Write a program in C to merge one sorted array into another sorted array.
Pivot element is the only element in input array which is smaller than it's previous
element.
A pivot element divided a sorted rotated array into two monotonically increasing
array.
Expected Output :
The given Large Array is : 10 12 14 16 18 20 22
The given Small Array is : 11 13 15 17 19 21
After merged the new Array is :
10 11 12 13 14 15 16 17 18 19 20 21 22
41. Write a program in C to find the Floor and Ceiling of the number 0 to 10 from
a sroted array.
Expected Output :
The given array is : 1 3 5 7 8 9
Number: 0 ceiling is: 1 floor is: -1
Number: 1 ceiling is: 1 floor is: 1
Number: 2 ceiling is: 3 floor is: 1
Number: 3 ceiling is: 3 floor is: 3
Number: 4 ceiling is: 5 floor is: 3
Number: 5 ceiling is: 5 floor is: 5
Number: 6 ceiling is: 7 floor is: 5
Number: 7 ceiling is: 7 floor is: 7
Number: 8 ceiling is: 8 floor is: 8
Number: 9 ceiling is: 9 floor is: 9
Number: 10 ceiling is: -1 floor is: 9
42. Write a program in C to find the smallest missing element in a sorted array.
Expected Output :
The given array is : 0 1 3 4 5 6 7 9
The missing smallest element is: 2
43. Write a program in C to print the next greatest elements in a given unsorted
array. Elements for which no superior element exists, consider the next greatest
element as -1.
Expected Output :
The given array is : 5 3 10 9 6 13
Next Bigger Elements are:
Next bigger element of 5 in the array is: 10
Next bigger element of 3 in the array is: 10
Next bigger element of 10 in the array is: 13
Next bigger element of 9 in the array is: 13
Next bigger element of 6 in the array is: 13
Next bigger element of 13 in the array is: -1
Next Bigger Elements Array:
10 10 13 13 13 -1
44. Write a program in C to find the two repeating elements in a given array.
Expected Output :
The given array is : 2 7 4 7 8 3 4
The repeating elements are: 7 4
45. Write a program in C to find two elements whose sum is closest to zero.
Expected Output :
The given array is : 38 44 63 -51 -35 19 84 -69 4 -46
The Pair of elements whose sum is minimum are:
[44, -46]
46. Write a program in C to find the smallest positive number missing from an
unsorted array.
Expected Output :
The given array is : 3 1 4 10 -5 15 2 -10 -20
The smallest positive number missed is: 5
47. Write a program in C to find a subarray with a given sum from the given
array.
Expected Output :
The given array is : 3 4 -7 1 3 3 1 -4
[0..1] -- { 3 4 }
[0..5] -- { 3 4 -7 1 3 3 }
[3..5] -- { 1 3 3 }
[4..6] -- { 3 3 1 }
48. Write a program in C to find out if a given integer x appears more than n/2
times in a sorted array of n integers.
Expected Output :
The given array is : 1 3 3 5 4 3 2 3 3
The given value is : 3
3 appears more than 4 times in the given array[]
51. Write a program in C to find the maximum circular subarray sum of a given
array.
Expected Output :
The given array is : 10 8 -20 5 -3 -5 10 -13 11
The maximum circular sum in the above array is: 29
52. Write a program in C to count the number of triangles that can be formed
from a given array.
Expected Output :
The given array is : 6 18 9 7 10
Number of possible triangles can be formed from the array is: 5
53. Write a program in C to find the number of times a given number appears in
an array.
Expected Output :
The given array is : 2 3 4 4 4 4 5 5 5 6 7 7
The number of times the number 4 occurs in the given array is: 4
56. Write a program in C to return the minimum number of jumps to reach the
end of the array.
Expected Output :
The given array is : 1 3 5 8 9 2 6 7 6 8 9 1 1 1
The minimum of number of jumps is required to reach the end is: 3
57. Write a program in C to find the minimum element in a sorted and rotated
array.
Expected Output :
The given array is : 3 4 5 6 7 9 2
The minimum element in the above array is: 2
58. Write a program in C to move all zeroes to the end of a given array.
Expected Output :
The given array is : 2 5 7 0 4 0 7 -5 8 0
The new array is:
2 5 7 8 4 -5 7 0 0 0
60. Write a program in C to find the row with the maximum number of 1s.
Expected Output :
The given 2D array is :
01011
11111
10010
00000
10001
The index of row with maximum 1s is: 1
61. Write a program in C to find the maximum product subarray in a given array.
Expected Output :
The given array is : -4 9 -7 0 -15 6 2 -3
The maximum product of a sub-array in the given array is: 540
62. Write a program in C to find the largest subarray with an equal number of 0s
and 1s.
Expected Output :
The given array is : 0 1 0 0 1 1 0 1 1 1
Subarray found from the index 0 to 7
63. Write a program in C to replace every element with the greatest element on
its right side.
Expected Output :
The given array is : 7 5 8 9 6 8 5 7 4 6
After replace the modified array is: 9 9 9 8 8 7 7 6 6 0
64. Write a program in C to find the median of two sorted arrays of the same
size.
Expected Output :
The given array - 1 is : 1 5 13 24 35
The given array - 2 is : 3 8 15 17 32
The Median of the 2 sorted arrays is: 14
65. Write a program in C to find the product of an array such that product is equal
to the product of all the elements of arr[] except arr[i].
Expected Output :
The given array is : 1 2 3 4 5 6
The product array is: 720 360 240 180 144 120
67. Write a program in C to search for an element in a row wise and column wise
sorted matrix.
Expected Output :
The given array in matrix form is :
15 23 31 39
18 26 36 43
25 28 37 48
30 34 39 50
The given value for searching is: 37
The element Found at the position in the matrix is: 2, 2
68. Write a program in C to return the maximum sum such that no two elements
are adjacent.
Expected Output :
The given array is : 1 3 5 9 7 10 1 10 100
The maximum sum from the array such that no two elements are adjacent is: 122
69. Write a program in C to find the maximum difference between any two
elements such that the larger element appears after the smaller number.
Expected Output :
The given array is : 7 9 5 6 13 2
The elements which provide maximum difference is: 5, 13
The Maximum difference between two elements in the array is: 8
70. Write a program in C to find two numbers that occur an odd number of times
in an array.
Expected Output:
The given array is: 6 7 3 6 8 7 6 8 3 3
The two numbers occuring odd number of times are: 3 & 6
71. Write a program in C to find the median of two sorted arrays of different
sizes.
Expected Output:
The given first array is : 90 240 300
The given second array is : 10 13 14 20 25
The median of two different size arrays are : 22.500000
72. Write a program in C to return only the unique rows from a given binary
matrix.
Expected Output:
The given array is :
01001
10110
01001
10100
The unique rows of the given array are :
01001
10110
10100
74. Write a program in C to find the sum of the upper triangular elements of a
matrix.
Expected Output:
The given array is :
123
456
789
The elements being summed of the upper triangular matrix are: 2 3 6
The Sum of the upper triangular Matrix Elements are: 11
75. Write a program in C to find the sum of the lower triangular elements of a
matrix.
Expected Output:
The given array is :
123
456
789
The elements being summed of the lower triangular matrix are: 4 7 8
The Sum of the lower triangular Matrix Elements are: 19
76. Write a program in C to find the largest number possible from the set of given
numbers.
Expected Output:
The given numbers are :
15 628 971 9 2143 12
The largest possible number by the given numbers are: 997162821431512
78. Write a program in C to find four array elements whose sum is equal to a
given number.
Expected Output:
The given array is:
3 7 1 9 15 14 6 2 5 7
The elements are:
3, 15, 14, 5
80. Write a program in C to count all distinct pairs for a specific difference.
Expected Output:
The given array is:
52376498
The distinct pairs for difference 5 are: [7, 2] [8, 3] [9, 4]
Number of distinct pairs for difference 5 are: 3
81. Write a program in C to find the maximum repeating number in a given array.
The array range is [0..n-1] and the elements are in the range [0..k-1] and k<=n..
Expected Output:
The given array is:
23353417777
The maximum repeating number is: 7
84. Write a program in C to find the minimum distance between two numbers in a
given array.
Expected Output:
The given array is:
7 9 5 11 7 4 12 6 2 11
The minimum distance between 7 and 11 is: 1
85. Write a program in C to count all possible paths from top left to bottom right
of a m X n matrix.
Expected Output:
The size of matrix is : 4 x 4
The all possible paths from top left to bottom right is: 20
87. Write a program in C to find the maximum element in an array that is first
increasing and then decreasing.
Expected Output:
The given array is:
2 7 12 25 4 57 27 44
The maximum element which is increasing then decreasing is: 57
88. Write a program in C to find the maximum n – m such that array[n] > array[m]
from a given array[].
Given an array arr[], find the maximum j – i such that arr[j] > arr[i]
Expected Output:
The given array is:
7582324210
m = 0, n = 2, arr1[m] = 7 arr1[n] = 8 difference = 2
m = 3, n = 6, arr1[m] = 2 arr1[n] = 4 difference = 3
The maximum differcences between two position of array index is: 3
89. Write a program in C to find the largest square sub-matrix with all 1s.
Expected Output:
The given array in matrix form is :
01011
11110
11110
11110
11111
01010
The maximum size sub-matrix is:
1111
1111
1111
1111
90. Given an array of size n such that every element is in the range from 0 to n-1.
Write a program in C to rearrange the given array so that arr[i] becomes
arr[arr[i]].
Expected Output:
The Original array is
2 1 4 3 0 The modified array is:
41032
91. An unsorted array of a specific size is given. Write a program in C to find the
minimum length of a subarray such that sorting this subarray makes the whole
array sorted.
Expected Output:
The given array is:
10 12 15 17 28 32 42 18 56 59 67
The minimum length of unsorted subarray which makes the given array sorted
lies between the indeces 4 and 7
92. Write a program in C that checks whether the elements in an unsorted array
appear consecutively or not.
Expected Output:
The given array is:
743562
The appearence of elements in the array are consecutive.
The given array is:
744562
The appearence of elements in the array are not consecutive.
The given array is:
749563
The appearence of elements in the array are not consecutive.
93. Write a program in C to rearrange positive and negative numbers
alternatively in a given array.
N.B.: If positive numbers are more they appear at the end and for also negative
numbers, they too appear in the end of the array.
Expected Output:
The given array is:
-4 8 -5 -6 5 -9 7 1 -21 -11 19
The rearranged array is:
-4 7 -5 1 -21 5 -11 8 -9 19 -6
94. Write a program in C to find the largest element of each and every
contiguous subarray of size k from a given array.
Expected Output:
The given array is:
1 3 6 21 4 9 12 3 16 10
The length of each subarray is: 4
The contigious subarray of length 4 and their maximum value are:
1 3 6 21 ----> 21
3 6 21 4 ----> 21
6 21 4 9 ----> 21
21 4 9 12 ----> 21
4 9 12 3 ----> 12
9 12 3 16 ----> 16
12 3 16 10 ----> 16
97. Write a program in C to find the index of the first peak element in a given
array.
Expected Output:
The given array is:
5 12 13 20 16 19 11 7 25
The index of first peak element in the array is: 3
98. Write a program in C to return the largest span found in the leftmost and
rightmost appearances of the same value (values are inclusive) in a given array.
Expected Output:
The given array is:
17 42 19 7 27 24 17 54 73
The span between the same values in the array is: 7
99. Write a program in C to return true if an array can be split in such a way that
the left side of the splitting is equal to the sum of the right side.
Expected Output:
The given array is : 1 3 3 8 4 3 2 3 3
The array can be split in a position where the sum of both side are equal.
Expected Output:
The given array is:
2 5 -1 6 -1 8 7 -1 9 1
The new array is: -1 1 2 -1 -1 5 6 7 8 9
104. Write a program in C to rearrange an array such that even index elements
are smaller and odd index elements are greater than their next.
Expected Output:
The array given is:
642183
The new array after rearranging:
461823
105. Write a program in C to find the minimum number of swaps required to
gather all elements less than or equal to k.
Expected Output:
The given array is:
2795874
The minimum swap required is: 2
106. Write a C program to convert an array in such a way that it doubles its
value. This will replace the next element with 0 if the current and next elements
are the same. This program will rearrange the array so that all 0's are moved to
the end.
Expected Output:
The given array is: 0 3 3 3 0 0 7 7 0 9
The new array is: 6 3 14 9 0 0 0 0 0 0