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
Solution: b.
#include<stdio.h>
int main ()
{int a,b,c;
scanf("%d %d %d",&a, &b, & c);
// a=c, b=a, c=b
a=a+b+c;
b=a-b-c;
c=a-b-c;
a=a-b-c;
printf("%d %d %d",a,b,c);
}
int main ()
{
int len;
printf("Enter the no of digits of number and then the number itself\n");
scanf("%d", &len);
int num ;
scanf("%d", & num);
int digits[len];
for (int i=0;i<len;i++)
{digits[i]=num%10;
num=num/10;
}
for (int i=0;i<len;i++)
{printf("%d", digits[i]); }
}
b.
#include<stdio.h>
int main ()
{
int num;
scanf("%d", & num);
int reverse=(num%10)*1000+ ((num/10)%10)*100+
(((num/10)/10)%10)*10+(((num/10)/10)/10)%10;
printf("%d", reverse);
}
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.
Solution: 3 & 4
#include<stdio.h>
int main ()
{ int select;
char newc;
char c;
scanf("%d", & select);
scanf(" %c", & c);
if(c<65 || (c>90 && c<97)|| c>122) {printf("Invalid character\n");
return 0 ;
}
int cipher ;
scanf("%d", & cipher);
if (select==1){
if (c>=65 && c<=90){newc= ((c+cipher-65)%26)+65; }
else if (c>=97 && c<=122){newc= ((c+cipher-97)%26)+97; }
}
if (select==2){
if (c>=65 && c<=90){newc= ((c-cipher-65+26)%26)+65; }
else if (c>=97 && c<=122){newc= ((c-cipher-97+26)%26)+97; }
printf("%c", newc);
}
Solution:
#include<stdio.h>
int main ()
{ int len;
int num ;
}
printf("%d", second);
#include<stdio.h>
int main ()
{
int num ;
int digit1=num%10;
num=num/10;
int digit2=num%10;
num=num/10;
int digit3=num%10;
num=num/10;
int digit4=num%10;
int temp;
int first=digit1;
int second=digit2;
if (first<second){ temp=first;
first=second;
second=temp;
}
if (digit3>first){second=first;
first=digit3;
}
else if (digit3>second && digit3!=first){second=digit3; }
if (digit4>first){second=first;
first=digit4;
}
else if (digit4>second && digit4!=first){second=digit4; }
printf("%d", second);
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: 30 10 ‘*’
Output: 300
Input: 40 6 ‘/’
Output: 6.67
int main ()
{ int year;
scanf("%d", & year);
if ((year%400==0)|| (year%4==0 && year%100!=0)){printf("Leap year\n");}
else printf("Not a leap year");
}
4. Given the marks of a student and the grading policy, print his/her obtained grade.
5. Given a number, check if it is divisible by 2 or 5.
6. Given a number, check if it is divisible by 2 or 5 but not both.
Solution:
#include<stdio.h>
int main ()
{ int num;
scanf("%d", & num);
if ((num%2==0 || num%5==0) && num%10!=0 ){printf("Correct\n");
} else {printf("InCorrect\n");}
}
7. Find out the maximum of three numbers.
Solution:
#include<stdio.h>
int main ()
{ int num[3];
for (int i=0;i<3;i++)
{scanf("%d", & num[i]);}
int first=0 ;
for (int i=0;i<3;i++)
{if(num[i]>first) {first=num[i]; } }
printf("%d", first);
}
Alternative without for loop and array
#include<stdio.h>
int main ()
{
int a,b,c;
scanf("%d %d %d", & a, &b, &c);
if (a>b){
if (a>c){printf("%d is the largest\n",a);}
else if (c>a) {printf("%d is the largest\n",c);}}
else {
if (b>c) {printf("%d is the largest\n",b);}
else {printf("%d is the largest\n",c);}
}
}
Alternative:
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
int main() {
int a, b, c;
scanf("%d %d %d", & a, & b, &c);
int first = a;
if (b > first) { first = b; }
if (c > first) { first = c; }
printf("%d", first);
}
#include<stdio.h>
int main ()
{
int a,b,c;
scanf("%d %d %d", & a, &b, &c);
int first=a;
int second=b;
if(first<second){int temp=first;
first=second;
second=temp;
}
if (c>first){second=first;
first=c;
}
else if(c>second && c!=first){second=c; }
printf("%d", second);
}
9. Given three lengths, find out if a triangle can be formed. If it can be, then print if the
triangle is isosceles or equilateral or scalene.
Solution:
#include<stdio.h>
int main ()
{ float a,b,c ;
scanf("%f %f %f", & a, & b, & c);
if((a+b>c) &&(b+c>a) && (c+a>b)) {printf("Triangle can be formed\n");
if ((a==b) && (b==c)) {printf("equi\n");}
else if((a==b) || (b==c) || (c==a)) {printf("Isos\n");}
else if((a!=b) && (b!=c) && (c!=a)) {printf("Scalene\n");}
}
else {printf("It cannot be formed\n");}
return 0 ;
}
#include <stdio.h>
#include <math.h>
int main ()
{ int n, i ;
scanf("%d", & n);
if (n==0 || n==1){printf("Not prime");
return 0;
}
for (i=2;i<n;i++) // (i=2;i<=sqrt(n);i++)
{if (n%i==0)
{break;}
}
if (i==n){printf("Prime");}
if (i!=n){printf("Not Prime");}
}
4. Print the first 1000 prime numbers
Solution:
For numbers for 1-1000 prime numbers:
#include <stdio.h>
#include <math.h>
int primecheck (int num)
{int p, check ;
if (num==0 || num==1){check=0;
}
for (p=2;p<num;p++)
{if (num%p==0)
{break;}
}
if (p==num){check=1;}
if (p!=num){check=0;}
return check;
}
int main ()
{ int i;
for (i=2;i<=1000;i++)
{ if (primecheck(i)==1){printf("%d \n", i);}
}
}
#include <stdio.h>
#include <math.h>
int main ()
while (count<1000)
{ primecheck=1;
for (p=2;p<num;p++)
{if (num%p==0)
{primecheck=0;
break;
if (primecheck==1){printf("%d\n", num);
count++;
num++; }
****
***
**
Solution:
#include <stdio.h>
#include <math.h>
int main ()
{ int row;
printf("Please enter the number of rows\n");
scanf("%d", & row);
int i,p ;
for (i=0;i<4;i++)
{
for (p=0;p<(row-i);p++)
{printf("* ");}
printf("\n");
}
}
6. Write a C program to print the following pyramid pattern using nested loops:
***
*****
*******
*********
Solution:
#include <stdio.h>
#include <math.h>
int main ()
int row;
int i,j;
for (i=0;i<row;i++)
{ for (j=0;j<(2*row-1);j++)
printf("\n");
}
7. Write a C program to generate the Fibonacci series up to the nth term.
Solution:
#include <stdio.h>
#include <math.h>
int main ()
{ printf("Please enter the nth term\n");
int n;
scanf("%d", & n);
if (n==1){printf("0");
return 0 ;
}
if (n==2){printf("1");
return 0 ;
}
int first=0;
int second =1;
int next;
for (int i=0;i<n-2;i++)
{next=first+second;
first=second;
second=next;
}
printf("%d", next);
}
8. Write a C program to check whether a given number is a palindrome or not.
Solution:
#include <stdio.h>
int main()
int num ;
int org=num ;
int rev=0;
int d;
while(num>0)
{ d= num%10;
num=num/10 ;
rev=rev*10+d;
if (rev==org){printf("Palindrome");}
9. 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
Solution:
#include <stdio.h>
int main()
int sum=0;
int i;
for (i=63;i<=600;i++)
printf("%d", sum);
10. Write a C program which takes a 11. Write a C program that takes a
number and prints how many digits number as input from the user and
are in 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
Solution: the number.
#include <stdio.h> Sample Input 1:
6
int main() Sample Output 2:
{ 6 is a perfect number
int num ; Explanation:
scanf("%d", & num); 6 has 4 divisors: 1, 2, 3, and 6.
int d, count=0; If we add all factors except 6 itself, 1 + 2 + 3 =
if (num==0){printf("1"); 6 (six).
return 0; Solution:
} #include <stdio.h>
while (num>0) int main()
{d=num%10; {
num=num/10; int num ;
count++; scanf("%d", & num);
} int i;
printf("%d", count); int sum=0;
} for (i=1; i<num;i++)
{if (num%i==0)
{sum=sum+i; }
}
if (sum==num){printf("Perfect
number\n");}
else {printf("Not a perfect number\n");}
}
12. Print the right triangle. 13. Print the Left Triangle.
Sample Input: 5 Sample Input : 5
Sample Output: Sample Output:
A 1
AB 12
ABC 123
ABCD 1234
ABCDE 12345
Solution:
Solution:
#include <stdio.h> #include <stdio.h>
int main()
int main() {
{ int row;
int row; scanf("%d", & row);
scanf("%d", & row);
int i,j,p;
int i,j,p;
for (i=0;i<row;i++)
for (i=0;i<row;i++)
{for (j=0;j<row;j++)
{for (j=0;j<=i;j++)
if (j>=row-i-1){ {printf("%d ",j+1);}
p= i+j-row+1; printf("\n");
printf("%c ", (p%26)+65); }
} }
else {printf(" ");}
printf("\n");
}
14. Write a C program that takes n as 15. Print the sum of the given sequence:
input. The next n lines will take an Sample input: 5
integer as input. Find out the Sample Output:
maximum, minimum and average of 1^2-2^2+3^2-4^2+5^2 = 15
the n numbers. Solution:
You cannot use arrays. #include <stdio.h>
If the user enters 5 as an input for quantity #include<math.h>
and the enters the 5 numbers, 10, 4, -1, int main()
-100, and 1. {
The output of your program should be: int n ;
Maximum 10 scanf("%d", &n);
Minimum -100 double sum=0;
Average is -17.2 int i;
for (i=1;i<=n;i++)
Solution: { sum=sum+ (pow(-1,i+1))*i*i;
#include <stdio.h> }
int main() printf("%lf", sum);
{ }
int n;
scanf("%d", & n);
int i,d;
double sum=0;
int max=0 ;
int min=0;
for (i=0;i<n;i++)
{scanf("%d", &d);
sum=sum+d;
if(d>max){max=d; }
if(d<min){min=d; }
}
double avg=sum/n;
printf("avg= %lf\n", avg);
printf("max= %d\n", max);
printf("min= %d\n", min);
}
Solution:
#include <stdio.h>
int main()
int num;
scanf("%d", & num);
int i,j;
int count=0 ;
for (i=2;i<=num;i++)
if (num%i==0){
if (j==i){count++;}
printf("%d", count);
q=q*i;
term=(double) pow(-p,i)/q;
sum=sum+term;
i++;
}
printf("%.5lf", sum );
}
#include<stdio.h>
int main ()
{ int row;
scanf("%d", & row);
int i,j ;
for (i=0;i<row;i++)
{ for (j=0;j<row;j++)
{ if (i+j<=row-1)
{printf(" ");}
}
int num=1;
for (j=0;j<=i;j++)
{printf("%d",num);
num=num+2;
}
printf("\n");
}
}
19. Write code for the following pattern when row=5. Take row as an input.
*****
****
***
**
*
Solution:
#include<stdio.h>
int main ()
{ int row;
scanf("%d", & row);
int i,j ;
for (i=0;i<row;i++)
{
for (j=0;j<i;j++)
{printf(" ");}
for (j=0;j<row-i;j++)
{printf("*");}
printf("\n");
}
}
15 -1 21
21 21 21
30 21 34
96 96 96
98 96 -1
Solution:
#include<stdio.h>
int main ()
{ int n;
int f=-1,c=-1;
scanf("%d", & n);
int num;
{ if (array[p]<=num)
if (array[p]>=num)
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)
Solution:
#include<stdio.h>
int main ()
{ int n;
int f=-1,c=-1;
scanf("%d", & n);
int array [n];
for (int p=0;p<n;p++)
{ scanf("%d", & array[p]); }
for (int i=0;i<n;i++)
{
for (int p=0;p<n;p++)
{if (array[i]!=array[p])
{int x=array[i];
int y=array[p];
int temp;
while (y>0)
{ temp=y;
y=x%y;
x=temp; }
if (x>1)
{printf("(%d,%d)", array[i], array[p]) ; }
}
}}}
4. Given an array of integers nums and an integer target, return the indices of the two numbers that add
up to target.
Input: nums = [2, 7, 11, 15], target = 9
Output: [0, 1] (because 2 + 7 = 9).
Solution:
#include <stdio.h>
int main() {
int n;
scanf("%d", & n);
int array[n];
for (int i=0;i<n;i++)
{scanf("%d", & array[i]);}
int t;
scanf("%d", & t);
for (int i=0;i<n;i++)
{for (int j=0;j<n;j++)
{if (((array[i]+array[j])==t)&& i<=j) {printf("(%d,%d)", i,j);}
}
}
}
Alternative:
#include <stdio.h>
int main() {
int n;
scanf("%d", & n);
int array[n];
for (int i=0;i<n;i++)
{scanf("%d", & array[i]);}
int t;
scanf("%d", & t);
for (int i=0;i<n;i++)
{for (int j=i+1;j<n;j++)
{if (((array[i]+array[j])==t)) {printf("(%d,%d)", i,j);}
}
}
}
5. Remove duplicates from an array
Input:
5
12312
Output: 1 2 3
Solution:
#include <stdio.h>
int main() {
int n;
scanf("%d", & n);
int array[n];
for (int i=0;i<n;i++)
{scanf("%d", & array[i]);}
int found;
for (int i=0;i<n;i++)
{ found=0;
for (int j=0;j<i;j++)
{if (array[i]==array[j]) {found=1;
break;
}
}
if (found==0){printf("%d ", array[i]);}
}
}
6. Find a Peak Element in an Array.
A peak element in an array is an element that is not smaller than its neighbors.
Input: [10, 20, 15, 2, 23, 90, 67]
Output: 20 90 (both are peak elements)
Solution:
#include <stdio.h>
int main() {
int n;
scanf("%d", & n);
int array[n];
for (int i=0;i<n;i++)
{scanf("%d", & array[i]);}
• Output: [4, 3, 2, 1]
Solution:
#include <stdio.h>
int main() {
int n;
scanf("%d", & n);
int array[n];
int temp;
for (int i=0;i<n;i++)
{scanf("%d", & array[i]);}
for (int i=0;i<(n/2);i++)
{temp=array[i];
array[i]=array[n-1-i];
array[n-1-i]=temp;
}
for (int i=0;i<n;i++)
{printf("%d ", array[i]);}
}
• Output: 3
Solution:
#include <stdio.h>
int main() {
int n;
scanf("%d", & n);
int array[n-1];
int sum=0;
for (int i=0;i<n-1;i++)
{scanf("%d", & array[i]);
sum=sum+array[i]; }
int missing;
int summ=(n*(n+1))*.5;
missing=summ-sum;
printf("%d", missing); }
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. Take the size of an array and the elements of the array input from the user. Write a function that
takes the array as parameter and creates two arrays that contain even numbers and odd numbers. Print
the arrays inside the function.
Sample Input:
6
219635
Sample Output:
even: {2,6}
odd: {1,9,3,5}
3. Take the size of an array and the elements of the array input from the user. Write a function that
takes the array as parameter, prints the frequency of each unique element in the array and returns the
element with the highest frequency.
Sample Input:
8
36432441
Sample Output:
3 : 2 times
6 : 1 times
4 : 3 times
2 : 1 times
1 : 1 times
4
5. Selection sort
6. Insertion sort
7. Bubble sort
Practice Problems on “Pointer”
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]
Sample Input Sample Output
-------------------- 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|
--------------------
-------------------- 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:
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:
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.