0% found this document useful (0 votes)
48 views16 pages

Week 3 - 1 Loop

The document contains solutions to programming problems involving integer numbers. It includes problems to count ones in binary representation, find the sum of digits, reverse a number, count occurrences of a digit, check for divisibility by 11, find the maximum and most frequent digits, and more. The solutions use basic operations like modulo, division, and while loops.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
48 views16 pages

Week 3 - 1 Loop

The document contains solutions to programming problems involving integer numbers. It includes problems to count ones in binary representation, find the sum of digits, reverse a number, count occurrences of a digit, check for divisibility by 11, find the maximum and most frequent digits, and more. The solutions use basic operations like modulo, division, and while loops.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

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

One integer n (0 ≤ n ≤ 2 ∙109).

Output

Print the number of ones in binary representation of n.

#include <studio.h>

int main() {

int n;

scanf("%d", &n);

// Count the number of ones in the binary representation of n

int count = 0;

while (n > 0) {

if (n % 2 == 1) {

count++;

}
n /= 2;

printf ("%d", count);

return 0;

B.The sum of digits(1603)


Find the sum of the digits of the integer n.

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>

#include <stdlib.h> // For abs function

int main() {

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

// Calculate the sum of digits (considering negative numbers)

int sum = 0;

int num = abs(n); // Take the absolute value to handle negative numbers

while (num > 0) {

sum += num % 10; // Extract the last digit and add it to the sum

num /= 10; // Remove the last digit

}
printf("%d", sum);

return 0;

C.The sum of the first and the last digits in


the number(1606)
Find the sum of the first and the last digit of an integer.

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>

#include <stdlib.h> // For abs function

nt main() {

int n;

scanf("%d", &n);

// Extract the first and last digit


int last_digit = abs(n) % 10;

int first_digit = abs(n);

while (first_digit >= 10) {

first_digit /= 10;

// Calculate the sum of the first and last digit

int sum = first_digit + last_digit;

printf("%d", sum);

return 0;
}

D.The number in reverse order(1607)


Write a nonnegative integer n in reverse order.

Input

One nonnegative 64-bit integer.

Output

Print the number in reverse order.

#include <stdio.h>

#include <string.h>

int main() {

char input[30]; scanf("%s", input);

int length = strlen(input);

for (int i = 0; i < length / 2; i ++){

char temp = input[i];

input[i] = input[length - i - 1];

input[length - i - 1] = temp;
}

printf("%s", input);

return 0;

E.The number of specified digits(1609)


Count the number of digits a in number n.
Input
The first line contains one 32-bit integer number n. Number n can be negative. The second line contains a
single digit a.

Output
Print the number of digits a in number n.

#include <stdio.h>

#include <stdlib.h> // For abs function

int main() {

int n, a;

scanf("%d %d", &n, &a);

// Take the absolute value of n to handle negative numbers

n = abs(n);

// Count the number of occurrences of digit a in n


int count = 0;
while (n > 0) {

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

A positive integer $n$$(10 ≤ n ≤ 2 \cdot 10^9)$.

Output

Print the product of sums of digits at even and at odd positions.

#include <stdio.h>

#include <stdlib.h> // For abs function

int main() {

unsigned long long n;

scanf("%llu", &n);
// Calculate the sums of digits at even and odd positions

unsigned long long sum_even = 0;

unsigned long long sum_odd = 0;

int position = 1; // Initialize position to track even or odd position

while (n > 0) {

if (position % 2 == 0) {

sum_even += n % 10;

} else {

sum_odd += n % 10;
}

n /= 10;

position++;

// Calculate the product of the sums

unsigned long long product = sum_even * sum_odd;

printf("%llu", product);

return 0;

H.The first digit of the number(8243)


Find the first digit of an integer. Start count digits from the leftmost one.

Input

One 64-bit integer containing at least one digits. The number can be negative.

Output

Print the first digit of a given integer. #include <stdio.h>

#include <stdlib.h>

int main() {

long long number;


scanf("%lld", &number);

number = abs(number);

while (number >= 10){

number /= 10;

printf("%lld", number);

return 0;

I.Numbers with different digits(8533)


Print all four digit numbers from a to b with different digits.

Input

Two integers a and b (1000 ≤ a ≤ b ≤ 9999).

Output

Print in one line all numbers from a to b with different digits.

#include <stdio.h>

int hasDifferentDigits(int num) {

int digitCount[10] = {0};


while (num > 0) {

int digit = num % 10;

if (digitCount[digit] > 0) {

return 0;

digitCount[digit]++;

num /= 10;

return 1;

int main() {

int a, b;

scanf("%d %d", &a, &b);

for (int i = a; i <= b; i++) {

if (hasDifferentDigits(i)) {

printf("%d ", i);

return 0;

J.Maximum digit in a number(8630)


Find the maximum digit in a positive integer n.
Input

One positive integer n (n < 1018).

Output

Print the maximum digit in a positive integer n.

#include <stdio.h>

int main() {

long long n;

scanf("%lld", &n);

int maxDigit = -1;

while (n > 0) {

int digit = n % 10;

if (digit > maxDigit) {

maxDigit = digit;

n /= 10;

}
printf("%d", maxDigit);

return 0;

K.Number of maximal digits(8631)


Find out how many times the maximum digit appears in the positive integer n.

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

int digit = n % 10;

if (digit > max_digit) {

max_digit = digit;
max_digit_count = 1;

} else if (digit == max_digit) {

max_digit_count++;

n /= 10;

printf("%d", max_digit_count);

return 0;

N.Numbers with odd digits(8640)


Print all four digit numbers from a to b with only odd digits.

Input

Two integers a and b (1000 ≤ a ≤ b ≤ 9999).

Output

Print in one line all numbers from a to b with only odd digits.

#include <stdio.h>

int main() {

int a, b, i, d1, d2, d3, d4;

scanf("%d %d", &a, &b);


for (i = a; i <= b; i++) {

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

printf("%d ", i);

return 0;

O.Three digit Armstrong numbers(8641)


Three digit number is called Armstrong number if the sum of cubes of its digits equals to the number. For
example, 153 = 13 + 53 + 33 is an Armstrong number. Print all Armstrong numbers from a to b.

Input

Two integers a and b (100 ≤ a ≤ b ≤ 999).

Output

Print in one line all Armstrong numbers from a to b.

#include <stdio.h>

int main() {

int a, b, i, d1, d2, d3;

scanf("%d %d", &a, &b);

for (i = a; i <= b; i++) {


d1 = i / 100;

d2 = (i / 10) % 10;

d3 = i % 10;

if ( i == d1 * d1 * d1 + d2 * d2 * d2 + d3 * d3 * d3) {

printf("%d ", i);

return 0;

P.Four digit Armstrong numbers(8642)


Four digit number is called Armstrong number if the sum of fourth power of its digits equals to the number.
For example, 8208 = 84 + 24 + 04 + 84 is an Armstrong number. Print all Armstrong numbers from a to b.

Input

Two integers a and b (1000 ≤ a ≤ b ≤ 9999).

Output

Print in one line all Armstrong numbers from a to b.

#include <stdio.h>

int main() {

int a, b, i, d1, d2, d3, d4;

scanf("%d %d", &a, &b);

for (i = a; i <= b; i++) {

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

printf("%d ", i);

return 0;

Q.Product of nonzero digits(8681)


Find the product of nonzero digits for given integer.

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

while (num >= 10){

d = num % 10;

num /= 10;
if (d != 0){

p = p * d;

p = p * num;

printf("%d", p);

return 0;

You might also like