Week 3 - 1 Loop
Week 3 - 1 Loop
A.Ones(622)
At informatics lessons you have probably been taught to transform the numbers from one number system to
another, and to perform other similar operations. It's time to demonstrate this knowledge. Find the number of
ones in binary representation of a number.
Input
Output
#include <studio.h>
int main() {
int n;
scanf("%d", &n);
int count = 0;
while (n > 0) {
if (n % 2 == 1) {
count++;
}
n /= 2;
return 0;
Input
One 32-bit integer n (the number can be negative).
Output
Print the sum of the digits of the number n.
#include <stdio.h>
int main() {
int n;
scanf("%d", &n);
int sum = 0;
int num = abs(n); // Take the absolute value to handle negative numbers
sum += num % 10; // Extract the last digit and add it to the sum
}
printf("%d", sum);
return 0;
Input
One 32-bit number n that consists of at least two digits. The number n can be positive or negative.
Output
Print the sum of the first and the last digit of n.
#include <stdio.h>
nt main() {
int n;
scanf("%d", &n);
first_digit /= 10;
printf("%d", sum);
return 0;
}
Input
Output
#include <stdio.h>
#include <string.h>
int main() {
input[length - i - 1] = temp;
}
printf("%s", input);
return 0;
Output
Print the number of digits a in number n.
#include <stdio.h>
int main() {
int n, a;
n = abs(n);
if (n % 10 == a) {
count++;
n /= 10;
printf("%d", count);
return 0;
}
F.Divisibility by 11(2607)
Divisibility by $11$ is often stated as follows: add the digits standing separately at even and at odd positions.
If the difference of these sums is divisible by $11$, then the number is divisible by $11$. Calculate the
product of these sums.
Input
Output
#include <stdio.h>
int main() {
scanf("%llu", &n);
// Calculate the sums of digits at even and odd positions
while (n > 0) {
if (position % 2 == 0) {
sum_even += n % 10;
} else {
sum_odd += n % 10;
}
n /= 10;
position++;
printf("%llu", product);
return 0;
Input
One 64-bit integer containing at least one digits. The number can be negative.
Output
#include <stdlib.h>
int main() {
number = abs(number);
number /= 10;
printf("%lld", number);
return 0;
Input
Output
#include <stdio.h>
if (digitCount[digit] > 0) {
return 0;
digitCount[digit]++;
num /= 10;
return 1;
int main() {
int a, b;
if (hasDifferentDigits(i)) {
return 0;
Output
#include <stdio.h>
int main() {
long long n;
scanf("%lld", &n);
while (n > 0) {
maxDigit = digit;
n /= 10;
}
printf("%d", maxDigit);
return 0;
Input
One positive integer n(n<1018).
Output
Print how many times the maximum digit appears in the positive integer n.
#include <stdio.h>
int main() {
long long n;
scanf("%lld", &n);
int max_digit = 0;
int max_digit_count = 0;
while (n > 0) {
max_digit = digit;
max_digit_count = 1;
max_digit_count++;
n /= 10;
printf("%d", max_digit_count);
return 0;
Input
Output
Print in one line all numbers from a to b with only odd digits.
#include <stdio.h>
int main() {
d1 = i / 1000;
d2 = (i / 100) % 10;
d3 = (i / 10) % 10;
d4 = i % 10;
if (!(d1 % 2 == 0 || d2 % 2 == 0 || d3 % 2 == 0 || d4 % 2 == 0)) {
return 0;
Input
Output
#include <stdio.h>
int main() {
d2 = (i / 10) % 10;
d3 = i % 10;
if ( i == d1 * d1 * d1 + d2 * d2 * d2 + d3 * d3 * d3) {
return 0;
Input
Output
#include <stdio.h>
int main() {
d1 = i / 1000;
d2 = (i / 100) % 10;
d3 = (i / 10) % 10;
d4 = i % 10;
if ( i == d1 * d1 * d1 * d1 + d2 * d2 * d2 * d2 + d3 * d3 * d3 * d3 + d4 * d4 * d4 * d4) {
return 0;
Input
One positive integer n (n≤109).
Output
Print the product of nonzero digits for number n.
#include <stdio.h>
int main() {
int num, d, p = 1;
scanf("%d", &num);
d = num % 10;
num /= 10;
if (d != 0){
p = p * d;
p = p * num;
printf("%d", p);
return 0;