Week-7 solutions
Week-7 solutions
#include <stdio.h>
int main() {
int a, b,i,j,temp;
//printf("Enter values of a and b:");
scanf("%d %d",&a,&b);
// Bitwise AND
int andResult=0;
for(i=1;i<=a;i++)
{
for(j=i+1;j<=a;j++)
{
temp = i & j;
if ((temp<b) && (temp>=andResult))
{
andResult=temp;
}
}
}
printf("%d\n", andResult);
// Bitwise OR
int orResult=0;
for(i=1;i<=a;i++)
{
for(j=i+1;j<=a;j++)
{
temp = i | j;
if ((temp<b) && (temp>=orResult))
{
orResult=temp;
}
}
}
printf("%d\n", orResult);
// Bitwise XOR
int xorResult=0;
for(i=1;i<=a;i++)
{
for(j=i+1;j<=a;j++)
{
temp = i ^ j;
if ((temp<b) && (temp>=xorResult))
{
xorResult=temp;
}
}
}
printf("%d\n", xorResult);
return 0;
}
#include <stdio.h>
bool isPowerOfThree(int n) {
if (n <= 0)
return 0;
while (n % 3 == 0) {
n /= 3;
}
if (n==1)
return 1;
else
return 0;
}
3. You are climbing a staircase. It takes n steps to reach the top. Each time you can either climb 1
or 2 steps. In how many distinct ways can you climb to the top?
if (n <= 0) {
return 0;
if (n == 1) {
return 1;
int a = 1, b = 1, c;
c = a + b;
a = b;
b = c;
}
return b;
Post lab:
bool isPowerOfFour(int n) {
if (n <= 0)
return 0;
while (n % 4 == 0) {
n /= 4;
if (n==1)
return 1;
else
return 0;
Input The first line of the input contains a single integer T denoting the number of test cases. The
description of T test cases follows. The first and only line of each test case contains a single integer
N. Output for each test case print a single line containing a single integer N!
#include <stdio.h>
if (n == 0 || n == 1) {
return 1;
} else {
int main() {
int n,T;
scanf("%d", &T);
while(T--)
scanf("%d", &n);
return 0;
3. The Fibonacci numbers, commonly denoted F(n) form a sequence, called the Fibonacci
sequence, such that each number is the sum of the two preceding ones, starting from 0 and 1
Recurssion:
if (n <= 1) {
return n;
} else {
4. Kristen loves playing with and comparing numbers. She thinks that if she takes two different
positive numbers, the one whose digits sum to a larger number is better than the other. If the
sum of digits is equal for both numbers, then she thinks the smaller number is better. For
example, Kristen thinks that 13 is better than 31 and that12 is better than 11. Given an integer,
n, can you find the divisor of n that Kristin will consider to be the best?
#include <stdio.h>
int sum = 0;
num /= 10;
return sum;
}
int findBestDivisor(int n) {
int i,bestDivisor = 1;
if (n % i == 0) {
bestDivisor = i;
maxSum = currentSum;
return bestDivisor;
int main() {
int n;
scanf("%d", &n);
if (n <= 0) {
return 1;
printf("%d\n", bestDivisor);
return 0;
Skill lab:
1. A perfect number is a positive integer that is equal to the sum of its positive divisors,
excluding the number itself. A divisor of an integer x is an integer that can divide x
evenly. Given an integer n, return true if n is a perfect number, otherwise return false.
bool checkPerfectNumber(int num){
int sum = 1; // Initialize sum with 1 since every number is divisible by 1
if (num==1)
return 0;
for (int i = 2; i * i <= num; i++) {
if (num % i == 0) {
sum += i;
// If i and n/i are not equal, add n/i to the sum
if (i != num / i) {
sum += num / i;
}
}
}
return sum == num;
}
2. Given an integer num, repeatedly add all its digits until the result has only one digit, and
return it.
int addDigits(int num){
while (num >= 10) {
int sum = 0;
while (num > 0) {
sum += num % 10;
num /= 10;
}
num = sum;
}
return num;
}
Total money spent = Rs. 60 (initial purchase) + Rs. 80 (buying it back) = Rs. 140
Total money earned = Rs. 70 (selling to her friend) + Rs. 90 (auction) = Rs. 160
Net profit = Total money earned - Total money spent = Rs. 160 - Rs. 140 = Rs. 20
#include <stdio.h>
int main() {
int initial_cost = 60;
int selling_price_1 = 70;
int buying_back_price = 80;
int selling_price_2 = 90;
return 0;
}
4. Given a signed 32-bit integer x, return x with its digits reversed. If reversing x causes the
value to go outside the signed 32-bit integer range [-231, 231 - 1], then return 0.
int reverse(int x) {
int result = 0;
while (x != 0) {
int digit = x % 10;
5. Given an integer array nums, move all 0's to the end of it while maintaining the relative
order of the non-zero elements.
Example 1:
Example 2:
6. Write a function that takes the binary representation of an unsigned integer and returns
the number of '1' bits it has (also known as the Hamming weight).
int hammingWeight(uint32_t n) {
int count = 0;
while (n) {
count += n & 1;
n >>= 1;
}
return count;
}
7. Martha is interviewing at Subway. One of the rounds of the interview requires her to cut a
bread of size l X b into smaller identical pieces such that each piece is a square having
maximum possible side length with no leftover piece of bread.
#include <stdio.h>
int gcd(int a, int b) {
while (b != 0) {
int temp = b;
b = a % b;
a = temp;
}
return a;
}
printf("%d\n", num_pieces);
int main() {
int l, b,T;
scanf("%d", &T);
while(T--)
{
scanf("%d %d", &l, &b);
cutBread(l, b);
}
return 0;
}
8. Given N two-dimensional points in space, determine whether they lie on some vertical or
horizontal line.
If yes, print YES; otherwise, print NO.
#include <stdio.h>
int main() {
int N;
//printf("Enter the number of points: ");
scanf("%d", &N);
if (vertical || horizontal) {
printf("YES\n");
} else {
printf("NO\n");
}
return 0;
}
PREDICT THE OUTPUT
#include <stdio.h>
void foo(int n) {
if (n > 0) {
printf("%d ", n);
foo(n - 1);
printf("%d ", n);
}
}
int main() {
foo(3);
return 0;
}
1. What is the output of the above program?
a) 3 2 1 1 2 3
b) 3 2 1
c) 1 2 3
d) 1 2 3 3 2 1
#include <stdio.h>
int bar(int n) {
if (n <= 0) {
return 0;
} else {
return n + bar(n - 2);
}
}
int main() {
int result = bar(7);
printf("%d", result);
return 0;
}
2.What is the output of the above program?
a) 20
b) 16
c) 14
d) 12
#include <stdio.h>
void baz(int n) {
if (n > 0) {
baz(n / 2);
printf("%d ", n % 2);
}
}
int main() {
baz(10);
return 0;
}
3.What is the output of the above program?
a) 1 0 1 0
b) 0 1 0 1
c) 0 0 1 0 1
d) 1 1 0 1
#include <stdio.h>
int factorial(int n) {
if (n == 0) {
return 1;
} else {
return n * factorial(n - 1);
}
int main() {
int result = factorial(5);
printf("%d", result);
return 0;
}
4.What is the output of the above program?
a) 120
b) 24
c) 25
d) 20
#include <stdio.h>
int power(int base, int exponent) {
if (exponent == 0) {
return 1;
} else {
return base * power(base, exponent - 1);
}
}
int main() {
int result = power(2, 4);
printf("%d", result);
return 0;
}
5.What is the output of the above program?
a) 16
b) 8
c) 32
d) 64