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 complete C++ program asking the user to enter numbers one by one. User enters...
Write a complete C++ program asking the user to enter numbers one by one. User enters only whole numbers 0 and 10. Once the user enters 0, then the program should print sum of only odd numbers entered by user prior to 0. A valid scenario given below: Enter a number: 5 Enter a number: 3 Enter a number: 2 Enter a number: 3 Enter a number: 8 Enter a number: 0 Sum of the odd numbers you entered is...
In C programming language please. -Construct a program that will make use of user defined functions,...
In C programming language please. -Construct a program that will make use of user defined functions, the do/while loop, the for loop and selection. -Using a do/while loop, your program will ask/prompt the user to enter in a positive value representing the number of values they wish to have processed by the program or a value to quit/exit. -If the user enters in a 0 or negative number the program should exit with a message to the user indicating they...
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...
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...
Binary Search Tree Code in Java Write a program that prompts user to enter however many...
Binary Search Tree Code in Java Write a program that prompts user to enter however many numbers they want to add to the binary search tree. Then have the user input numbers until the tree contains that many elements. If the user enters a number that already exists in the tree, then a message should be displayed "Number is already in the tree". Once all elements are added to the tree print its contents in sorted order. Assume all user...
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.
(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...
C - Language Write an expression that executes the loop while the user enters a number...
C - Language 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 -1. Also note: If the submitted code has an infinite loop, the system will stop running the code after a few seconds,...
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...