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.
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:
Get Answers For Free
Most questions answered within 1 hours.