Question

This program is for kindergartners to practice knowledge on numbers divisible by 5. Write a code...

This program is for kindergartners to practice knowledge on numbers divisible by 5.

Write a code using a while loop, which keeps asking the user to enter an number divisible by 5. The program(i.e the while loop) terminates when the user enters a number divisible by 5, but keeps running if the user fails to enter a number divisible by 5. Also print appropriate message after each failed try. When successful, tell the user how many attempts were needed for success.

Homework Answers

Answer #1

The program will ask the user to input numbers till the input number is divisible by 5. The program will show a message after each invalid input. After a succesful input, the number of attempts will be also printed by the program. Since the program has to be implemented using while loop, the first input should be normal, and the remaining inputs(if any) should go through iteration using while loop.

The algorithm of the program would be:

Step 1: Ask the first number from the user normally.

Step 2: In case of invalid number, show an error message.

Step 3: Start a while loop which will check if the first number is divisible by 5 or not. If the number is not divisible by 5, start a loop that will ask the user for a valid number.

Step 4: After each input, increase the count which should be initialized to zero. This will track the number of attempts.

Step 5: Lastly print the valid number and number of attempts when the program exits from the loop due to valid input.

The required program in C language is given below:

#include<stdio.h>       //import basic package

int main()               //main method
{
   int n,count=0;
  
   //code for first input
   printf("Enter a number divisible by 5 : ");
   scanf("%d",&n);       //requesting the user for a number
   count++;           //counting number of attempts
   if(n%5!=0)           //error message in case of invalid input
       printf("\nPlease enter a number which is divisible by 5.\n\n");
      
   //code for remaining inputs(if any) using while loop
   while(n%5!=0)       //the loop executes till valid input is given
   {
       printf("Enter a number divisible by 5 : ");
       scanf("%d",&n);       //requesting the user for a number
       count++;           //counting number of attempts
       if(n%5!=0)           //error message in case of invalid input
           printf("\nPlease enter a number which is divisible by 5.\n\n");
   }
  
   printf("\n................................\n");
   printf("Entered valid number: %d\n",n);       //printing the valid input
   printf("Number of attempts: %d",count);       //printing the number of attempts
   return 0;
}


Screenshot of the code:


Output:


NOTE: Since the language is not mentioned, the solution is given in some other languages below.

The required program in C++ is given below:

#include<iostream>       //import basic package
using namespace std;

int main()               //main method
{
   int n,count=0;
  
   //code for first input
   cout<<"Enter a number divisible by 5 : ";
   cin>>n;       //requesting the user for a number
   count++;           //counting number of attempts
   if(n%5!=0)           //error message in case of invalid input
       cout<<"\nPlease enter a number which is divisible by 5.\n\n";
      
   //code for remaining inputs(if any) using while loop
   while(n%5!=0)       //the loop executes till valid input is given
   {
       cout<<"Enter a number divisible by 5 : ";
       cin>>n;       //requesting the user for a number
       count++;           //counting number of attempts
       if(n%5!=0)           //error message in case of invalid input
           cout<<"\nPlease enter a number which is divisible by 5.\n\n";
   }
  
   cout<<"\n................................\n";
   cout<<"Entered valid number: "<<n;       //printing the valid input
   cout<<"\nNumber of attempts: "<<count;       //printing the number of attempts
   return 0;
}

Screenshot of the code:

Output:

The required solution in JAVA is given below:

import java.util.*;         //import basic package

public class Main           //defining a class
{
   public static void main(String[] args)      //main method
   {
        int n,count=0;
        Scanner sc=new Scanner(System.in);
      
        //code for first input
        System.out.printf("Enter a number divisible by 5 : ");
        n=sc.nextInt();       //requesting the user for a number
        count++;           //counting number of attempts
        if(n%5!=0)           //error message in case of invalid input
            System.out.println("\nPlease enter a number which is divisible by 5.\n\n");
      
        //code for remaining inputs(if any) using while loop
        while(n%5!=0)       //the loop executes till valid input is given
        {
            System.out.printf("Enter a number divisible by 5 : ");
            n=sc.nextInt();       //requesting the user for a number
            count++;           //counting number of attempts
            if(n%5!=0)           //error message in case of invalid input
                System.out.printf("\nPlease enter a number which is divisible by 5.\n\n");
        }
  
        System.out.println("\n................................\n");
        System.out.println("Entered valid number: "+n);       //printing the valid input
        System.out.println("Number of attempts: "+count);       //printing the number of attempts
   }
}

Screenshot of the code:

Output:

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Write a program in C++ coding that asks the user to input an integer between 25...
Write a program in C++ coding that asks the user to input an integer between 25 and 50, inclusive. Utilize a WHILE loop to test for INVALID input. If the input is INVALID, the loop will repeat, and ask the user to try again. The pseudocode looks like this: Prompt user to input an integer between 25 and 50 Accept the input from the user Test for invalid input (HINT: use a while loop and the OR operator with 2...
Write a program that asks the user to input an integer between 25 and 50, inclusive....
Write a program that asks the user to input an integer between 25 and 50, inclusive. Utilize a WHILE loop to test for INVALID input. If the input is INVALID, the loop will repeat, and ask the user to try again. The pseudocode looks like this: Prompt user to input an integer between 25 and 50 Accept the input from the user Test for invalid input (HINT: use a while loop and the OR operator with 2 relational expressions. You...
(8 marks) Write a program to ask user to input an integer and display the special...
Write a program to ask user to input an integer and display the special pattern accordingly. REQUIREMENTS The user input is always correct (input verification is not required). Your code must use loop statements (for, while or do-while). Your program should use only the following 3 output statements, one of EACH of the followings: System.out.print("-"); // print # System.out.print("+"); // print + System.out.println(); // print a newline Your code must work exactly like the following example (the text in bold...
4.2.2 Basic while loop with user input. JAVA Write an expression that executes the loop while...
4.2.2 Basic while loop with user input. JAVA Write an expression that executes the loop while the user enters a number greater than or equal to 0. Note: These activities may test code with different test values. This activity will perform three tests, with user input of 9, 5, 2, -1, then with user input of 0, -17, then with user input of 0 1 0 -1. See "How to Use zyBooks". Also note: If the submitted code has an...
The Java program should sum the odd integers from 1 to the integer that the user...
The Java program should sum the odd integers from 1 to the integer that the user enters. For example, if the user enters 30, the program should sum the odd integers from 1 to 30.   Print "Please enter a positive nonzero integer." Declare a Scanner and obtain an integer from the user.   Use a Do...while statement and calculate the sum Use the integer that the obtained from the previous step for the loop-continuation condition. Display the sum Print the sum...
Draw a flowchart based on the Python code below: ch = -1 #Variable declared for taking...
Draw a flowchart based on the Python code below: ch = -1 #Variable declared for taking option entered by the user print("Enter 0 to 5 for following options: ") print("0-> Issue new ticket number.") print("1-> Assign first ticket in queue to counter 1.") print("2-> Assign first ticket in queue to counter 2.") print("3-> Assign first ticket in queue to counter 3.") print("4-> Assign first ticket in queue to counter 4.") print("5-> Quit Program.") tickets = [] #Declaring list to store...
3.plese to write the correct code: The code below will loop as long as the number...
3.plese to write the correct code: The code below will loop as long as the number that you enter isn’t negative. It will add each non negative number to the current sum. It will print out the sum and average of all of the numbers you have entered. But someone jumbled up the code. Can you reorganize and indent it so it works properly? count = count + 1 message = "Enter an integer or a negative number to stop"...
Data Encryption (Strings and Bitwise Operators) Write a C program that uses bitwise operators (e.g. bitwise...
Data Encryption (Strings and Bitwise Operators) Write a C program that uses bitwise operators (e.g. bitwise XOR) to encrypt/decrypt a message. The program will prompt the user to select one of the following menu options: 1. Enter and encrypt a message 2. View encrypted message 3. Decrypt and view the message (NOTE: password protected) 4. Exit If the user selects option 1, he/she will be prompted to enter a message (a string up to 50 characters long). The program will...
Write a program with while loop that prints all numbers between 5 and 100 (inclusive) that...
Write a program with while loop that prints all numbers between 5 and 100 (inclusive) that are divisible by 5.
Collapse Write a program that prompts the user to input a positive integer. It should then...
Collapse Write a program that prompts the user to input a positive integer. It should then output a message indicating whether the number is a prime number. (Note: An even number is prime if it is 2. An odd integer is prime if it is not divisible by any odd integer less than or equal to the square root of the number.) Turn in: Your source code for with The name of your program as a comment at the top...