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
Write a function int sum_to_n(int n) that returns 1 + 2 + ... + n.
Input format
One line containing one integer n.Constraint: 1 ≤ n ≤ 10,000Output format
One line containing the sum from 1 to n.Sample 1
Input
5Output
15Sample 2
Input
1Output
1Sample 3
Input
100Output
5050Required structure
#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;}def sum_to_n(n): # TODO: use a loop to compute 1 + 2 + ... + n passn = int(input())print(sum_to_n(n))total = 0. Then add each number from 1 to n.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
One line containing one non-negative integer n.Constraint: 0 ≤ n ≤ 100,000,000Output format
One line containing the number of digits in n.Sample 1
Input
0Output
1Sample 2
Input
7Output
1Sample 3
Input
120030Output
6Required structure
#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;}def count_digits(n): # TODO: return how many digits n has passn = int(input())print(count_digits(n))n = n / 10 in C or n //= 10 in Python to remove the last digit. Handle n == 0 before the loop.Write a function int sum_digits(int n) that returns the sum of all digits in n.
Input format
One line containing one non-negative integer n.Constraint: 0 ≤ n ≤ 100,000,000Output format
One line containing the sum of digits.Sample 1
Input
1234Output
10Sample 2
Input
100Output
1Sample 3
Input
0Output
0Sample 4
Input
9999Output
36Required structure
#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;}def sum_digits(n): # TODO: use n % 10 to get each digit passn = int(input())print(sum_digits(n))n % 10, add it to total, then remove it.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
One line containing two integers: base and exponent.Constraint: -10 ≤ base ≤ 10, 0 ≤ exponent ≤ 9Output format
One line containing base raised to exponent.Sample 1
Input
2 5Output
32Sample 2
Input
7 0Output
1Sample 3
Input
-2 3Output
-8Required structure
#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;}def power_int(base, exponent): # TODO: multiply base exponent times passbase, exponent = map(int, input().split())print(power_int(base, exponent))result = 1. This also handles exponent 0 correctly.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
One line containing two integers L and R.Constraint: 1 ≤ L ≤ R ≤ 10,000Output format
One line containing how many primes are in [L, R].Sample 1
Input
2 10Output
4Sample 2
Input
14 16Output
0Sample 3
Input
20 30Output
2Sample 4
Input
1 1Output
0Required structure
#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;}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))is_prime, remember that numbers less than 2 are not prime. In the loop, checking up to i * i <= n is enough.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
One line containing two integers L and R.Constraint: 1 ≤ L ≤ R ≤ 10,000Output format
One line containing how many even numbers are in [L, R].Sample 1
Input
1 10Output
5Sample 2
Input
3 3Output
0Sample 3
Input
8 12Output
3Required structure
#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;}def count_even(L, R): # TODO: count numbers x where x % 2 == 0 passL, R = map(int, input().split())print(count_even(L, R))x % 2 == 0, increase the counter by 1.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
One line containing two integers n and k.Constraint: 1 ≤ n ≤ 10,000, 1 ≤ k ≤ 10,000Output format
One line containing the sum of multiples of k from 1 to n.Sample 1
Input
10 3Output
18Sample 2
Input
5 2Output
6Sample 3
Input
7 9Output
0Required structure
#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;}def sum_multiples(n, k): # TODO: add every x where x % k == 0 passn, k = map(int, input().split())print(sum_multiples(n, k))if condition inside the loop.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
One line containing one integer n.Constraint: 1 ≤ n ≤ 10,000Output format
One line containing the largest proper divisor of n.Sample 1
Input
10Output
5Sample 2
Input
13Output
1Sample 3
Input
1Output
0Sample 4
Input
36Output
18Required structure
#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;}def largest_proper_divisor(n): # TODO: return the largest positive divisor smaller than n passn = int(input())print(largest_proper_divisor(n))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
One line containing one non-negative integer n.Constraint: 0 ≤ n ≤ 1,000,000Output format
Print YES if n is a palindrome. Otherwise print NO.Sample 1
Input
121Output
YESSample 2
Input
123Output
NOSample 3
Input
7Output
YESSample 4
Input
1001Output
YESRequired structure
#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;}def is_palindrome(n): # TODO: reverse n and compare with the original value passn = int(input())print("YES" if is_palindrome(n) else "NO")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
One line containing one integer n.Constraint: 1 ≤ n ≤ 10,000Output format
Print YES if n is a perfect number. Otherwise print NO.Sample 1
Input
6Output
YESSample 2
Input
28Output
YESSample 3
Input
12Output
NOSample 4
Input
1Output
NORequired structure
#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;}def is_perfect(n): # TODO: sum all proper divisors of n passn = int(input())print("YES" if is_perfect(n) else "NO")