0% found this document useful (0 votes)
3 views7 pages

Cse1005 Sample Questions - Module-1

The document contains a series of coding questions and problems that require writing programs in Python. Each question includes a problem statement, input format, output format, and test cases to validate the solution. Topics covered include summing digits, counting vowels, checking for prime numbers, and determining leap years, among others.
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)
3 views7 pages

Cse1005 Sample Questions - Module-1

The document contains a series of coding questions and problems that require writing programs in Python. Each question includes a problem statement, input format, output format, and test cases to validate the solution. Topics covered include summing digits, counting vowels, checking for prime numbers, and determining leap years, among others.
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/ 7

CODING QUESTIONS CODING QUESTIONS

Question 5: Sum of Digits


Problem Statement:
Write a program to calculate the sum of the digits of a given positive
integer
𝑁.

Input:
An integer 𝑁
Question 1: Sum of N Natural Numbers N (1 ≤ 𝑁 ≤ 10^6)
Problem Statement:
Write a Python program to calculate the sum of the first N natural numbers. Output:
The sum of the digits of 𝑁.
Input: an Integer N
N (1 ≤ N ≤ 1000) Test Cases:

Output: Input: 123


The sum of the first 𝑁 Output: 6
Explanation:
Test Cases: 1+2+3=6

Input: 5 Input: 4567


Output: 15 Output: 22
Explanation: Explanation:
4+5+6+7=22
1+2+3+4+5=15
Input: 1001
Input: 10 Output: 2
Output: 55 Explanation:
Explanation: 1+0+0+1=2

55 Input: 9999
1+2+3+...+10=55 Output: 36
Explanation:
Input: 1 9+9+9+9=36
Output: 1
Input: 54321
Input: 20 Output: 15
Output: 210 Explanation:
5+4+3+2+1=15
Input: 100
Output: 5050 Input: 1
Output: 1
Input: 1000
1 Output: 500500
Question: Neon Number Check
Problem Statement:
Write a program to check if a given number 𝑁 is a Neon Number.
A Neon Number is defined as a number where
the sum of the digits of its square is equal to the number itself.

Input:
An integer 𝑁
N (1 ≤ 𝑁 ≤10000)

Output:
Yes if 𝑁
N is a Neon Number; otherwise, output No.

Test Cases:

Input: 9
Output: Yes
Explanation:
9^2 = 81
Question 2: Count Digits in a Number
->8+1=9
Problem Statement:
Write a program to count the number of digits in a given positive integer 𝑁.
. Input: 1
Input: Output: Yes
An integer 𝑁 Explanation:
N (1 ≤ 𝑁 ≤ 100000000000) 1^2 = 1

Output: Input: 10
The number of digits in 𝑁 Output: No
Explanation:
10^2 =100
Test Cases: -> 1+0+0=1, which is not equal to 10.

Input: 12345 Input: 7


Output: 5 Output: No
Explanation:
Input: 100000 7^2 = 49
Output: 6 4+9=13, which is not equal to 7.

Input: 9 Input: 45
Output: 1 Output: No
Explanation:
Input: 456789 45^2 = 2025
Output: 6 2+0+2+5=9, which is not equal to 45.

Input: 1000000000 Input: 3


Output: 10 Output: No
Explanation:
Input: 987654321 3^2 =9
2 Output: 9 -> 9, which is not equal to 3.
Question: Separate Even and Odd Numbers from a List
Problem Statement:
Given a list of integers, separate the even numbers and odd numbers
into two separate lists.

Input:
A list of integers, for example: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

Output:
Two lists: one containing all the even numbers and one containing all
the odd numbers from the input list.

Test Cases:
Question 3: Print Even Numbers in a Range
Problem Statement: Input: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Write a program to print all even numbers from 0 to A Output:

Input: Even numbers: [2, 4, 6, 8, 10]


Integer A Odd numbers: [1, 3, 5, 7, 9]
Input: [15, 24, 33, 42, 51, 60]
(0 >A≤1000) Output:

Output: Even numbers: [24, 42, 60]


All even numbers between 0 to 𝐴 has to be separated by a space. Odd numbers: [15, 33, 51]
Input: [11, 13, 17, 19]
Test Cases: Output:

Input: 10 Even numbers: []


Output: 2 4 6 8 10 Odd numbers: [11, 13, 17, 19]
Input: [2, 4, 6, 8, 10]
Input: 15 Output:
Output: 6 8 10 12 14
Even numbers: [2, 4, 6, 8, 10]
Input: 20 Odd numbers: []
Output: 2 4 6 8 10 12 14 16 18 20 Input: [5, 3, 8, 6, 7, 2, 1, 4]
Output:
Input: 8
Output: 2 4 6 8 Even numbers: [8, 6, 2, 4]
Odd numbers: [5, 3, 7, 1]
Input: 7 Input: [0, 1, 3, 5, 8, 12]
Output: 2 4 6 Output:

Input: 2 Even numbers: [0, 8, 12]


3 Output: 2 Odd numbers: [1, 3, 5]
Question: Count Vowels in a String
Problem Statement:
Write a program to count the number of vowels (a, e, i, o, u) in a given string
𝑆
S.

Input: Question 4: Check Prime Number (Medium)


A string Problem Statement:
𝑆 Write a program to check if a given integer
S containing lowercase English alphabets (1 ≤ length of 𝑆 ≤ 100). 𝑁
N is a prime number. A prime number is a natural number greater
Output: than 1 that has no positive divisors other than 1 and itself.
The number of vowels in the string 𝑆.
Input:
Test Cases: An integer 𝑁
N (2 ≤ 𝑁≤1000).
Input: hello
Output: 2 Output:
Explanation: The vowels in hello are e and o, so the count is 2. Yes if 𝑁 is a prime number; otherwise, output No.

Input: bye Test Cases:


Output: 1
Explanation: The vowels in openai are o, e, a, i, so the count is 1. Input: 7
Output: Yes
Input: abcde
Output: 2 Input: 10
Explanation: The vowels in abcde are a and e, so the count is 2. Output: No

Input: ai Input: 13
Output: 2 Output: Yes
Explanation: The vowels in ai are a and i, so the count is 2.
Input: 4
Input: xyz Output: No
Output: 0
Explanation: There are no vowels in xyz, so the count is 0. Input: 29
Output: Yes
Input: python
Output: 1 Input: 100
Explanation: The only vowel in python is o, so the count is 1 Output: No

ABSOLUTE
DIFFERENCE IN
Shopping Cart Total Leap Year Odd and even Count PRIME NUMBER RANGE
Problem Statement: Problem Statement: Problem Statement: Problem Statement:
You need to calculate the Write a program to write a program to find total number of odd digit Take an integer and You are given two integers,
total cost of items in a print "YES" if the integer start and end, where start is
shopping cart after applying
check whether the and even digit in a given whole number(Dont strictly less than end. Your
given year is a leap consider zero as even number) is prime and "NO" if it is
a discount. First, apply the task is to compute the
not.
initial discount based on the year or not absolute value of the sum of
specified percentage. If the all integers between start
total cost after this initial and end (exclusive). In other
discount exceeds ₹100, words, you need to subtract
apply an additional discount each integer from the sum
of 10% to the total. of all integers in the range
from start+1 to end
(inclusive).

Input Format: Input Format: Input Format: Input Format: Input Format:

The first line contains an single line contains two


integer N (the number of The first line contains The first line contains space seperated values
items in the cart). an integer year The first line contains an integer N an integer N start and end
The next N lines each
contain a floating-point
number representing the
price of each item.
The last line contains a
floating-point number
representing the discount
percentage.

Output Format: Output Format: Output Format: Output Format: Output Format:

Print only the total cost Print Leap YeaR or Print the absolute difference
after discounts Not Leap YeaR Print count of odd digit and even digit YES OR NO value

Sample Input: Sample Input: Sample Input: Sample Input: Sample Input:

3 1999 65841520 9 1
50 . 0 Sample output : 10
30 . 0 Not Leap YeaR Sample output : Sample output :
25 3 NO Sample output :
20 TESTCASE - 1 4 54
INPUT TESTCASE - 1
Sample output : 2024 TESTCASE - 1 INPUT TESTCASE - 1
84 . 0 OUTPUT INPUT 88888 INPUT
Leap YeaR 88888 OUTPUT 10
TESTCASE - 1 OUTPUT NO 100
INPUT TESTCASE - 2 0 OUTPUT
4 INPUT 5 TESTCASE - 2 4995
60 1900 INPUT
40 OUTPUT TESTCASE - 2 79 TESTCASE - 2
30 Not Leap YeaR INPUT OUTPUT INPUT
20 333333 YES 200
10 TESTCASE - 3 OUTPUT 500
OUTPUT INPUT 6 TESTCASE - 3 OUTPUT
121.5 2000 0 INPUT 105150
OUTPUT 97
TESTCASE - 2 Leap YeaR TESTCASE - 3 OUTPUT TESTCASE - 3
INPUT INPUT YES INPUT
5 TESTCASE - 4 1234567 -1
100 INPUT OUTPUT TESTCASE - 4 100
50 2009 4 INPUT OUTPUT
20 OUTPUT 3 111 5050
10 Not Leap YeaR OUTPUT
5 TESTCASE - 4 NO TESTCASE - 4
15 INPUT INPUT
OUTPUT 1357924680 -50
141.5 OUTPUT 50
5 OUTPUT
TESTCASE - 3 4 50
INPUT
4
129
879
560
156
10
OUTPUT
1396.4

TESTCASE - 4
INPUT
3
190
540
632
12
OUTPUT
1078.7

MEDIUM LEVEL MEDIUM LEVEL MEDIUM LEVEL MEDIUM LEVEL MEDIUM LEVEL
first and last guy Fibonacci number TWISTED PRODUCT SPECIAL NUMBER ARMSTRONG OR NOT

Problem Statement: Problem Statement: Problem Statement: Problem Statement: Problem Statement:
write a program to Find the nth fibonacci Take two numbers and find sum of all numbers between Take an integer as input and Write a program to
them that satisfy following condition (inclusive range): print 'YES' if the number is a
sum the frst two and number of a special number. Otherwise, take a number from
last two digit of a sequence print 'NO'. the input and print
number whether or not it's an
Hint:
A special number is a
armstrong number.
number which is equal to 153 = (1*1*1)+(5*5*5)
the sum of the factorials of +(3*3*3) sample two:
its digits.
Example:
8208=> (8**4)+(2**4)+
For number = 145, the (0**4)+(8**4)=>8208
output should be 1! + 4! + 5!
= 145
Hence 145 is a special
number.
Input Format: Input Format: -> Second last digit of number is 4 Input Format:
The first line contains an The first line contains The first line contains
integer N an integer N Input Format: Input Format: an integer N
The first line contains
constrains : Two lines of input each containing a single integer. an integer N
9<N<=10^12 FIRST VALUE INDICATES START VALUE OF RANGE Output Format:
SECOND VALUE INDIACTES END VALUE OF RANGE YES OR NO
Output Format: Output Format: Output Format: TESTCASE - 1
INPUT Output Format:
print the n th position
value in fibonacci A single integer which is the product of all such numbers
sum as integer series which follow the above mentioned conditions. 143 YES OR NO
OUTPUT
Sample Input: Sample Input: Sample Input: NO Sample Input:
30
65165 5 40 TESTCASE - 2 153
Sample output : Sample output : INPUT sample output:
Sample output : 3 40 40585 YES
22 OUTPUT TESTCASE - 1
TESTCASE - 1 TESTCASE - 1 YES INPUT
TESTCASE - 1 INPUT INPUT 123
INPUT 5 10 TESTCASE - 3 OUTPUT
123456 OUTPUT 100 INPUT NO
OUTPUT 3 OUTPUT 6451
14 221 OUTPUT TESTCASE - 2
TESTCASE - 2 NO INPUT
TESTCASE - 2 INPUT TESTCASE - 2 370
INPUT 100 INPUT TESTCASE - 4 OUTPUT
123456 OUTPUT 10 INPUT YES
21892299583455516
OUTPUT 9026 200 1
14 OUTPUT OUTPUT TESTCASE - 3
TESTCASE - 3 941 YES INPUT
TESTCASE - 3 INPUT 371
INPUT 20 TESTCASE - 3 OUTPUT
123457 OUTPUT INPUT YES
OUTPUT 4181 100
15 200 TESTCASE - 4
TESTCASE - 4 OUTPUT INPUT
TESTCASE - 4 INPUT 721 152
INPUT 10 OUTPUT
875612 OUTPUT TESTCASE - 4 NO
OUTPUT 34 INPUT
18 100
1000
OUTPUT
EASY-LEVEL(I ADD EXTRA 4 QUESTIONS
FOR BACKUP) MEDIUM - LEVEL 24481
Find the Fibnocci Series of Number, 1.Check number is Armstrong or not using loop,
Input format:

The first line contains a single integer n (1 ≤ n ≤ 10000), which is the


number to check if it is an Armstrong number.

Input format: Test Case 1:-


The first line contains a single integer n (1 ≤ n ≤ 100), which represents the
number of Fibonacci numbers to printed . Input:153
Output:Armstrong
Sample Input: 3
Sample Output: 011 Test Case 2:-

Sample Input: 4 Input:370


Sample Output: 0112 Output:Armstrong

Sample Input: 6 Test Case 3:-


Sample Output: 011234
Input:123
Sample Input: 7 Output:Not Armstrong
Sample Output: 0112345

Sample Input: 8
Sample Output: 01123456

2.Imagine a group of friends, Sarah, Mike, and Lucy, who went out to
dinner together. They had a great time chatting and sharing stories,
and when the bill came, they wanted to split it fairly and even leave a
nice tip for the service. Sarah suggested using a bill calculator that
she had recently learned about in her programming class.

The calculator first asks them what the bill amount is, then what
percentage of tip they’d like to leave (they decided between 10, 20, or
30%), and finally, how many people will split the bill. Once they enter
all this information, the calculator tells them the total bill, including the
tip, and how much each person needs to pay.,Sample input:bill:300,
tips:10,Share:3,Sample output:Total bill:330.0,People per share:
Reverse the String using for loop without predefined Function, 110.0
Input format:

The first line contains an integer bill (total bill amount).


The second line contains an integer tips (tip amount).
The third line contains an integer Share (the number of people
sharing the bill).

Test Case-1

Input format: Sampleinput:-


bill:300,
The first line contains a single string s, which can be a combination of tips:10,
alphabets or digits. The length of the string s will be between 1 and 100 Share:3,
characters.
Sample output:-
Total bill:330.0,
People per share:110.0
1.Sample input:Hello
SampleOutput:olleH Test-case-2
2.Sample Input: World
Sample Output: dlroW Sample Input:-
3.Sample Input: OpenAI bill: 500
Sample Output: IAnepO tips: 15,Share: 4,
4.Sample Input: Programming
Sample Output: gnimmargorP Expected Output:
5.Sample Input: 12345 Total bill: 575.0
Sample Output: 54321 People per share: 143.75
3.

Emma is planning a movie night with her friends and decides to order
a pizza for the group. She opens a pizza ordering app, and the
options pop up: she can choose between a small, medium, or large
pizza. Since they’re a big group, Emma selects the **large size**.
She’s also prompted with add-on options for toppings: **pepperoni**
and extra cheese

The app shows her the following:


- Pizza Prices:Small ($15), Medium ($20), Large ($25)
-Add-ons
Adding pepperonicosts $2 for a small pizza and $3 for medium or
large.
Adding extra cheese costs $1 for any pizza size.

Emma chooses a **large pizza** with both **pepperoni** and **extra


cheese**. Now, she’s curious about the total price.
Input format:

The first line contains a single character representing the size of the
pizza:
S for small, M for medium, or L for large.

The second line contains a single character indicating whether the


customer wants pepperoni:
Y for yes or N for no.

The third line contains a single character indicating if extra cheese is


desired:
Y for yes or N for no.

Test case-1
Sample Input:

What size pizza do you want? S, M, or L: m


Do you want pepperoni on your pizza? Y or N: y
Do you want extra cheese? Y or N: n

Sample Output:
Your final bill is $23.

Test Case 2:

Input:

What size pizza do you want? S, M, or L: l


Do you want pepperoni on your pizza? Y or N: n
Do you want extra cheese? Y or N: y
Output:

Your final bill is $26.

Test Case 3:-


Input:

What size pizza do you want? S, M, or L: m


Do you want pepperoni on your pizza? Y or N: n
Do you want extra cheese? Y or N: y
Output:

Your final bill is $21.

Test case 4:-


Input:

What size pizza do you want? S, M, or L: l


Do you want pepperoni on your pizza? Y or N: y
Do you want extra cheese? Y or N: y
Output:
Count number of vowels in string using for loop without using any predefined
function., Your final bill is $29.
Input format:

The first line contains a single string s, which can be a combination of


lowercase or uppercase alphabets. The length of the string s will be between
1 and 100 characters.

Sample Input: hello


Sample Output: 2

Sample Input: world


Sample Output: 1

Sample Input: OpenAI


Sample Output: 3

Sample Input: Programming


Sample Output: 3

Sample Input: Python


Sample Output: 1
4.
Calculate the BMI for a person weighing 80 kg and 5.5 feet tall.
Determine their weight classification based on the result.
Sum of odd number from 1 to n number using for loop without predefined weight: 80
function height: 5.5 Underweight
Input Format:

The first line contains the weight as an integer in kilograms, denoted


as Weight: integer kg.
The second line contains the height as a float in feet, denoted as
Height: float feet

Test-case 1

Sample input:-
Weight: 80 kg
Height: 5.5 feet

Sample output:-
BMI: 28.46
Weight Classification: Overweight

Test-case 2

Sample input:-
Weight: 55 kg
Height: 5.2 feet

Sample output:-
BMI: 22.23
Weight Classification: Normal weight

Test-case 3

Sample input:-
Weight: 70 kg
Height: 5.9 feet

Sample output:-
BMI: 23.39
Weight Classification: Normal weight

Test-case 4

Sample input:-
Weight: 95 kg
Height: 6.0 feet

Sample output:-
BMI: 30.04
Weight Classification: Obese

Test-case 5:-

Sample input:-
Weight: 50 kg
Height: 5.3 feet

Input format:

The first line contains two integers, a and b (1 ≤ a ≤ b ≤ 100), separated by the Sample output:-
word "to". These integers represent a range starting from a to b. BMI: 18.71
Weight Classification: Normal weight

Sample input: 1 to 10
Sample Output: 25

Sample input: 1 to 20
Sample Output: 100

Sample input: 1 to 15
Sample Output: 64

Sample input:1 to 5
Sample Output: 9

Sample input: 1 to 1
Sample Output: 1
5.

Liam is excited to visit an amusement park for the first time. As he


arrives at the rollercoaster ride, he sees a sign that says, "Welcome
to the Rollercoaster Ride!" With his heart racing, he steps up to the
height measurement station.

After being measured, the attendant tells Liam he is **135 cm tall**.


Thrilled that he meets the height requirement of **120 cm**, he
moves on to the next step. The attendant asks for his age, and Liam
proudly states that he is **15 years old**.

The attendant informs him that the ride costs different amounts based
on age:
- $7** for ages 18 and under
- $5** for ages 12 and under
- $12** for ages 19 and older

Before Liam pays, the attendant asks if he wants a photo taken


during the ride for an additional **$3**.

Liam decides to go for the photo option to capture the thrill of the ride.
Now, he wonders what his total bill will be after all the charges.

Write a Python program that iterates through numbers from 1 to 50. The Question:
program should:
Using the pricing rules provided in the story, calculate Liam's total bill
Print only the even numbers. if he is **15 years old** and chooses to add a photo to his
Skip numbers that are multiples of 5. rollercoaster ride experience. What is the final amount he needs to
Stop the loop entirely if it reaches the number 49. pay?
Input Format:-

The first line contains the height in centimeters, denoted as Height:


integer cm.
The second line contains the age as an integer, denoted as Age:
integer.
The third line indicates if the user wants a photo taken, with Wants
photo: <y/n> where y stands for yes and n stands for no.

Test Case 1

Sample Input:-
Height: 135 cm
Age: 15
Wants photo: y

Input format: Sample Output:-


Final bill: 10 dollars
The first line contains a string that specifies the range, in the form: "Range
from X to Y", where X and Y are integers representing the start and end of the
range (1 ≤ X ≤ Y ≤ 100). Test Case 2
Sample Input:-
Sample Input: Range from 1 to 50 Height: 135 cm
Expected Output: [2, 4, 6, 8, 12, 14, 16, 18, 22, 24, 26, 28, 32, 34, 36, 38, 42, Age: 10
44, 46, 48] Wants photo: y

Input: Modify range to 1 to 30 Sample Output:-


Expected Output: [2, 4, 6, 8, 12, 14, 16, 18, 22, 24, 26, 28] Final bill: 8 dollars

Input: Modify range to 1 to 20


Expected Output: [2, 4, 6, 8, 12, 14, 16, 18] Test Case 3
Sample Input:-
Input: Modify range to 1 to 10 Height: 135 cm
Expected Output: [2, 4, 6, 8] Age: 20
Wants photo: n
Input: Modify range to 1 to 48
Expected Output: [2, 4, 6, 8, 12, 14, 16, 18, 22, 24, 26, 28, 32, 34, 36, 38, 42, Sample Output:-
44, 46, 48] Final bill: 12 dollars
find Factorial of number using loop.
Input format:

The first line contains a single integer n (0 ≤ n ≤ 20), which represents the
number for which you need to calculate the factorial.

Input: 5
Expected Output: 120

Input: 0
Expected Output: 1

Input: 7
Expected Output: 5040

Input: 3
Expected Output: 6

Input: 10
Expected Output: 3628800
Ask input from user .Find the uppercase letter inside string using predefined
function.
Input format:

The first line contains a single string s, which may include both lowercase and
uppercase alphabets. The string will have a length between 1 and 100
characters.

Input: "HelloWorld"
Expected Output: ['H', 'W']

Input: "PythonProgramming"
Expected Output: ['P', 'P']

Input: "OpenAI"
Expected Output: ['O', 'A', 'I']

Input: "findUpperCASEletters"
Expected Output: ['U', 'C', 'A', 'S', 'E']

Input: "nocaps"
Expected Output: []
Write a Python program that takes an integer input from user.count the
number of odd number.
Input format:

The first line contains a single integer n (0 ≤ n ≤ 100), which represents the
upper limit of the range starting from 1.

Input: 10
Expected Output: 5
(Odd numbers: 1, 3, 5, 7, 9)

Input: 15
Expected Output: 8
(Odd numbers: 1, 3, 5, 7, 9, 11, 13, 15)

Input: 1
Expected Output: 1
(Odd number: 1)

Input: 20
Expected Output: 10
(Odd numbers: 1, 3, 5, 7, 9, 11, 13, 15, 17, 19)

Input: 0
Expected Output: 0
(No odd numbers in range)

Write a program that checks if a given integer is a prime number.

Input: An integer n.
Output: Print "Prime" if n is a prime number, otherwise "Not Prime".

Write a program that checks if a given integer is even or odd. Test Cases:

Input: An integer n. Input: 2 → Output: Prime


Output: Print "Even" if n is even, otherwise "Odd". Input: 10 → Output: Not Prime
Input: 17 → Output: Prime
Test Cases: Input: 20 → Output: Not Prime
Input: 23 → Output: Prime
Input: 4 → Output: Even
Input: 7 → Output: Odd # Input
Input: 0 → Output: Even n = int(input("Enter an integer: "))
Input: -3 → Output: Odd is_prime = True
Input: 100 → Output: Even
# Check for prime
if n <= 1:
is_prime = False
# Input else:
n = int(input("Enter an integer: ")) for i in range(2, int(n**0.5) + 1):
if n % i == 0:
# Check even or odd is_prime = False
if n % 2 == 0: break
print("Even")
else: if is_prime:
print("Odd") print("Prime")
else:
print("Not Prime")
Write a program that calculates the sum of the digits of a given number.

Input: An integer n.
Output: Print the sum of the digits of n. Program to Print the Sum of All Even Numbers up to n
Problem: Given an integer n, print the sum of all even numbers from 1 to n.
Test Cases:
Input: An integer n.
Input: 123 → Output: 6 Output: Print the sum of all even numbers from 1 to n.
Input: 0 → Output: 0
Input: 456 → Output: 15 n = int(input("Enter an integer: "))
Input: 789 → Output: 24 sum_even = 0
Input: 1023 → Output: 6
for i in range(2, n + 1, 2):
# Input sum_even += i
n = int(input("Enter an integer: "))
sum_of_digits = 0 print("Sum of even numbers:", sum_even)
Test Cases:
# Calculate sum of digits
for digit in str(n): Input: 10 → Output: 30
sum_of_digits += int(digit) Input: 5 → Output: 6
Input: 12 → Output: 42
print("Sum of digits:", sum_of_digits) Input: 20 → Output: 110
Input: 1 → Output: 0
Program to Find the Average of Positive Integers
Problem: Given a series of integers, find the average of all positive integers. Stop the input when a negative integer is entered.

Input: Multiple integers until a negative integer is entered.


Output: Print the average of all positive integers.

count = 0
Program to Skip Multiples of 3 Using Continue sum_positive = 0
Problem: Given an integer n, print all numbers from 1 to n, skipping multiples of 3.
while True:
Input: An integer n. num = int(input("Enter an integer: "))
Output: Print numbers from 1 to n, excluding multiples of 3. if num < 0:
break
sum_positive += num
n = int(input("Enter an integer: ")) count += 1

for i in range(1, n + 1): if count > 0:


if i % 3 == 0: average = sum_positive / count
continue print("Average of positive integers:", average)
print(i, end=" ") else:
Test Cases: print("No positive integers entered")
Test Cases:
Input: 10 → Output: 1 2 4 5 7 8 10
Input: 6 → Output: 1 2 4 5 Input: 5 10 15 -1 → Output: 10.0
Input: 15 → Output: 1 2 4 5 7 8 10 11 13 14 Input: 3 7 11 0 -5 → Output: 5.25
Input: 3 → Output: 1 2 Input: -1 → Output: No positive integers entered
Input: 1 → Output: 1 Input: 1 2 -10 → Output: 1.5
Input: 6 -6 → Output: 6.0
Write a program to check if a number is an Armstrong number. An Armstrong number is one where the sum of each digit raised to the power of the number of digits equals the number itself.

Input: A positive integer n.


Output: Print "Armstrong" if n is an Armstrong number, otherwise "Not Armstrong".
Write a program to print the first n terms of the Fibonacci series.
Test Cases:
Input: An integer n.
Output: Print the first n numbers in the Fibonacci series. Input: 153 → Output: Armstrong
Input: 370 → Output: Armstrong
Test Cases: Input: 123 → Output: Not Armstrong
Input: 9474 → Output: Armstrong
Input: 1 → Output: 0 Input: 9475 → Output: Not Armstrong
Input: 5 → Output: 0 1 1 2 3
Input: 7 → Output: 0 1 1 2 3 5 8 # Input
Input: 10 → Output: 0 1 1 2 3 5 8 13 21 34 n = int(input("Enter a positive integer: "))
Input: 0 → Output: No output num_str = str(n)
num_len = len(num_str)
# Input sum_of_powers = 0
n = int(input("Enter the number of terms: "))
a, b = 0, 1 # Calculate sum of powers
for digit in num_str:
# Print Fibonacci series sum_of_powers += int(digit) ** num_len
print("Fibonacci series:")
for _ in range(n): if sum_of_powers == n:
print(a, end=" ") print("Armstrong")
a, b = b, a + b else:
print() # for new line print("Not Armstrong")

Write a program to calculate the factorial of a given integer.

Input: A positive integer n.


Output: Print the factorial of n. Write a program that calculates the sum of squares of the first n natural numbers.

Test Cases: Input: A positive integer n.


Output: Print the sum of squares of the first n natural numbers.
Input: 0 → Output: 1
Input: 3 → Output: 6 Test Cases:
Input: 5 → Output: 120
Input: 7 → Output: 5040 Input: 1 → Output: 1
Input: 10 → Output: 3628800 Input: 2 → Output: 5
Input: 3 → Output: 14
# Input Input: 5 → Output: 55
n = int(input("Enter a positive integer: ")) Input: 10 → Output: 385
result = 1
# Input
# Calculate factorial n = int(input("Enter a positive integer: "))
if n == 0: sum_of_squares = 0
result = 1
else: # Calculate sum of squares
for i in range(1, n + 1): for i in range(1, n + 1):
result *= i sum_of_squares += i ** 2

print("Factorial:", result) print("Sum of squares:", sum_of_squares)

Program to Count the Number of Digits Using While Loop


Problem: Given an integer n, count the number of digits in n.

Input: An integer n.
Program to Calculate the Product of Digits of a Number Output: Print the count of digits in n.
Problem: Given an integer n, calculate the product of its digits.

Input: An integer n. n = int(input("Enter an integer: "))


Output: Print the product of the digits of n. count = 0

if n == 0:
n = int(input("Enter an integer: ")) count = 1
product = 1 else:
while n != 0:
for digit in str(n): count += 1
product *= int(digit) n //= 10

print("Product of digits:", product) print("Number of digits:", count)


Test Cases: Test Cases:

Input: 123 → Output: 6 Input: 123 → Output: 3


Input: 0 → Output: 0 Input: 0 → Output: 1
Input: 456 → Output: 120 Input: 4567 → Output: 4
Input: 789 → Output: 504 Input: 78901 → Output: 5
Input: 1023 → Output: 0
Program to Check if a Number is a Perfect Number
Problem: A perfect number is a positive integer that is equal to the sum of its proper divisors, excluding the number itself. Given an integer n, check if it is a perfect number.

Input: An integer n.
Output: Print "Perfect Number" if n is a perfect number; otherwise, print "Not a Perfect Number".

n = int(input("Enter an integer: "))


sum_of_divisors = 0
Program to Count Occurrences of a Digit in an Integer
for i in range(1, n): Problem: Given an integer n and a digit d, count how many times d appears in n.
if n % i == 0:
sum_of_divisors += i Input: An integer n and a digit d.
Output: Print the count of digit d in n.
if sum_of_divisors == n:
print("Perfect Number")
else: n = int(input("Enter an integer: "))
print("Not a Perfect Number") d = input("Enter the digit to count: ")
Test Cases: count = 0

Input: 6 → Output: Perfect Number for digit in str(n):


(Explanation: The divisors of 6 are 1, 2, and 3, and their sum is 6.) if digit == d:
count += 1
Input: 28 → Output: Perfect Number
(Explanation: The divisors of 28 are 1, 2, 4, 7, and 14, and their sum is 28.) print(f"The digit {d} appears {count} times in {n}")
Test Cases:
Input: 12 → Output: Not a Perfect Number
(Explanation: The divisors of 12 are 1, 2, 3, 4, and 6, and their sum is 16.) Input: 12233, 2 → Output: 2
Input: 55555, 5 → Output: 5
Input: 496 → Output: Perfect Number Input: 789, 1 → Output: 0
(Explanation: The divisors of 496 are 1, 2, 4, 8, 16, 31, 62, 124, and 248, and their
Input:
sum101010,
is 496.)0 → Output: 3
Input: 123456, 6 → Output: 1
Input: 10 → Output: Not a Perfect Number
Program to Check if a Number is Positive, Negative, or Zero
Problem: Given an integer n, determine if it is positive, negative, or zero.

Input: An integer n.
Program to Calculate the Sum of Square of Digits Output: Print "Positive", "Negative", or "Zero" based on the value of n.
Problem: Given an integer n, calculate the sum of the squares of its digits.

Input: An integer n. n = int(input("Enter an integer: "))


Output: Print the sum of the squares of the digits of n.
if n > 0:
print("Positive")
n = int(input("Enter an integer: ")) elif n < 0:
sum_of_squares = 0 print("Negative")
else:
for digit in str(n): print("Zero")
sum_of_squares += int(digit) ** 2 Test Cases:

print("Sum of squares of digits:", sum_of_squares) Input: 5


Output: Positive

Test Cases: Input: -3


Output: Negative
Input: 12 → Output: 5
Input: 0
Input: 123 → Output: 14 Output: Zero

Input: 456 → Output: 77 Input: 100


Output: Positive
Input: 0 → Output: 0
Input: -25
Input: 99 → Output: 162 Output: Negative

You might also like