Computer Applications
In a competitive examination, a set of 'N' number of questions results in 'True' or 'False'. Write a program by using scanner class to accept the answers. Print the frequency of 'True' and 'False'.
Answer
import java.util.Scanner;
public class KboatCompetitiveExam
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("Enter number of questions: ");
int n = in.nextInt();
int trueFreq = 0, falseFreq = 0;
System.out.println("Enter answers:");
for (int i = 1; i <= n; i++) {
boolean ans = in.nextBoolean();
if (ans)
trueFreq++;
else
falseFreq++;
}
System.out.println("True Frequency = " + trueFreq);
System.out.println("False Frequency = " + falseFreq);
}
}Variable Description Table
Program Explanation
Output
Related Questions
Write a program to accept a set of 50 integers. Find and print the greatest and the smallest numbers by using scanner class method.
Write a program (using scanner class) to generate a pattern of a token in the form of a triangle or in the form of an inverted triangle depending upon the user's choice.
Sample Input/Output:
Enter your choice 1
*
* *
* * *
* * * *
* * * * *Enter your choice 2
* * * * *
* * * *
* * *
* *
*Write a program to accept a sentence in mixed case. Find the frequency of vowels in each word and print the words along with their frequencies in separate lines.
Sample Input:
We are learning scanner classSample Output:
Word Frequency of vowels
We 1
are 2
learning 3
scanner 2
class 1Write a program by using scanner class to input a sentence. Display the longest word along with the number of characters in it.
Sample Input:
We are learning scanner class in JavaSample Output:
The longest word: learning
Number of characters: 8