CSE110 Practice Problems
CSE110 Practice Problems
expressions, typecasting”:
1. Given three numbers, swap this in cyclic order. Suppose a=5, b=6, c=7. Then the output
will be a= 7, b= 5, c= 6.
a. Solve it using temporary variable
b. Solve it without using any temporary variable
2. Given a 4 digits number, reverse the number and print it.
a. Solve it following any approach.
b. Solve it using a single equation.
3. Given a letter, print the next letter. If the letter is z, then the output will be a. OR
4. i. Given a capital letter X, print the letter N position ahead of the given letter. Consider
that there are 26 letters in the alphabet. If the position of X+N goes beyond 26, rotate
your counter to start from the beginning (‘A’).
Example:
X='A', N=25, Output = 'Z'
X='A', N=27, Output = 'B'
ii. Solve the above problem, but print the letter that is N position behind from the given
letter considering rotation.
Practice for “Conditional statements”:
1. Given a 4 digit number as input, print the second largest digit.
Input : 5764
Output: 6
2. Take two numbers and a operator (+, -, *, /) as input and print the output after the
operation. Use Switch Case to do this.
Input: 2 3 ‘+’
Output: 5
Input: 5.7 4.4 ‘-’
Output: 1.3
****
***
**
6. Write a C program to print the following pyramid pattern using nested loops:
***
*****
*******
*********
7. Write a C program to generate the Fibonacci series up to the nth term.
8. Write a C program to check whether a given number is a palindrome or not.
Write the C code of a program that adds all numbers that are multiples of both 7 and 9 up to
600 (including 600) i.e. 63, 126, 189, 252, ....
[Solve it without using conditional statement]
The output of your program should be: 2835
since 63 + 126 + 189 + 252 + 315 + 378 + 441 + 504 + 567 = 2835
Write a C program which takes a number and Write a C program that takes a number as
and prints how many digits are in input from the user and tells if it is
that number. a perfect number or not.
Example: If the user gives 9876, your program Perfect Number: An integer number is said to
should print 4. be a perfect number if its factors,
including 1 but not the number itself, sum to
the number.
Sample Input 1:
6
Sample Output 2:
6 is a perfect number
Explanation:
6 has 4 divisors: 1, 2, 3, and 6.
If we add all factors except 6 itself, 1 + 2 + 3 =
6.
Write a C program that takes n as input. The Print the sum of the given sequence:
next n lines will take an integer as input. Find Sample input: 5
out the maximum, minimum and average of Sample Output:
the n numbers. 1^2-2^2+3^2-4^2+5^2 = 15
You cannot use arrays.
If the user enters 5 as an input for quantity
and the enters the 5 numbers, 10, 4, -1,
-100, and 1.
The output of your program should be:
Maximum 10
Minimum -100
Average is -17.2
Practice Problems on “Array”
Problem 1: Floor, Ceil within an array
15 -1 21
21 21 21
30 21 34
96 96 96
98 96 -1
Problem 2: Print all possible pairs that have a common factor other than 1
A [ ] = {5, 3, 15, 4, 2}
(5, 15), (15,5), (2,4), (4,2) (3,15), (15, 3)
Input:
12312
Output: 1 2 3
A peak element in an array is an element that is not smaller than its neighbors.
● Output: [4, 3, 2, 1]
You're given an array of n-1 integers in the range 1 to n. There are no duplicates, and exactly
one number is missing.
● Output: 3
Practice Problems on “Strings”, “Functions”
1. Given 3 strings s1, s2, s3, construct a 4th string with appropriate size that will be generated by
replacing all occurrences of substring s2 by the substring s3 in the string s1. Also report how many times
the replacement occurred. No library functions should be used.
Sample Input:
s1 = "I scream, you scream, we all scream for ice cream!"
s2 = "scream"
s3 = "shout"
Sample Output:
Resulting string: I shout, you shout, we all shout for ice cream!
Number of replacements: 3
2. Given two strings s1 and s2, check if s2 is a rotation of s1. For example, "abcde" and "cdeab"
are rotations.
Sample Input:
abcde
bcdea
Sample Output:
“YES”
Sample Input:
abcde
bdcea
Sample Output:
“NO”
3. Implement a basic string compression using the counts of repeated characters. For example,
"aabcccccaaa" becomes "a2b1c5a3". If the compressed string is not shorter than the original,
return the original.
Input:
Output:
Sample Input:
abbbbcdd
Sample Output:
abbbbcdd (same as the input because a1b4c1d2 has the same length)
4.Given two strings, determine whether they are anagrams of each other.
Two strings are anagrams if they contain the same characters with the same frequency, regardless of
order.
Input 1:
listen
silent
Output1:
Yes
Input 2:
listen
lessen
Output 2:
No
5. Given a sentence, identify the longest word in it. If there are multiple, return the first longest one.
Input: The quick brown fox jumps over the lazy dog
Output: quick
5. Selection sort
6. Insertion sort
7. Bubble sort
Practice Problems on “Pointer”
1. Write a function called void compressString(char *str) to compress a string using
counts of repeated characters. The function should print the compressed string if it is shorter
than the original; otherwise, it should print the original string. The output should be printed
within the function.
Finally, write a main function to:
● Take a string input from the user.
● Call the compressString function.
Note:
● You may use library functions as needed to achieve the task.
● Do not use array syntax.
● Do not use nested loops.
Examples
1. Input: aabcccccaaa
Output: a2b1c5a3
2. Input: abc
Output: abc
2. Write a function that takes two integer pointers as parameters and swap their values using
pointers. Your function should not return anything.
3. Write a function that takes an integer pointer as a parameter and finds the factorial of a given
number using pointers. It takes another integer pointer as a parameter and stores the factorial in
it.
Function Skeleton:
4. Write a function that takes a source string pointer and a destination string pointer as parameters
and copies the source string to the destination string using pointers.
● Take an integer as input indicating row/column of a square matrix and then take all the
elements of the matrix as input. Write a program in C to find the sum of the upper
triangular elements of the matrix.
Constraint:
1. Use dynamic memory allocation
2. NO USE OF [] operator.
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
Sample I/O:
Input Output
3 11
123
456
789
5 38
21341
23561
24519
02417
94032
● Take two integers as input indicating row and column of a binary matrix and then take all
the elements of the matrix as input. Write a program in C to return only the unique rows
from the given binary matrix.
Sample I/O:
Input Output
45 01001
01001 10110
10110 10100
01001
10100
56 011001
011001 101110
101110 101101
101101
011001
101101
●
Sample Input All Box Returned Array Explanation
● As a child, you often played this game while walking on a tiled floor. You
walked avoiding a certain color, for example white tiles (it almost felt like
if you stepped on a white tile, you would die!). Now you are in a room of
m x n dimension. The room has m*n black and white tiles. You step on
the black tiles only. Your movement is like this:
Now suppose you are given a 2D array which resembles the tiled floor.
Each tile has a number. Can you write a method that will print your
walking sequence on the floor?
Constraint: The first tile of the room is always black.
Hint: Look out for the number of rows in the matrix [Notice the
transition from column 0 to column 1 in the above figures]
-------------------- 391
3 | 8| | 4 | 6 | 1 | 12
-------------------- 472
|7|2|1|9|3| 49
-------------------- 186
|9|0|7|5|8|
--------------------
|2|1|3|4|0|
--------------------
|1|4|2|8|6|
--------------------
Sample Input Sample Output
-------------------- 39
|3|8|4|6|1| 12
-------------------- 47
|7|2|1|9|3| 49
-------------------- 18
|9|0|7|5|8|
--------------------
|2|1|3|4|0|
--------------------
● Take two matrices input from the user. [You know how to take them, right? Already done before]
Write a program in C to return the multiplication of two matrices.
Practice Problems on “Bitwise Operator”
● Take an integer as input and determine whether it is “even” or “odd”.
● Take an integer/char as input and determine whether it is “positive” or
“negative”.
● Take an integer/char as input and print the binary representation of that integer.
● Swap the content of two integers/characters using bitwise operation
● Take two integers r and m. Left rotate the bits of m by r bits.
● Take two integers r and m. Right rotate the bits of m by r bits.
● Take two integers n and var. Extract rightmost n bits of var and assign it to var.
All other bits of var except the rightmost n bits will be set to zero.
Practice Problems on “Structure”
Concepts: Structure, Array of structures, Nested structures
1. Create a structure named Student with the fields name, marks, and roll. Then, implement a
function that calculates the grade of a student based on their marks, using the structure as a
parameter. Additionally, create another function to display the details of a student. Write a third
function that identifies and displays the student with the highest marks.
In the main function, first prompt the user to enter the number of students. Then, create an
array of Student structures and gather the details for each student (name, marks, and roll
number). Afterward, calculate and display the grade for each student. Finally, print the student
details who got the highest marks using the third function.
2. Enter the marks of n students in Chemistry, Mathematics and Physics (each out of 100)
using a structure named Marks having elements/members/fields roll no., name,
chem_marks, math_marks and phy_marks and then display the overall percentage of
marks for each student.
3. Write a structure to store the roll no., name, age (between 11 to 14) and address of n
students. Store the information of the students.
a. Write a function to print the names of all the students having age 14.
b. Write another function to print the names of all the students having even roll no.
c. Write another function to display the details of the student whose roll no is taken
from input (i.e. roll no. entered by the user).
4. Write a structure to store the name, account number and balance of n customers and store
their information.
a. Write a function to print the names of all the customers having balances less than
$200.
b. Write a function to add $100 in the balance of all the customers having more than
$1000 in their balance and then print the incremented value of their balance.
5. Write a structure to store the x, y coordinates of a point. Then, write another structure that
stores the two endpoints of a diagonal of a rectangle (For simplicity, you can assume that
the sides of the rectangle are aligned with x and y axis). Then do the following:
2. Create a structure named 'RGBColor' to represent colors using red, green, and blue
components. Each component should be represented by an integer ranging from 0 to 255.
Implement the structure in such a way that it minimizes memory usage using bitfields,
ensuring that no additional bits are wasted.
1. Write a program in C to create and store information in a text file named with your
student ID. The file content should be the following:
This is the first line
This is the second line
Use both fprintf and fputs functions for writing to the file.
2. Now, use both fscanf and fgets function to print the content of the same file to your
console.
3. Now, write a new program to open the same file and append more content to the same
file. The file content should be the following:
This is the first line
This is the second line
This is the third line
This is the fourth line
You should only write the last two lines in the file for this problem.
5. Write a program in C to merge two files and write them to another file.