0% found this document useful (0 votes)
32 views63 pages

Cse1005 Question Bank Module 1

Engineering science

Uploaded by

deekshithkr12345
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)
32 views63 pages

Cse1005 Question Bank Module 1

Engineering science

Uploaded by

deekshithkr12345
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/ 63

MODULE – 1

Question 1: Sum of N Natural Numbers


Problem Statement:
Write a Python program to calculate the sum of the first N natural
numbers.

Input: an Integer N
N (1 ≤ N ≤ 1000)

Output:
The sum of the first 𝑁

Test Cases:

Input: 5
Output: 15
Explanation:

1+2+3+4+5=15

Input: 10
Output: 55
Explanation:

55
1+2+3+...+10=55

Input: 1
Output: 1

Input: 20
Output: 210

Input: 100
Output: 5050

Input: 1000
Output: 500500
Question 2: Count Digits in a Number
Problem Statement:
Write a program to count the number of digits in a given positive integer
𝑁.
.
Input:
An integer 𝑁
N (1 ≤ 𝑁 ≤ 100000000000)

Output:
The number of digits in 𝑁

Test Cases:
Input: 12345
Output: 5

Input: 100000
Output: 6

Input: 9
Output: 1

Input: 456789
Output: 6

Input: 1000000000
Output: 10

Input: 987654321
Output: 9
Question 3: Print Even Numbers in a Range
Problem Statement:
Write a program to print all even numbers from 0 to A

Input:
Integer A

(0 >A≤1000)

Output:
All even numbers between 0 to 𝐴 has to be separated by a space.

Test Cases:

Input: 10
Output: 2 4 6 8 10

Input: 15
Output: 6 8 10 12 14

Input: 20
Output: 2 4 6 8 10 12 14 16 18 20

Input: 8
Output: 2 4 6 8

Input: 7
Output: 2 4 6

Input: 2
Output: 2
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:
A string
𝑆
S containing lowercase English alphabets (1 ≤ length of 𝑆 ≤ 100).

Output:
The number of vowels in the string 𝑆.

Test Cases:

Input: hello
Output: 2
Explanation: The vowels in hello are e and o, so the count is 2.

Input: bye
Output: 1
Explanation: The vowels in openai are o, e, a, i, so the count is 1.

Input: abcde
Output: 2
Explanation: The vowels in abcde are a and e, so the count is 2.

Input: ai
Output: 2
Explanation: The vowels in ai are a and i, so the count is 2.

Input: xyz
Output: 0
Explanation: There are no vowels in xyz, so the count is 0.

Input: python
Output: 1
Explanation: The only vowel in python is o, so the count is 1

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 𝑁
N (1 ≤ 𝑁 ≤ 10^6)

Output:
The sum of the digits of 𝑁.

Test Cases:

Input: 123
Output: 6
Explanation:
1+2+3=6

Input: 4567
Output: 22
Explanation:
4+5+6+7=22

Input: 1001
Output: 2
Explanation:
1+0+0+1=2

Input: 9999
Output: 36
Explanation:
9+9+9+9=36

Input: 54321
Output: 15
Explanation:
5+4+3+2+1=15

Input: 1
Output: 1
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
->8+1=9

Input: 1
Output: Yes
Explanation:
1^2 = 1

Input: 10
Output: No
Explanation:
10^2 =100
-> 1+0+0=1, which is not equal to 10.

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

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

Input: 3
Output: No
Explanation:
3^2 =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:

Input:
1, 2, 3, 4, 5, 6, 7, 8, 9, 10
Output:

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


Odd numbers: [1, 3, 5, 7, 9]
Input: [15, 24, 33, 42, 51, 60]
Output:

Even numbers: [24, 42, 60]


Odd numbers: [15, 33, 51]
Input: [11, 13, 17, 19]
Output:

Even numbers: []
Odd numbers: [11, 13, 17, 19]
Input: [2, 4, 6, 8, 10]
Output:

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


Odd numbers: []
Input: [5, 3, 8, 6, 7, 2, 1, 4]
Output:

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


Odd numbers: [5, 3, 7, 1]
Input: [0, 1, 3, 5, 8, 12]
Output:

Even numbers: [0, 8, 12]


Odd numbers: [1, 3, 5]
Question 4: Check Prime Number (Medium)
Problem Statement:
Write a program to check if a given integer
𝑁
N is a prime number. A prime number is a natural number greater than 1
that has no positive divisors other than 1 and itself.

Input:
An integer 𝑁
N (2 ≤ 𝑁≤1000).

Output:
Yes if 𝑁 is a prime number; otherwise, output No.
Test Cases:

Input: 7
Output: Yes

Input: 10
Output: No

Input: 13
Output: Yes

Input: 4
Output: No

Input: 29
Output: Yes

Input: 100
Output: No

Shopping Cart Total

Problem Statement:
You need to calculate the total cost of items in a shopping cart after
applying a discount. First, apply the initial discount based on the
specified percentage. If the total cost after this initial discount exceeds
₹100, apply an additional discount of 10% to the total.

Input Format:

The first line contains an integer N (the number of items in the cart).
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:

Print only the total cost after discounts

Sample Input:

3
50 . 0
30 . 0
25
20

Sample output :
84 . 0

TESTCASE - 1
INPUT
4
60
40
30
20
10
OUTPUT
121.5

TESTCASE - 2
INPUT
5
100
50
20
10
5
15
OUTPUT
141.5

TESTCASE - 3
INPUT
4
129
879
560
156
10
OUTPUT
1396.4

TESTCASE - 4
INPUT
3
190
540
632
12
OUTPUT
1078.7
MEDIUM LEVEL
first and last guy

Problem Statement:
write a program to sum the frst two and last two digit of a number
Input Format:
The first line contains an integer N
constrains :
9<N<=10^12

Output Format:

sum as integer
Sample Input:

65165

Sample output :
22

TESTCASE - 1
INPUT
123456
OUTPUT
14

TESTCASE - 2
INPUT
123456
OUTPUT
14

TESTCASE - 3
INPUT
123457
OUTPUT
15

TESTCASE - 4
INPUT
875612
OUTPUT
18

Leap Year

Problem Statement:
Write a program to check whether the given year is a leap year or not

Input Format:

The first line contains an integer year

Output Format:

Print Leap YeaR or Not Leap YeaR

Sample Input:

1999
Sample output :
Not Leap YeaR
TESTCASE - 1
INPUT
2024
OUTPUT
Leap YeaR

TESTCASE - 2
INPUT
1900
OUTPUT
Not Leap YeaR

TESTCASE - 3
INPUT
2000
OUTPUT
Leap YeaR

TESTCASE - 4
INPUT
2009
OUTPUT
Not Leap YeaR

Odd and even Count


Problem Statement:
write a program to find total number of odd digit and even digit in a given
whole number(Dont consider zero as even number)

Input Format:

The first line contains an integer N

Output Format:

Print count of odd digit and even digit

Sample Input:

65841520

Sample output :
3
4

TESTCASE - 1
INPUT
88888
OUTPUT
0
5
TESTCASE - 2
INPUT
333333
OUTPUT
6
0

TESTCASE - 3
INPUT
1234567
OUTPUT
4
3

TESTCASE - 4
INPUT
1357924680
OUTPUT
5
4

PRIME NUMBER

Problem Statement:
Take an integer and print "YES" if the integer is prime and "NO" if it is
not.

Input Format:

The first line contains an integer N

Output Format:

YES OR NO

Sample Input:

Sample output :
NO

TESTCASE - 1
INPUT
88888
OUTPUT
NO

TESTCASE - 2
INPUT
79
OUTPUT
YES

TESTCASE - 3
INPUT
97
OUTPUT
YES

TESTCASE - 4
INPUT
111
OUTPUT
NO

ABSOLUTE DIFFERENCE IN RANGE

You are given two integers, start and end, where start is strictly less than
end. Your task is to compute the absolute value of the sum of all
integers between start and end (exclusive). In other words, you need to
subtract each integer from the sum of all integers in the range from
start+1 to end (inclusive).

Input Format:

single line contains two space seperated values start and end
Output Format:

Print the absolute difference value

Sample Input:

1
10

Sample output :
54

TESTCASE - 1
INPUT
10
100
OUTPUT
4995

TESTCASE - 2
INPUT
200
500
OUTPUT
105150

TESTCASE - 3
INPUT
-1
100
OUTPUT
5050

TESTCASE - 4
INPUT
-50
50
OUTPUT
50

MEDIUM LEVEL
Fibonacci number

Problem Statement:
Find the nth fibonacci number of a sequence
Input Format:
The first line contains an integer N
Output Format:

print the n th position value in fibonacci series

Sample Input:

5
Sample output :
3

TESTCASE - 1
INPUT
5
OUTPUT
3

TESTCASE - 2
INPUT
100
OUTPUT
218922995834555169026

TESTCASE - 3
INPUT
20
OUTPUT
4181

TESTCASE - 4
INPUT
10
OUTPUT
34

MEDIUM LEVEL
TWISTED PRODUCT

Problem Statement:
Take two numbers and find sum of all numbers between them that
satisfy following condition (inclusive range):
-> Second last digit of number is 4
Input Format:
Two lines of input each containing a single integer.
FIRST VALUE INDICATES START VALUE OF RANGE
SECOND VALUE INDIACTES END VALUE OF RANGE
Output Format:

A single integer which is the product of all such numbers which follow
the above mentioned conditions.

Sample Input:
30
40
Sample output :
40

TESTCASE - 1
INPUT
10
100
OUTPUT
221

TESTCASE - 2
INPUT
10
200
OUTPUT
941

TESTCASE - 3
INPUT
100
200
OUTPUT
721

TESTCASE - 4
INPUT
100
1000
OUTPUT
24481

MEDIUM LEVEL
SPECIAL NUMBER

Problem Statement:
Take an integer as input and print 'YES' if the number is a special
number. Otherwise, print 'NO'.

Hint:
A special number is a number which is equal to the sum of the factorials
of its digits.
Example:
For number = 145, the output should be 1! + 4! + 5! = 145
Hence 145 is a special number.

Input Format:
The first line contains an integer N
Output Format:
YES OR NO
TESTCASE - 1
INPUT
143
OUTPUT
NO

TESTCASE - 2
INPUT
40585
OUTPUT
YES

TESTCASE - 3
INPUT
6451
OUTPUT
NO

TESTCASE - 4
INPUT
1
OUTPUT
YES

MEDIUM LEVEL
ARMSTRONG OR NOT

Problem Statement:
Write a program to take a number from the input and print whether or not
it's an armstrong number. 153 = (1*1*1)+(5*5*5)+(3*3*3) sample two:
8208=> (8**4)+(2**4)+(0**4)+(8**4)=>8208
Input Format:
The first line contains an integer N

Output Format:
YES OR NO

Sample Input:

153
sample output:
YES
TESTCASE - 1
INPUT
123
OUTPUT
NO

TESTCASE - 2
INPUT
370
OUTPUT
YES

TESTCASE - 3
INPUT
371
OUTPUT
YES

TESTCASE - 4
INPUT
152
OUTPUT
NO

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.

Test Case 1:-

Input:153
Output:Armstrong
Test Case 2:-

Input:370
Output:Armstrong

Test Case 3:-

Input:123
Output:Not Armstrong

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: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

Sampleinput:-
bill:300,
tips:10,
Share:3,

Sample output:-
Total bill:330.0,
People per share:110.0

Test-case-2

Sample Input:-
bill: 500
tips: 15,Share: 4,

Expected Output:
Total bill: 575.0
People per share: 143.75

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:

Your final bill is $29.

4.
Calculate the BMI for a person weighing 80 kg and 5.5 feet tall.
Determine their weight classification based on the result.
weight: 80
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
Sample output:-
BMI: 18.71
Weight Classification: Normal weight

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.

Question:

Using the pricing rules provided in the story, calculate Liam's total bill if
he is **15 years old** and chooses to add a photo to his rollercoaster
ride experience. What is the final amount he needs to 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

Sample Output:-
Final bill: 10 dollars

Test Case 2
Sample Input:-
Height: 135 cm
Age: 10
Wants photo: y

Sample Output:-
Final bill: 8 dollars

Test Case 3
Sample Input:-
Height: 135 cm
Age: 20
Wants photo: n

Sample Output:-
Final bill: 12 dollars
Input format:
The first line contains a single integer n (1 ≤ n ≤ 100), which represents
the number of Fibonacci numbers to printed .

Sample Input: 3
Sample Output: 011

Sample Input: 4
Sample Output: 0112

Sample Input: 6
Sample Output: 011234

Sample Input: 7
Sample Output: 0112345

Sample Input: 8
Sample Output: 01123456

Reverse the string


-------------------
Input format:

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


alphabets or digits. The length of the string s will be between 1 and 100
characters.
1.Sample input:Hello
SampleOutput:olleH
2.Sample Input: World
Sample Output: dlroW
3.Sample Input: OpenAI
Sample Output: IAnepO
4.Sample Input: Programming
Sample Output: gnimmargorP
5.Sample Input: 12345
Sample Output: 54321

3.
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"
Sum of odd number from 1 to n number using for loop without
predefined function
"Input format:

The first line contains two integers, a and b (1 ≤ a ≤ b ≤ 100), separated


by the word ""to"". These integers represent a range starting from a to b.

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
"
"Write a Python program that iterates through numbers from 1 to 50. The
program should:

Print only the even numbers.


Skip numbers that are multiples of 5.
Stop the loop entirely if it reaches the number 49. "
"Input format:

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

Sample Input: Range from 1 to 50


Expected Output: [2, 4, 6, 8, 12, 14, 16, 18, 22, 24, 26, 28, 32, 34, 36,
38, 42, 44, 46, 48]

Input: Modify range to 1 to 30


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

Input: Modify range to 1 to 20


Expected Output: [2, 4, 6, 8, 12, 14, 16, 18]

Input: Modify range to 1 to 10


Expected Output: [2, 4, 6, 8]

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, 44, 46, 48]"
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 even or odd.

Input: An integer n.
Output: Print ""Even"" if n is even, otherwise ""Odd"".

Test Cases:

Input: 4 → Output: Even


Input: 7 → Output: Odd
Input: 0 → Output: Even
Input: -3 → Output: Odd
Input: 100 → Output: Even
# Input
n = int(input(""Enter an integer: ""))

# Check even or odd


if n % 2 == 0:
print(""Even"")
else:
print(""Odd"")

"
"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.

Test Cases:

Input: 123 → Output: 6


Input: 0 → Output: 0
Input: 456 → Output: 15
Input: 789 → Output: 24
Input: 1023 → Output: 6

# Input
n = int(input(""Enter an integer: ""))
sum_of_digits = 0

# Calculate sum of digits


for digit in str(n):
sum_of_digits += int(digit)

print(""Sum of digits:"", sum_of_digits)


"
"Program to Skip Multiples of 3 Using Continue
Problem: Given an integer n, print all numbers from 1 to n, skipping
multiples of 3.

Input: An integer n.
Output: Print numbers from 1 to n, excluding multiples of 3.

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

for i in range(1, n + 1):


if i % 3 == 0:
continue
print(i, end="" "")
Test Cases:

Input: 10 → Output: 1 2 4 5 7 8 10
Input: 6 → Output: 1 2 4 5
Input: 15 → Output: 1 2 4 5 7 8 10 11 13 14
Input: 3 → Output: 1 2
Input: 1 → Output: 1
"
"Write a program to print the first n terms of the Fibonacci series.

Input: An integer n.
Output: Print the first n numbers in the Fibonacci series.

Test Cases:

Input: 1 → Output: 0
Input: 5 → Output: 0 1 1 2 3
Input: 7 → Output: 0 1 1 2 3 5 8
Input: 10 → Output: 0 1 1 2 3 5 8 13 21 34
Input: 0 → Output: No output

# Input
n = int(input(""Enter the number of terms: ""))
a, b = 0, 1

# Print Fibonacci series


print(""Fibonacci series:"")
for _ in range(n):
print(a, end="" "")
a, b = b, a + b
print() # for new line
"
"Write a program to calculate the factorial of a given integer.
Input: A positive integer n.
Output: Print the factorial of n.

Test Cases:

Input: 0 → Output: 1
Input: 3 → Output: 6
Input: 5 → Output: 120
Input: 7 → Output: 5040
Input: 10 → Output: 3628800

# Input
n = int(input(""Enter a positive integer: ""))
result = 1

# Calculate factorial
if n == 0:
result = 1
else:
for i in range(1, n + 1):
result *= i

print(""Factorial:"", result)
"
" Program to Calculate the Product of Digits of a Number
Problem: Given an integer n, calculate the product of its digits.
Input: An integer n.
Output: Print the product of the digits of n.

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


product = 1

for digit in str(n):


product *= int(digit)

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


Test Cases:

Input: 123 → Output: 6


Input: 0 → Output: 0
Input: 456 → Output: 120
Input: 789 → Output: 504
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

for i in range(1, n):


if n % i == 0:
sum_of_divisors += i

if sum_of_divisors == n:
print(""Perfect Number"")
else:
print(""Not a Perfect Number"")
Test Cases:

Input: 6 → Output: Perfect Number


(Explanation: The divisors of 6 are 1, 2, and 3, and their sum is 6.)

Input: 28 → Output: Perfect Number


(Explanation: The divisors of 28 are 1, 2, 4, 7, and 14, and their sum is
28.)

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: 496 → Output: Perfect Number


(Explanation: The divisors of 496 are 1, 2, 4, 8, 16, 31, 62, 124, and 248,
and their sum is 496.)
Input: 10 → Output: Not a Perfect Number"
" Program to Calculate the Sum of Square of Digits
Problem: Given an integer n, calculate the sum of the squares of its
digits.

Input: An integer n.
Output: Print the sum of the squares of the digits of n.

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


sum_of_squares = 0

for digit in str(n):


sum_of_squares += int(digit) ** 2

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

Test Cases:

Input: 12 → Output: 5

Input: 123 → Output: 14

Input: 456 → Output: 77

Input: 0 → Output: 0
Input: 99 → Output: 162"

"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"".

Test Cases:

Input: 2 → Output: Prime


Input: 10 → Output: Not Prime
Input: 17 → Output: Prime
Input: 20 → Output: Not Prime
Input: 23 → Output: Prime

# Input
n = int(input(""Enter an integer: ""))
is_prime = True

# Check for prime


if n <= 1:
is_prime = False
else:
for i in range(2, int(n**0.5) + 1):
if n % i == 0:
is_prime = False
break

if is_prime:
print(""Prime"")
else:
print(""Not Prime"")
"
"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.

Input: An integer n.
Output: Print the sum of all even numbers from 1 to n.

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


sum_even = 0

for i in range(2, n + 1, 2):


sum_even += i

print(""Sum of even numbers:"", sum_even)


Test Cases:

Input: 10 → Output: 30
Input: 5 → Output: 6
Input: 12 → Output: 42
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
sum_positive = 0

while True:
num = int(input(""Enter an integer: ""))
if num < 0:
break
sum_positive += num
count += 1

if count > 0:
average = sum_positive / count
print(""Average of positive integers:"", average)
else:
print(""No positive integers entered"")
Test Cases:

Input: 5 10 15 -1 → Output: 10.0


Input: 3 7 11 0 -5 → Output: 5.25
Input: -1 → Output: No positive integers entered
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"".

Test Cases:

Input: 153 → Output: Armstrong


Input: 370 → Output: Armstrong
Input: 123 → Output: Not Armstrong
Input: 9474 → Output: Armstrong
Input: 9475 → Output: Not Armstrong

# Input
n = int(input(""Enter a positive integer: ""))
num_str = str(n)
num_len = len(num_str)
sum_of_powers = 0

# Calculate sum of powers


for digit in num_str:
sum_of_powers += int(digit) ** num_len
if sum_of_powers == n:
print(""Armstrong"")
else:
print(""Not Armstrong"")
"
"Write a program that calculates the sum of squares of the first n natural
numbers.

Input: A positive integer n.


Output: Print the sum of squares of the first n natural numbers.

Test Cases:

Input: 1 → Output: 1
Input: 2 → Output: 5
Input: 3 → Output: 14
Input: 5 → Output: 55
Input: 10 → Output: 385

# Input
n = int(input(""Enter a positive integer: ""))
sum_of_squares = 0

# Calculate sum of squares


for i in range(1, n + 1):
sum_of_squares += i ** 2

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.
Output: Print the count of digits in n.

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


count = 0

if n == 0:
count = 1
else:
while n != 0:
count += 1
n //= 10

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


Test Cases:

Input: 123 → Output: 3


Input: 0 → Output: 1
Input: 4567 → Output: 4
Input: 78901 → Output: 5
"
"Program to Count Occurrences of a Digit in an Integer
Problem: Given an integer n and a digit d, count how many times d
appears in n.

Input: An integer n and a digit d.


Output: Print the count of digit d in n.

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


d = input(""Enter the digit to count: "")
count = 0

for digit in str(n):


if digit == d:
count += 1

print(f""The digit {d} appears {count} times in {n}"")


Test Cases:

Input: 12233, 2 → Output: 2


Input: 55555, 5 → Output: 5
Input: 789, 1 → Output: 0
Input: 101010, 0 → Output: 3
Input: 123456, 6 → Output: 1
"
"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.
Output: Print ""Positive"", ""Negative"", or ""Zero"" based on the value of
n.

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

if n > 0:
print(""Positive"")
elif n < 0:
print(""Negative"")
else:
print(""Zero"")
Test Cases:

Input: 5
Output: Positive

Input: -3
Output: Negative

Input: 0
Output: Zero

Input: 100
Output: Positive

Input: -25
Output: Negative"

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy