First Year Practice Questions
First Year Practice Questions
Sol :
bool isPalindrome(int x) {
if (x < 0) return false;
int temp = x;
int rev = 0;
while (x > 0) {
int digit = x%10;
rev = rev*10 + digit;
x /= 10;
}
if (rev == temp) {
return true;
}
return false;
}
Q2. The n-th term of Fibonacci series F(n), where F(n) is a func on, is calculated using
the following formula -
F(n) = F(n - 1) + F(n - 2),
Where, F(0) = 0, F(1) = 1
Provided 'n' you have to find out the n-th Fibonacci Number.
Example :
Input: 6
Output: 8
Sol :
int fib(int n) {
int prev = 0, prev2 = 1;
for (int i = 2; i <= n; i++) {
int curr = prev2 + prev;
prev2 = prev;
prev = curr;
}
return prev;
}
Q3. Ninja was very fond of pa erns. For a given integer ‘N’, he wants to make the N-
Star Rotated Triangle.
Example:
Input: ‘N’ = 3
Output:
*
**
***
**
*
Sol :
void nStarTriangle(int n) {
for (int i = 1; i <= n; i++) {
for (int k = 0; k < i; k++) {
printf("*");
}
printf("\n");
}
Sol :
void nNumberTriangle(int n) {
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n - i + 1; j++) {
printf("%d ", j);
}
printf("\n");
}
}
Q5. Aryan and his friends are very fond of pa erns. For a given integer ‘N’, they want
to make the Increasing Le er Triangle.
Example:
Input: ‘N’ = 3
Output:
A
AB
ABC
Sol :
void nLetterTriangle(int n) {
for (int i = 0; i < n; i++) {
char ch = 'A';
for (int j = 1; j <= i + 1; j++) {
printf("%c ", ch);
ch++;
}
printf("\n");
}
}
Q6. Given a number n. Count the number of digits in n which evenly divide n. Return
an integer, total number of digits of n which divides n evenly.
Examples :
Input: n = 12
Output: 2
Explana on: Both 1 and 2 divide 12 completely, hence ans is 2.
-----------------------------------------------------------
Input: n = 2446
Output: 1
Explana on: Only 2 divides 2446 completely, hence ans is 1.
-----------------------------------------------------------
Sol :
int evenlyDivides(int N) {
int temp = N;
int cnt = 0;
while (N != 0) {
int digit = N % 10;
if (digit != 0 && temp % digit == 0) {
cnt++;
}
N /= 10;
}
return cnt;
Q7. Given a number n. For all integers i from 1 to N, Perform the following:
If i is Divisible by 3 print “Fizz”
If i is divisible by 5 print “Buzz”
If i is divisible by both 3 and 5 print “FizzBuzz”
If i is neither divisible by 3 nor 5 print i
Example :
Input: n = 5
Output:
1
2
Fizz
4
Buzz
-----------------------------------------------------------
Sol :
#include <stdio.h>
int main()
{
int n;
scanf("%d", &n);
for (int i = 1; i <= n; i++)
{
if (i % 15 == 0) { printf("FizzBuzz\n"); }
else if (i % 5 == 0) { printf("Buzz\n");}
else if (i % 3 == 0) { printf("Fizz\n");}
else { printf("%d\n", i);}
}
return 0;
}
Note:
Do not use Print Statements like “Enter the number” or “The Output is: ” in online
contests, Strictly follow the Input and Output format, adding such statements can
lead to incorrect output despite correct solu on.
Q8. In a mathema cal language developed by Graphians, There is only one Operand X
there are only two operators ++ and --, the former increments the value of X by 1 and
the la er decrements the value of X by 1, addi onally, each statement can be wri en
in mul ple ways. Eg: ++X, X++ are the same expressions.
Let the ini al value of X be 0, Given N, the number of such statements followed by N
statements, compute the final Value of X.
Assume all the statements will be valid. (No statement of the form +X- will be given).
Example :
Input 1:
2
X++
--X
Output: 0
-----------------------------------------------------------
Input 2:
3
++X
--X
--X
Output: -1
-----------------------------------------------------------
Input 3:
5
++X
--X
++X
--X
--X
Output: -1
Sol :
#include <stdio.h>
int main()
{
int n;
scanf("%d", &n);
char c1, c2, c3;
int X = 0;
for (int i = 0; i < n; i++)
{
getchar(); //To read ‘\n’ from previous line buffer
scanf("%c%c%c", &c1, &c2, &c3);
if (c1 == '+' || c2 == '+' || c3 == '+')
{
X++;
}
else
{
X--;
}
}
printf("%d", X);
return 0;
}
Note:
Do not use Print Statements like “Enter the number” or “The Output is: ” in online
contests, Strictly follow the Input and Output format, adding such statements can
lead to incorrect output despite correct solu on.
Q9. Rahul loves math, but due to Extra curricular ac vi es, He was not able to a end
classes and now his a endance is below 75%, He is only allowed to sit for the exam if
he can find the solu on to this problem given by his professor, Rahul seeks your help
to sit for the exam.
Given integers N and M, count how many pairs of integers (a, b) (0 ≤ a, b) which sa sfy
the following system of equa ons:
A2+B = N
A+B2 =M
Example :
Input 1:
9 3
Output: 1
-----------------------------------------------------------
Input 2:
14 28
Output: 1
-----------------------------------------------------------
HINT:
How much is the maximum value A and B can go upto?
In EQN1 A2 + B = N, put A =0, B can go upto N;
Similarly A can go Upto M.
Now check all pairs and take the count of valid pairs.
-----------------------------------------------------------
Sol :
#include <stdio.h>
int main()
{
int n, m, count = 0;
scanf("%d %d", &n, &m);
printf("%d", count);
return 0;
}
Note:
Do not use Print Statements like “Enter the number” or “The Output is: ” in online
contests, Strictly follow the Input and Output format, adding such statements can lead
to incorrect output despite correct solu on.
Q10. Manu is fond of reading books, Today is Sunday and he just got a New book and
decided to start reading it from the next day, that is, from Monday. Since Manu is now
in college, he has a very ght schedule and for each day of the week he knows how
many pages he will be able to read on that day. Some days are so busy that Manu will
have no me to read whatsoever.
However, we know that he will be able to read at least one page a week.
Manu promises that he will not skip days and will read as much as he can every day,
determine on which day of the week he will read the last page of the book.
Assume Monday =1, Tuesday=2 ….. , Sunday=7.
You given an integer N, N (1 ≤ N ≤ 1000) the number of pages in the book followed by
seven non nega ve integers , the number of pages Manu can Read on that day of the
week.
Example :
Input 1:
100
15 20 20 15 10 30 45
Output: 6
-----------------------------------------------------------
Input 2:
2
1 0 0 0 0 0 0
Output: 1
----------------------------------------------------------
Input 3:
77
11 11 11 11 11 11 10
Output: 1
---------------------------------------------------------
Sol :
#include<stdio.h>
int main()
{
int n;
scanf("%d", &n);
int arr[7];
while(total < n)
{
for(int i = 0; i < 7; i++)
{
total += arr[i];
if(total >= n)
{
ans = i + 1;
break;
}
}
}
printf("%d", ans);
return 0;
}
Note:
Do not use Print Statements like “Enter the number” or “The Output is: ” in online
contests, Strictly follow the Input and Output format, adding such statements can lead
to incorrect output despite correct solu on.