0% found this document useful (0 votes)
6 views

Week-7 solutions

Uploaded by

Nithin kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Week-7 solutions

Uploaded by

Nithin kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 13

In Lab:

1. Solve the problem “Bitwise operators” in the Hacker Rank platform.

#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;
}

2. Given an integer n, return true if it is a power of three. Otherwise, return false.


An integer n is a power of three, if there exists an integer x such that n == 3x.

#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?

int climbStairs(int n){

if (n <= 0) {

return 0;

if (n == 1) {

return 1;

int a = 1, b = 1, c;

for (int i = 2; i <= n; i++) {

c = a + b;

a = b;

b = c;
}

return b;

Post lab:

1. Given an integer n, return true if it is a power of four. Otherwise, return false.

An integer n is a power of four, if there exists an integer x such that n == 4x.

bool isPowerOfFour(int n) {

if (n <= 0)

return 0;

while (n % 4 == 0) {

n /= 4;

if (n==1)

return 1;

else

return 0;

2. You are given an integer N. You need to print N! - the factorial of N.

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>

unsigned long long factorial(int n) {

if (n == 0 || n == 1) {

return 1;

} else {

return n * factorial(n - 1);

int main() {

int n,T;
scanf("%d", &T);

while(T--)

scanf("%d", &n);

unsigned long long result = factorial(n);

printf("%llu \n", result);

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:

int fib(int n){

if (n <= 1) {

return n;

} else {

return fib(n-1) + fib(n-2);

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 sumOfDigits(int num) {

int sum = 0;

while (num > 0) {

sum += num % 10;

num /= 10;

return sum;
}

int findBestDivisor(int n) {

int i,bestDivisor = 1;

int maxSum = sumOfDigits(1);

for (i = 2; i <= n; i++) {

if (n % i == 0) {

int currentSum = sumOfDigits(i);

if (currentSum > maxSum || (currentSum == maxSum && i < bestDivisor)) {

bestDivisor = i;

maxSum = currentSum;

return bestDivisor;

int main() {

int n;

//printf("Enter a positive integer: ");

scanf("%d", &n);

if (n <= 0) {

//printf("Please enter a positive integer.\n");

return 1;

int bestDivisor = findBestDivisor(n);

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;
}

3. Something for Profit


A friend of mine bought a used pressure cooker for Rs. 60. She somehow did not find it
useful and so when a friend of hers offered her Rs. 70 she sold it to her. However, she
felt bad after selling it and decided to buy it back from her friend' by offering her Rs. 80.
After having bought it once again she felt that she did not really need the cooker. So, she
sold it at the auction for Rs. 90. How much profit did she make? Did she at all make any
profit?

She bought the pressure cooker for Rs. 60.


She sold it to her friend for Rs. 70.
She bought it back from her friend for Rs. 80.
She sold it at an auction for Rs. 90.
Now, let's calculate the net profit:

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

So, she made a profit of Rs. 20 in this series of transactions.

#include <stdio.h>

int main() {
int initial_cost = 60;
int selling_price_1 = 70;
int buying_back_price = 80;
int selling_price_2 = 90;

int total_spent = initial_cost + buying_back_price;


int total_earned = selling_price_1 + selling_price_2;

int profit = total_earned - total_spent;

printf("Total money spent: Rs. %d\n", total_spent);


printf("Total money earned: Rs. %d\n", total_earned);
printf("Net profit: Rs. %d\n", profit);

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;

// Check for potential overflow


if (result > INT_MAX / 10 || (result == INT_MAX / 10 && digit > 7)) {
return 0;
}
if (result < INT_MIN / 10 || (result == INT_MIN / 10 && digit < -8)) {
return 0;
}

result = result * 10 + digit;


x /= 10;
}
return result;
}

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:

Input: nums = [0,1,0,3,12]


Output: [1,3,12,0,0]

Example 2:

Input: nums = [0]


Output: [0]

void moveZeroes(int* nums, int numsSize) {


int nonZeroIndex = 0;

for (int i = 0; i < numsSize; i++) {


if (nums[i] != 0) {
nums[nonZeroIndex++] = nums[i];
}
}

for (int i = nonZeroIndex; i < numsSize; i++) {


nums[i] = 0;
}

for (int i = 0; i < numsSize; i++) {


printf("%d ", nums[i]);
}
}

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;
}

void cutBread(int l, int b) {


int side_length = gcd(l, b);
int num_pieces = (l / side_length) * (b / side_length);

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);

int x[N], y[N];

//printf("Enter the coordinates of the points (x y):\n");


for (int i = 0; i < N; i++) {
scanf("%d %d", &x[i], &y[i]);
}

// Checking if all points have the same x-coordinate (vertical line)


int vertical = 1;
for (int i = 1; i < N; i++) {
if (x[i] != x[0]) {
vertical = 0;
break;
}
}

// Checking if all points have the same y-coordinate (horizontal line)


int horizontal = 1;
for (int i = 1; i < N; i++) {
if (y[i] != y[0]) {
horizontal = 0;
break;
}
}

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

You might also like