Find Sum of Digits of Number Without Using Recursion: Lab#1 (Basics of Functions and Loops) 1. Time Display in Format

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

Lab#1 (Basics of Functions and Loops)

1. Time display in format


Write a program which reads hours, minutes and seconds as integer values for a meeting time.
Define a function which prints it in format “Time:hh:mm:ss”. The program should display
‘Wrong input’ if values are not in required range : hours (0-23), minutes(0-59) and seconds (0-
59).
Input 1 Input 2 Input 3 Input 4
10 25 12 25
20 30 60 30
40 30 30 65
Output 1 Output 2 Output 3 Output 4
Time:10:20:40 Wrong input Wrong input Wrong input

2. Find sum of digits of number without using recursion


Given an integer N, find the sum of digits of N without using recursion.
Write a function that accepts an integer N. The function should return the sum of N's digits.
Assume that, N is an integer within the range [0 to 1000000000].
Input 1 Input 2 Input 3 Input 4 Input 5 Input 6
121 1002345 9 0 1741725 1000000000
Output 1 Output 2 Output 3 Output 4 Output 5 Output 6
4 15 9 0 27 1

3. Winner of lottery ticket


Suppose there is a lottery ticket with an integer number N. A person wins a lottery only if the
number of even number of digits in N are 3. Write a program to input a positive lottery number
and print “winner” or ‘Better luck next time’ using a function. There should be no output for
invalid lottery number.
Input 1 Input 2 Input 3
2134 2122 -10
Output 1 Output 2 Output 3
Better luck next time Winner

4. Number of men eligible for competition


Suppose there is a competition where many male members are participating. Write a program
which inputs Age(int), Height(float), Weight(float) for n (max 3) people and finds the number
of men who satisfy the following conditions for participating in the competition:
a) Age should be greater than 18 and less than 25
b) Height should be between 5.2 to 5.6 inches
c) Weight should be between 45 to 60 kg.
Define functions to check each of the above conditions. “error” should be printed in case n is
not valid.

Input 1 Input 2 Input 3


2 4 -1
12 Output 2 Output 3
5.3 error error
44
20
5.3
45
Output 1
1

5. Count number of zeroes, positive and negative numbers using function.


Write a program to count number of zeroes, positive and negative numbers from N numbers
input by the user. Use function to check whether number is zero, positive or negative. First
input is N.
Input 1 Input 2 Input 3
3 4 3
12 -1 -12
2 -2 -40
0 0 -10
Output 1 5 Output 3
120 Output 2 0 0 3
112

You might also like