0% found this document useful (0 votes)
4 views6 pages

Lab Exam 2

The document contains a series of programming tasks that require writing Python programs for various functionalities. These tasks include counting even and odd digits in a number, validating hexadecimal strings, counting character frequency in a string, finding the maximum element in an array, determining the maximum and minimum of user-input numbers, and printing negative integers from an array. Each task specifies input and output formats along with examples.
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)
4 views6 pages

Lab Exam 2

The document contains a series of programming tasks that require writing Python programs for various functionalities. These tasks include counting even and odd digits in a number, validating hexadecimal strings, counting character frequency in a string, finding the maximum element in an array, determining the maximum and minimum of user-input numbers, and printing negative integers from an array. Each task specifies input and output formats along with examples.
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/ 6

(Don’t use any type of built in functions excepts for len(), range()).

1. Write a Python program that takes a number, Z and prints how many even digits are in
that number. Then print the sum of the odd digits in the number Z. For example, if Z =
5218, then there are two odd digits in Z and the sum of even digits is : 5+1=6

Input: The users will give a positive integer Z as input.


Output: In the first line print the number of odd digits. Then in the next line, print the
sum of odd digits in Z. See the below format.

Input Output

5218 Odd Digits: 2


Sum: 6

302 Odd Digits: 2


Sum: 5

81 Odd Digits: 1
Sum: 1

64 Odd Digits: 0
Sum: 0
2. Write a program that takes a string as input and checks if it is a valid hexadecimal
number. A valid hexadecimal number contains only the digits 0-9 and the letters a-f or
A-F. The program should print “A hexadecimal number” if the string meets these criteria.
Otherwise, it should print “Not a hexadecimal number”.

Input:

The input consists of a single line containing a string S (1 ≤ length(S) ≤ 1000). This string
represents the sequence of characters to be checked.

Output:
The output should be a single line. If the string S contains only valid hexadecimal digits
(0-9, a-f, A-F), print “A hexadecimal number”. Otherwise, print “Not a hexadecimal
number”.

Input Output

1a3f A hexadecimal number

G23 Not a hexadecimal number

123ABC A hexadecimal number

12x34 Not a hexadecimal number


3. Write a program that counts the frequency of each character in a given string, ignoring
case. That is, 'A' and 'a' should be considered the same character.

Input
A single line containing a string S (1 ≤ length(S) ≤ 1000). The string will contain
upper case and lower case characters, and space.

Output
For each unique character (case-insensitive) in the string, print the character followed by
a colon and its frequency, each on a new line. Don’t print the frequency of space.

Input Output

ApPle a: 1
p: 2
l: 1
e: 1

Banana b: 1
a: 3
n: 2

Hello World h: 1
e: 1
l: 3
o: 2
w: 1
r: 1
d: 1

Explanation
In the third example, the string "Hello World" includes a space, which is ignored, and
only the frequencies of the non-whitespace characters are printed.
4. You are given an array A of N integers. Find and print the maximum element in the array
and its index (0-based).

Input:
The first line of input contains one integer N (1 ≤ N ≤ 1000), indicating the number of
elements in the array.
The second line contains N integers separated by spaces.

Output:
Print the maximum element.
Print the index (0-based) of the maximum element on a new line.

Input Output

5 Max element: 5
31415 The position is: 4

3 Max element: -1
-1 -2 -3 The position is: 0

4 Max element: 9
7912 The position is: 1
5. Write a program that asks the user to enter N numbers and print the maximum and
minimum of these N numbers. Next, find the sum of the even numbers among those N
numbers. See the sample output for better understanding.

Input:
The first line of input should be an integer N (where 1<=N < 10000), indicating the total
number of values the user will input.

Following the first line, there should be N lines, each containing an integer number X
(-10^7 < X < 10^7).

Output: See the below format.

Input Output

5 Maximum number: 11
10 Minimum number: -7
11 Sum of even numbers: 18
8
-7
3

4 Maximum number: 100


100 Minimum number: -30
29 Sum of even numbers: 100
-30
3
6. You are given an array A of N integers. Print all the negative integers of the array, A. If
there is no negative integer in the array, then don’t have to print anything.
Input:
The first line of the input contains an integer N.
The second line consists of N space separated integers where the i-th integer is A[i].

Output:
Print the output in the below format.

Sample Input Sample Output


5 -3 -90
-3 2 1 -90 100
4 -21 -2
2 -21 -2 1
5
1 10 90 21 30

You might also like