Python,C,C++ and JAVA programs for CBSE, ISC, B.Tech and I.T Computer Science and MCA students

The Programming Project: Python program to enter 10 numbers in list and how many of them are ending with 5: Python

Thursday, September 30, 2021

Python program to enter 10 numbers in list and how many of them are ending with 5: Python

PYTHON PROGRASM FOR BEGINNERS

SCHOOL LEVEL PROGRAMMING

Write a Python program to enter 10 numbers in list and how many of them are ending with 5. Display all the numbers ending with 5 and the total of such numbers. If there are no numbers ending with 5 display "NIL".

Both Java and Python codes are given below.

In this program you will learn:

i. Taking input

ii. Adding numbers to list using append() method

iii. for loop

iv. using boolean variables

v. using if statements

PYTHON CODE

#Python program to enter 10 numbers in list and how many of them are ending with 5

numberList = []
for i in range(10):
    print("Enter the element at the position: ",i+1)
    numberList.append(int(input("Enter the value:")))
counter = 0
flagEndingFive = False
print("Numbers ending with 5 is/are:")
for i in numberList:
    if i%10 == 5:
        counter +=1
        flagEndingFive = True
        print(i)
if flagEndingFive == False:
    print("NIL")
print("Total number of numbers ending with 5 = ",counter)


JAVA CODE

import java.util.Scanner;
public class NumbersEndingFive{
       public static void main(String[] args) {
        int[] numberList;
        boolean flagEndingFive = false;
        int counter = 0;
        Scanner in = new Scanner(System.in);
        numberList = new int[10];
        for(int i = 0i10i++){
               System.out.println("Enter the element at the position:"+(i+1));
               numberList[i] = in.nextInt();
       }
       System.out.println("Numbers ending with 5 is/are:");
       for(int i = 0i10i++) 
              if(numberList[i]%10 == 5) {
                     flagEndingFive = true;
                     counter++;
                     System.out.println(numberList[i]);
              }
       if(!flagEndingFive)
              System.out.println("NIL");
       System.out.println("Total number of numbers ending with 5 = "+counter);
       in.close();
       }
}

No comments:

Post a Comment