ENTER THE AMOUNT DEPOSITED PER MONTH:
ENTER THE TOTAL PERIOD IN MONTHS:
ENTER THE RATE OF INTEREST (PER ANNUM):
JAVA, Python, C++ and C programs for students. Here you will also find solutions to boards papers of ISC Computer Science Practical and CBSE Computer Science. ISC and ICSE JAVA PROGRAMS
ENTER THE AMOUNT DEPOSITED PER MONTH:
ENTER THE TOTAL PERIOD IN MONTHS:
ENTER THE RATE OF INTEREST (PER ANNUM):
import java.util.Scanner;
• The if statement is used for selection or decision making.
• The looping constructs while and for allow sections of code to be executed repeatedly under some condition.
• for statement iterates over a range of values or a sequence.
• The statements within the body of for loop are executed till the range of values is exhausted.
• The statements within the body of a while are executed over and over until the condition of the while is false.
• If the condition of the while loop is initially false, the body is not executed even once.
• The statements within the body of the while loop must ensure that the condition eventually becomes false; otherwise, the loop will become an infinite loop, leading to a logical error in the program.
• The break statement immediately exits a loop, skipping the rest of the loop’s body. Execution continues with the statement immediately following the body of the loop. When a continue statement is encountered, the control jumps to the beginning of the loop for the next iteration.
• A loop contained within another loop is called a nested loop.
1. What is the difference between else and elif construct of if statement?
To check or implement condition(s) in a program "if statement" is used. Depending upon the situation, single or multiple if statement can be used. If there is only two possibilities ( either a number is even or odd) if else can be used. The else construct will come into play if the "if" construct condition fails: For example,
if n is even:
workings
else: # if n is not even then it must be odd
workings
But if there are multiple possibilities ( remainder on dividing by 5 ) then instead of using multiple "if" statement, elif construct is used. It allows to check the next block of "elif" construct if the current "if" or "elif" condition fails. There can be only one else statement based on a particular if statement, while there can be multiple elif statement for a particular elif statement.
if n%5 == 1:
n = 5k+1
elif n%5 == 2
n = 5k+2
elif n%5 == 3
n = 5k+3
elif n%5 == 4
n = 5k+4
else
n is divisible by 5
2. What is the purpose of range() function? Give one example.
3. Differentiate between break and continue statements using examples.
4. What is an infinite loop? Give one example.
5. Find the output of the following program segments:
(i) a = 110
while a > 100:
print(a)
a -= 2
OUTPUT:
110
108
106
104
102
(ii) for i in range(20,30,2):
print(i)
OUTPUT:
20
22
24
26
28
(iii) country = 'INDIA'
for i in country:
print (i)
OUTPUT:
I
N
D
I
A
(iv)
OUTPUT: 12
(v)
OUTPUT:
2
3
4
4
6
8
6
9
(vi)
OUTPUT:
Current variable value: 7
Current variable value: 5
Current variable value: 4
Good bye!
1. Write a program that takes the name and age of the user as input and displays a message whether the user is eligible to apply for a driving license or not.
(the eligible age is 18 years).
2. Write a function to print the table of a given number. The number has to be entered by the user.
3. Write a program that prints minimum and maximum of five numbers entered by the user.
4. Write a program to check if the year entered by the user is a leap year or not.
5. Write a program to generate the sequence: –5, 10,–15, 20, –25…….. up to n, where n is an integer input by the user.
6. Write a program to find the sum of 1+ 1/8 +1/27......1/n3, where n is the number input by the user.
7. Write a program to find the sum of digits of an integer number, input by the user.
8. Write a function that checks whether an input number is a palindrome or not.
[Note: A number or a string is called palindrome if it appears same when written in reverse order also. For example, 12321 is a palindrome while 123421 is not a palindrome]
9. Write a program to print the following patterns:
I have modified the program a bit.
10. Write a program to find the grade of a student when grades are allocated as given in the table below.
Percentage of Marks Grade
Above 90% A
80% to 90% B
70% to 80% C
60% to 70% D
Below 60% E
Percentage of the marks obtained by the student is input to the program.
Case Study-based Questions
Let us add more functionality to our SMIS developed in Chapter 5.
6.1 Write a menu driven program that has options to
• accept the marks of the student in five major subjects in Class X and display the same.
• calculate the sum of the marks of all subjects.
Divide the total marks by number of subjects (i.e. 5), calculate percentage = total marks/5
and display the percentage.
• Find the grade of the student as per the following criteria:
Criteria Grade
percentage > 85 A
percentage < 85 && percentage >= 75 B
percentage < 75 && percentage >= 50 C
percentage > 30 && percentage <= 50 D
percentage <30 Reappear
Let’s peer review the case studies of others based on the parameters given under “DOCUMENTATION TIPS” at the end of Chapter 5 and provide a feedback to them.
Programming for school students ( upper to middle/secondary school) PYTHON AND JAVA PROGRAMS
Design a Railway Ticket Class with the following description:
Instance Variables/Data Members:
String name : To store the name of the customer
String coach : To store the type of coach customer wants to travel
long mobno : To store customer’s mobile number
int amt : To store basic amount of ticket
int totalamt : To store the amount to be paid after updating the original amount
Member methods :
void accept () – To take input for name, coach, mobile number and amount.
void update() – To update the amount as per the coach selected
(extra amount to be added in the amount as follows)
Type of Coaches Amount
First_AC 700
Second_AC 500
Third_AC 250
sleeper None
void display() – To display all details of a customer such as name, coach, total amount and mobile number.
Write a main method to create an object of the class and call the above member
methods.