# Solutions: CsCode Programming Contest 3

Contest pattern: 2 Programming Questions and 5 MCQ’s.
Contest was organised by @Computer_Science_Engineering .

1)

What will be the output of above C program?

Choices

• 123
• 1
• 32123

2)

Above code defines which concept?

Choices

• Iteration
• Simple function call
• Conditional Statement

3)

What is the time complexity of code below?

Choices

• O(n)
• O(200)
• O(n-200)

Check this post for explanation

4)

Tailed recursive functions are slower than non-tailed recursive functions.

Above statement is correct or incorrect?

Choices

• Correct

Check following post for explanation.

5)

Choose the correct output

Choices

• 4
• 3
• 2

Hint:

## Solutions of Programming Questions

### 1) Perfect Number

Problem statement

Write a program to check whether the given number is a perfect number or not. The program should print 1 if the number is a perfect number, else it should print 0.

Hint: Perfect number is a positive whole number that is equal to the sum of its proper divisors.
The first perfect number is 6 as the sum of its proper positive divisors, 1,2 and 3 is 6. Other perfect numbers are 28, 496 and so on.

Input Format:

First line of input contains T test cases.

For each test case,

a positive integer on new line

Output:

Display 1 if number is perfect else 0 for each numbes on new line.

Constraints:

``````1 <= T <= 1000
1<= Number <= 100000``````

Solution:

```def check_perfect_number(number):
if number == 1:
return False
sum_ = 1
for i in range(2, number//2+2):
if number%i == 0:
sum_ += i
if sum_ == number:
return True
return False

for _ in range(int(input())):
number = int(input())
if check_perfect_number(number):
print(1)
else:
print(0)```
```def isPerfect(n):
s = 1
i = 2
while i*i <= n:
if(n % i ==0):
if n//i == i:
s = s+i
else:
s = s + i + n//i
i += 1
return (1 if s == n and n!=1 else 0)

for i in range(int(input())):
n = int(input())
if isPerfect(n):
print(1)
else:
print(0)```
```// By kamesh upmanyu

#include<bits/stdc++.h>
using namespace std;
bool check(int n)
{ if(n==1||n==0)
return 0;
int res=0;
for(int i=2;i<=sqrt(n);i++)
{
if(n%i==0)
{
if(i==(n/i))
res+=i;
else
res+=(i+(n/i));
}
}
if(res+1==n)
return 1;
return 0;
}
int main()
{
int t,n;
cin>>t;
while(t--)
{
cin>>n;
cout<<check(n)<<"\n";
}
}```

### 2) Special Sequence – Count Numbers

Problem statement

The following sequence is formed using words and numbers:

• First number is 1
• In the first number, there was one 1 so the second number is 11.
• In the second number, there was two 1s, so the third number is 21
• Like that the fourth number is 1211 (one 2 and one 1)
• The next number is 111221 (one 1, one 2, two 1’s).

This sequence can continue infinitely. Given an integer, q[i], determine the sum of digits of the value in the sequence at that position. For example, position 4 contains the value 1211. The sum of those digits is 1+2+1+1 = 5.

Constraints:

``````1 <= n <= 1000
1 <= q[i]  <= 54``````

Input Format:

The first line contains an integer n (total number of queries).

Next n lines contains an integer q[i].

Output Format:

n  lines denoting sum of digits of sequence at q[i]

Sample Input:

``````2
2
4``````

Sample Output:

``````2
5``````

Explanation:

The sequence is 1, 11, 21, 1211, 111221 and so on.

The sum of digits of element at 2nd(11) and 4th(1211) position in the sequence are 2, 5.

Solution:

```sumOfDigits = [0 for _ in range(56)]
sumOfDigits = 1

firstNumber = '11'

for i in range(1, 55):
sum_ = 0
for no in firstNumber:
sum_ += int(no)
sumOfDigits[i+1] = sum_

nextNumber = ''

j=1

count = 1
for j in range(1, len(firstNumber)):
if firstNumber[j] == firstNumber[j-1]:
count += 1
else:
nextNumber += str(count)+firstNumber[j-1]
count = 1
nextNumber += str(count)+firstNumber[-1]

firstNumber = nextNumber

#input
for _ in range(int(input())):
print(sumOfDigits[int(input())])```
```//By kamesh upmanyu

#include<bits/stdc++.h>
using namespace std;
string v;;
void create()
{

v=to_string(1);

for(int i=2;i<=54;i++)
{
string str=v[i-1];
string res="";
for(int j=0;j<str.length();j++)
{
int c=1;
while(str[j]==str[j+1])
{ c++;j++;}
res+=to_string(c)+str[j];
}
v[i]=res;
}

}
int main()
{    create();
int t,n;
cin>>t;
while(t--){
int s=0;
cin>>n;
string str=v[n];
for(int i=0;i<str.length();i++)
{
s+=str[i]-'0';
}
cout<<s<<"\n";
}

}```

If you have any doubts, ask us on Instagram or comment below. 