write a java bluej program to accept a sentence. Display the longest work also the length of the longest word of the sentence icse class 10
Answers
import java.util.Scanner;
public class FiftyPrimes
{
// Return true if a number n is prime
public static boolean isPrime(long n)
{
// Check division from 2 to sqrt(n)
for (long i = 2; i <= Math.sqrt(n); i++)
{
if (n % i == 0)
{
return false;
}
}
// If no division is found, number is prime
return true;
}
public static void main(String[] args)
{
// Create Scanner object
Scanner sc = new Scanner(System.in);
// Take user input
System.out.print("Enter the starting point: ");
long start = sc.nextInt();
// Close Scanner object
sc.close();
// If start point is less than 2, make it 2
if (start < 2)
{
start = 2;
}
int numberOfPrimes = 0; // Number of primes printed
long number = start; // Number to be tested for prime
// Iterate until 50 primes are printed
while (numberOfPrimes < 50)
{
if (isPrime(number))
{
System.out.println(number);
numberOfPrimes++;
}
number++;
}
}
}
Answer:
import java.util.*;
class Q5{
public static void main(String args[])
{
Scanner in=new Scanner(System.in);
System.out.println("Enter a sentence");
String n=in.nextLine();
n=n+" ";
String a="",max="";
int l=n.length();
char ch;
int b,m=0;
for(int i=0;i<l;i++)
{
ch=n.charAt(i);
if(ch!=' ')
{
a=a+ch;
}
else
{
b=a.length();
if(b>m)
{
m=b;
max=a;
}
a="";
}
}
System.out.println("The longest word: "+max+". The length is: "+m);
}
}
Explanation:
Enter a sentence
tata football academy will play against mohan began
The longest word: football. The length is: 8