Week - 6
Week - 6
Given an integer num, repeatedly add all its digits until the result has only one digit and
return it.
int addDigits(int num)
{
int sum,rem;
do{
sum=0;
while(num>0)
{
rem=num%10;
sum+=rem;
num/=10;
}
num=sum;
}while(sum>=10);
return sum;
}
In-Lab – 2
A happy number is a number defined by the following process: Starting with any positive
integer, replace the number by the sum of the squares of its digits. Repeat the process until the
number equals 1 (where it will stay), or it loops endlessly in a cycle which does not include 1.
Those numbers for which this process ends in 1 are happy. Return true if n is a happy number,
and false if not.
bool isHappy(int n)
{
int sum,digit;
while(1)
{
sum=0;
while(n>0)
{
digit=n%10;
sum=sum+digit*digit;
n=n/10;
}
if(sum==1 )
return true;
if(sum==89)
return false;
n=sum;
}
}
In-Lab – 3
An ugly number is a positive integer whose prime factors are limited to 2, 3, and 5.
bool isUgly(int n)
{
if (n <= 0) return false;
while(n > 1)
{
if (n % 2 == 0)
n = n / 2;
else if (n % 3 == 0)
n = n / 3;
else if (n % 5 == 0)
n = n / 5;
else
break;
}
return (n == 1);
}
Post-Lab - 1
You have n coins, and you want to build a staircase with these coins. The staircase consists of
k rows where the ith row has exactly i coins. The last row of the staircase may be incomplete.
int arrangeCoins(int n)
{
int rows=0;
while(n>=rows+1)
{
rows++;
n-=rows;
}
return rows;
}
Post-Lab – 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.
#include <stdio.h>
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int n;
scanf("%d",&n);
long int fact=1;
for(int i=1;i<=n;i++)
fact=fact*i;
printf("%ld ",fact);
}
return 0;
}
Skill - 1
Write a program using while loop to check if the positive number N is prime or not.
#include <stdio.h>
int main()
{
int num,i,count=0;
scanf("%d", &num);
for(i=1;i<=num;i++)
{
if(num%i == 0)
{
count++;
}
}
if(count == 2)
printf("YES");
else
printf("NO");
return 0;
}
Skill – 2
Write a program to find the factorial value of any number entered by the user.
#include <stdio.h>
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int num,i;
long int fact=1;
scanf("%d",&num);
for(i=1;i<=num;i++)
{
fact=fact*i;
}
printf("%ld\n",fact);
}
return 0;
}
Skill – 3
Write a program to find the factorial value of any number entered by the user.
#include <stdio.h>
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int n;
scanf("%d",&n);
int rows=0;
for(int i=1;n>=i;i++)
{
rows++;
n=n-i;
}
printf("%d\n",rows);
}
}
Skill – 4
There are N piles where the ith pile consists of Ai stones. Chef and Chefina are playing a game
taking alternate turns with Chef starting first. In his/her turn, a player can choose any non-
empty pile and remove exactly 1 stone from it. The game ends when exactly 1 pile becomes
empty. The player who made the last move wins. Determine the winner if both players play
optimally.
#include <stdio.h>
int main()
{
int T;
scanf("%d", &T);
while(T--)
{
int N, i, A[100000];
scanf("%d", &N);
for(i = 1; i <= N; i++)
{
scanf("%d", &A[i]);
}
int count = 0, count1 = 0;
for(i = 1; i <= N; i++)
{
if(A[i] % 2 != 0)
{
count++;
}
}
for(i = 1; i <= N; i++)
{
if(A[i] == 1)
{
count1++;
}
}
if(count % 2 == 0 && count1 == 0)
{
printf("Chefina \n");
}
else if(count % 2 != 0 && count1 == 0)
{
printf("Chef\n");
}
else if(count1 != 0)
{
printf("Chef\n");
}
}
return 0;
}
Skill – 5
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.
int fib(int n)
{
if (n == 0)
return 0;
if (n == 1)
return 1;
int a = 0, b = 1,c;
for (int i=2; i<=n; i++)
{
c = a + b;
a = b;
b = c;
}
return c;
}
Skill – 6
A self-dividing number is a number that is divisible by every digit it contains. For example,
128 is a self-dividing number because 128 % 1 == 0, 128 % 2 == 0, and 128 % 8 == 0.d
int isSelfDividing(int n)
{
int temp = n;
int digit;
while (n > 0)
{
digit = n % 10;
if (digit == 0 || temp % digit != 0)
{
return 0;
}
n /= 10;
}
return 1;
}
int* selfDividingNumbers(int left, int right, int* returnSize)
{
int* result = (int*)malloc((right - left + 1) * sizeof(int));
int count = 0;
for (int i = left; i <= right; i++)
{
if (isSelfDividing(i))
{
result[count] = i;
count++;
}
}
*returnSize = count;
return result;
}