writewrite a program to input 10 numbers into an integer array and check whether all the numbers are three digit numbers or not also do the Java program by using scanner class
Answers
Answer:
Program:-
//Program using Scanner class
import java.util.*;
public class Brainly_Answer
{
public static void main(String args[ ])
{
Scanner in=new Scanner(System.in);
int c=0;
System.out.println("Enter the elements");
int a[ ]=new int[10];
for(int i=0;i<a.length;i++)
{
a[i]=in.nextInt();
}
for(int i=0;i<a.length;i++)
{
if(a[i]>=100&&a[i]<1000)
c++;
}
if(c==10)
System.out.println("All the numbers are three digit numbers");
else
System.out.println("All the numbers are not three digit numbers");
}
}
- The first photo is the program
- The second photo is the input I entered all three digit numbers
- The third photo is the output for the input of all three digit numbers
- The fourth photo I entered all randomly added numbers input
- The fifth photo is the output for all random input numbers
Logic:-
- Logic is simple first we create an array of size 10
- We now enter the elements using a for loop
- We now run the same for loop and check the condition
- If it is true it iterates c
- If c==10 I.e the length you may also write if c==a.length it prints all numbers as three digit numbers else prints all numbers are not three digit numbers.
Answer:
Explanation:
import java.util.*;
class threeDigit
{
static void main()
{
Scanner sc=new Scanner(System.in);
int a[]=new int[10],i;
boolean f=true;
System.out.println(“Enter 10 numbers:”);
for(i=0;i<10;i++)
a[i]=sc.nextInt();
for(i=0;i<10;i++)
{
if(!(a[i]>=100 && a[i]<=999))
f=false;
}
if(f)
System.out.println(“All are 3 digit numbers”);
else
System.out.println(“All are not 3 digit numbers”);
}
}