homework.wenqian.dev
← Back to assignments
Assignment 12026-06-06

Function and Loop Practice

Week 2 level · No arrays · C and Python required

Requirements

  • 10 required problems. Estimated time: 3 – 5 hours.
  • Each problem requires a C version (.c) and a Python version (.py).
  • Do not use arrays or lists for these problems. These are Week 2 practice problems.
  • You must define and call the required function. Do not put all logic in main.
  • Output format must match the samples exactly.

Problems

1Sum from 1 to n

Write a function int sum_to_n(int n) that returns 1 + 2 + ... + n.

Input format

text
One line containing one integer n.Constraint: 1 ≤ n ≤ 10,000

Output format

text
One line containing the sum from 1 to n.

Sample 1

Input

text
5

Output

text
15

Sample 2

Input

text
1

Output

text
1

Sample 3

Input

text
100

Output

text
5050

Required structure

sum_to_n.c
#include <stdio.h>int sum_to_n(int n) {    // TODO: use a loop to compute 1 + 2 + ... + n}int main() {    int n;    scanf("%d", &n);    printf("%d\n", sum_to_n(n));    return 0;}
sum_to_n.py
def sum_to_n(n):    # TODO: use a loop to compute 1 + 2 + ... + n    passn = int(input())print(sum_to_n(n))
Hint: Start with total = 0. Then add each number from 1 to n.
2Count Digits

Write a function int count_digits(int n) that returns how many digits are in a non-negative integer.

For this problem, 0 has 1 digit.

Input format

text
One line containing one non-negative integer n.Constraint: 0 ≤ n ≤ 100,000,000

Output format

text
One line containing the number of digits in n.

Sample 1

Input

text
0

Output

text
1

Sample 2

Input

text
7

Output

text
1

Sample 3

Input

text
120030

Output

text
6

Required structure

count_digits.c
#include <stdio.h>int count_digits(int n) {    // TODO: return how many digits n has}int main() {    int n;    scanf("%d", &n);    printf("%d\n", count_digits(n));    return 0;}
count_digits.py
def count_digits(n):    # TODO: return how many digits n has    passn = int(input())print(count_digits(n))
Hint: Use n = n / 10 in C or n //= 10 in Python to remove the last digit. Handle n == 0 before the loop.
3Sum of Digits

Write a function int sum_digits(int n) that returns the sum of all digits in n.

Input format

text
One line containing one non-negative integer n.Constraint: 0 ≤ n ≤ 100,000,000

Output format

text
One line containing the sum of digits.

Sample 1

Input

text
1234

Output

text
10

Sample 2

Input

text
100

Output

text
1

Sample 3

Input

text
0

Output

text
0

Sample 4

Input

text
9999

Output

text
36

Required structure

sum_digits.c
#include <stdio.h>int sum_digits(int n) {    // TODO: use n % 10 to get each digit}int main() {    int n;    scanf("%d", &n);    printf("%d\n", sum_digits(n));    return 0;}
sum_digits.py
def sum_digits(n):    # TODO: use n % 10 to get each digit    passn = int(input())print(sum_digits(n))
Hint: This is the same digit pattern as reverse integer: get the last digit with n % 10, add it to total, then remove it.
4Integer Power

Write a function int power_int(int base, int exponent) that returns base raised to exponent.

Do not use pow or **. Use a loop.

Input format

text
One line containing two integers: base and exponent.Constraint: -10 ≤ base ≤ 10, 0 ≤ exponent ≤ 9

Output format

text
One line containing base raised to exponent.

Sample 1

Input

text
2 5

Output

text
32

Sample 2

Input

text
7 0

Output

text
1

Sample 3

Input

text
-2 3

Output

text
-8

Required structure

power_int.c
#include <stdio.h>int power_int(int base, int exponent) {    // TODO: multiply base exponent times}int main() {    int base, exponent;    scanf("%d %d", &base, &exponent);    printf("%d\n", power_int(base, exponent));    return 0;}
power_int.py
def power_int(base, exponent):    # TODO: multiply base exponent times    passbase, exponent = map(int, input().split())print(power_int(base, exponent))
Hint: Start with result = 1. This also handles exponent 0 correctly.
5Count Primes in a Range

Write int is_prime(int n) and int count_primes(int L, int R). The second function returns how many primes are in the inclusive range [L, R].

Input format

text
One line containing two integers L and R.Constraint: 1 ≤ L ≤ R ≤ 10,000

Output format

text
One line containing how many primes are in [L, R].

Sample 1

Input

text
2 10

Output

text
4

Sample 2

Input

text
14 16

Output

text
0

Sample 3

Input

text
20 30

Output

text
2

Sample 4

Input

text
1 1

Output

text
0

Required structure

count_primes.c
#include <stdio.h>int is_prime(int n) {    // TODO: return 1 if n is prime, 0 otherwise}int count_primes(int L, int R) {    // TODO: call is_prime for every number from L to R}int main() {    int L, R;    scanf("%d %d", &L, &R);    printf("%d\n", count_primes(L, R));    return 0;}
count_primes.py
def is_prime(n):    # TODO: return True if n is prime, False otherwise    passdef count_primes(L, R):    # TODO: call is_prime for every number from L to R    passL, R = map(int, input().split())print(count_primes(L, R))
Hint: Inside is_prime, remember that numbers less than 2 are not prime. In the loop, checking up to i * i <= n is enough.
6Count Even Numbers in a Range

Write a function int count_even(int L, int R) that returns how many even numbers are in the inclusive range [L, R].

Input format

text
One line containing two integers L and R.Constraint: 1 ≤ L ≤ R ≤ 10,000

Output format

text
One line containing how many even numbers are in [L, R].

Sample 1

Input

text
1 10

Output

text
5

Sample 2

Input

text
3 3

Output

text
0

Sample 3

Input

text
8 12

Output

text
3

Required structure

count_even.c
#include <stdio.h>int count_even(int L, int R) {    // TODO: count numbers x where x % 2 == 0}int main() {    int L, R;    scanf("%d %d", &L, &R);    printf("%d\n", count_even(L, R));    return 0;}
count_even.py
def count_even(L, R):    # TODO: count numbers x where x % 2 == 0    passL, R = map(int, input().split())print(count_even(L, R))
Hint: Loop from L to R. Each time x % 2 == 0, increase the counter by 1.
7Sum of Multiples

Write a function int sum_multiples(int n, int k) that returns the sum of all positive integers from 1 to n that are divisible by k.

Input format

text
One line containing two integers n and k.Constraint: 1 ≤ n ≤ 10,000, 1 ≤ k ≤ 10,000

Output format

text
One line containing the sum of multiples of k from 1 to n.

Sample 1

Input

text
10 3

Output

text
18

Sample 2

Input

text
5 2

Output

text
6

Sample 3

Input

text
7 9

Output

text
0

Required structure

sum_multiples.c
#include <stdio.h>int sum_multiples(int n, int k) {    // TODO: add every x where x % k == 0}int main() {    int n, k;    scanf("%d %d", &n, &k);    printf("%d\n", sum_multiples(n, k));    return 0;}
sum_multiples.py
def sum_multiples(n, k):    # TODO: add every x where x % k == 0    passn, k = map(int, input().split())print(sum_multiples(n, k))
Hint: This is a sum problem with an if condition inside the loop.
8Largest Proper Divisor

A proper divisor of n is a positive divisor smaller than n. Write int largest_proper_divisor(int n) that returns the largest proper divisor of n.

For this problem, if n is 1, return 0.

Input format

text
One line containing one integer n.Constraint: 1 ≤ n ≤ 10,000

Output format

text
One line containing the largest proper divisor of n.

Sample 1

Input

text
10

Output

text
5

Sample 2

Input

text
13

Output

text
1

Sample 3

Input

text
1

Output

text
0

Sample 4

Input

text
36

Output

text
18

Required structure

largest_proper_divisor.c
#include <stdio.h>int largest_proper_divisor(int n) {    // TODO: return the largest positive divisor smaller than n}int main() {    int n;    scanf("%d", &n);    printf("%d\n", largest_proper_divisor(n));    return 0;}
largest_proper_divisor.py
def largest_proper_divisor(n):    # TODO: return the largest positive divisor smaller than n    passn = int(input())print(largest_proper_divisor(n))
Hint: One simple solution is to loop from 1 to n - 1 and remember the latest divisor. Handle n = 1 first.
9Palindrome Number

A number is a palindrome if it reads the same from left to right and from right to left. Write int is_palindrome(int n) that returns 1 if n is a palindrome, and 0 otherwise.

Input format

text
One line containing one non-negative integer n.Constraint: 0 ≤ n ≤ 1,000,000

Output format

text
Print YES if n is a palindrome. Otherwise print NO.

Sample 1

Input

text
121

Output

text
YES

Sample 2

Input

text
123

Output

text
NO

Sample 3

Input

text
7

Output

text
YES

Sample 4

Input

text
1001

Output

text
YES

Required structure

palindrome.c
#include <stdio.h>int is_palindrome(int n) {    // TODO: reverse n and compare with the original value}int main() {    int n;    scanf("%d", &n);    printf("%s\n", is_palindrome(n) ? "YES" : "NO");    return 0;}
palindrome.py
def is_palindrome(n):    # TODO: reverse n and compare with the original value    passn = int(input())print("YES" if is_palindrome(n) else "NO")
Hint: This reuses the reverse-integer idea. Save the original value before changing n.
10Perfect Number

A number is perfect if the sum of its proper divisors equals the number itself. Write int is_perfect(int n) that returns 1 if n is perfect, and 0 otherwise.

Input format

text
One line containing one integer n.Constraint: 1 ≤ n ≤ 10,000

Output format

text
Print YES if n is a perfect number. Otherwise print NO.

Sample 1

Input

text
6

Output

text
YES

Sample 2

Input

text
28

Output

text
YES

Sample 3

Input

text
12

Output

text
NO

Sample 4

Input

text
1

Output

text
NO

Required structure

perfect.c
#include <stdio.h>int is_perfect(int n) {    // TODO: sum all proper divisors of n}int main() {    int n;    scanf("%d", &n);    printf("%s\n", is_perfect(n) ? "YES" : "NO");    return 0;}
perfect.py
def is_perfect(n):    # TODO: sum all proper divisors of n    passn = int(input())print("YES" if is_perfect(n) else "NO")
Hint: Loop from 1 to n - 1. If a number divides n, add it to the sum. Then compare the sum with n.
Check: For every problem, test the sample inputs first. Then test the smallest input allowed by the problem.
— Assignment 1 End —