ICSE Computer Applications - Trish Badhwar
ICSE Computer Applications - Trish Badhwar
ANSWER:
import java.util.Scanner;
public class gg
{
public static void main ()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter Your Name");
String name=sc.next();
System.out.println("Your Name:" + name);
}
}
OUTPUT:
2. Write a java program using a for loop to print your name 10 times.
ANSWER:
import java.util.*;
public class name
{
public static void useless()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter Your Name;");
String name=sc.next();
for(int x=0; x<10; x++)
{
System.out.println(name);
}
}
}
OUTPUT:
3. Write a Java program using a for loop to print the numbers from 1 to 10.
ANSWER:
import java.util.*;
public class loop2
{
public static void loop()
{
Scanner sc=new Scanner(System.in);
for(int x=1; x<=10; x++)
{
System.out.println(x);
}
}
}
OUTPUT:
4. . Write a Java program using a for loop to print the even numbers between 1 and 20.
ANSWER:
import java.util.*;
public class loop3
{
public static void loop()
{
Scanner sc=new Scanner(System.in);
for(int x=2; x<=20; x=x+2)
{
System.out.println(x);
}
}
}
OUTPUT:
5. Write a Java program using a for loop to print the multiplication table of 5 up to 10.
ANSWER:
import java.util.*;
public class loop4
{
public static void loop()
{
Scanner sc=new Scanner(System.in);
for(int x=5; x<=50; x=x+5)
{
System.out.println(x);
}
}
}
OUTPUT:
ANSWER:
import java.util.*;
public class area
{
public static void main ()
{
Scanner sc= new Scanner(System.in);
System.out.println("Enter The Length");
float L=sc.nextFloat();
System.out.println("Enter The Breadth");
float B=sc.nextFloat();
System.out.println("Area:" + L*B);
System.out.println("Perimeter:" + 2*(L+B) );
OUTPUT:
7. Write a program that accepts three numbers and prints the highest one. What happens if all the numbers
are equal?
ANSWERS:
public class Largest_No
{
public static void main (int n1, int n2, int n3)
{
if (n1>n2 && n1>n3)
System.out.println(n1+" is the largest number");
else if (n2>n1 && n2>n3)
System.out.println(n2+" is the largest number");
else if (n3>n1 && n3>n2)
System.out.println(n3+" is the largest number");
else if (n1==n2 && n2==n3)
System.out.println("All the numbers are equal");
else
System.out.println("The largest numbers are equal");
}
}
OUTPUT:
ANSWER:
import java.util.Scanner;
public class Neg_Pos_Or_Zero
{
public static void main ()
{
Scanner sc=new Scanner(System.in);
System.out.print("Enter a number");
float num= sc.nextFloat();
if (num>0.0)
System.out.println("It is a positive number");
else if (num<0.0)
System.out.println("It is a negative number");
else
System.out.print ("The number is 0");
}
OUTPUT:
9. Write a program to check if a triangle is valid (the sum of the three angles is 180 degrees).
ANSWER:
import java.util.Scanner;
OUTPUT:
ANSWER:
import java.util.Scanner;
public class tickets
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter how many tickets you would like to buy:");
int ticketno=sc.nextInt();
if (ticketno>0 && ticketno<10)
{
System.out.println("You have to pay:$"+(ticketno*20));
}
else if (ticketno>9 && ticketno<20)
{
System.out.println("You have to pay:$"+(ticketno*20)*0.9);
}
else if (ticketno>19 && ticketno<26)
{
System.out.println("You have to pay:$"+(ticketno*20)*0.8);
}
else if (ticketno<0)
{
System.out.println("ERROR:WRONG ENTRY");
}
else
{System.out.println("You cannot purchase more than 25 tickets at once");
}
}
}
OUTPUT:
11. Write a program to accept a date from the user (d, m, and y). Assume it is a valid date entered, then print
out the next date.
ANSWER:
import java.util.Scanner;
public class NextDate {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the day (1-31):");
int day = scanner.nextInt();
System.out.println("Enter the month (1-12):");
int month = scanner.nextInt();
System.out.println("Enter the year:");
int year = scanner.nextInt();
int nextDay, nextMonth, nextYear;
int daysInMonth;
if (month == 4 || month == 6 || month == 9 || month == 11) {
daysInMonth = 30;
} else if (month == 2) {
if (year % 4 == 0 ) {
daysInMonth = 29;
} else {
daysInMonth = 28;
}
} else {
daysInMonth = 31;
}
if (day < daysInMonth) {
nextDay = day + 1;
nextMonth = month;
nextYear = year;
} else {
nextDay = 1;
if (month < 12) {
nextMonth = month + 1;
nextYear = year;
} else {
nextMonth = 1;
nextYear = year + 1;
}
}
System.out.println("Today's date: " + day + "/" + month + "/" + year);
System.out.println("Next date: " + nextDay + "/" + nextMonth + "/" + nextYear);
}
}
OUTPUT:
12. Write a Java program that prompts the user to input their age and determines whether they are eligible to
vote or not. If the age is 18 or above, the user is eligible to vote; otherwise, they are not eligible.
ANSWER:
import java.util.*;
public class loop4
{
public static void loop()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter Your Age");
int Age=sc.nextInt();
if(Age>18)
System.out.println("Eligible");
else
System.out.println("Non-Eligible");
}
}
OUTPUT:
13. Write a program in Java that takes input for marks in three subjects, calculates the total, average, and
evaluates the result based on the following criteria: if the average is greater than 90, it is considered a
Distinction; if the average is greater than 75, it is considered a Merit; otherwise, it is considered a Pass.
ANSWER:
import java.util.*;
public class loop4
{
public static void loop()
{
Scanner sc=new Scanner(System.in);
System.out.println("Marks in Subject 1");
int mark1=sc.nextInt();
System.out.println("Marks in Subject 2");
int mark2=sc.nextInt();
System.out.println("Marks in Subject 3");
int mark3=sc.nextInt();
int total=mark1+mark2+mark3;
double average=total/3;
if(average>90)
System.out.println("Distinction");
else if(average>75 && average<90)
System.out.println("Merit");
else
System.out.println("Pass");
}
}
OUTPUT:
Accept the name and the price of the item using the methods of Scanner class.
To calculate the net amount to be paid by a customer, based on the following
criteria:
Price Discount
Price Discount
1000-25000 5.0%
25001-57000 7.5 %
57001-100000 10.0%
Display the name of the item and the net amount to be paid.
ANSWER:
import java.util.Scanner;//step1
public class Eshop
{
public static void main()
{
Scanner sc = new Scanner(System.in);//step2
System.out.println("Enter the item's name ");
String name=sc.next();
System.out.println("Enter the item's price ");
double price=sc.nextDouble();
OUTPUT:
15. Using Arrays Create The Class Average
ANSWER:
import java.util.*;
public class studentresults
{
public static void main()
{
Scanner trish=new Scanner(System.in);
OUTPUT:
16. There are 5 students in a class. Accept their marks and calculate the class average.
ANSWER:
import java.util.*;
public class ggg
{
public static void main ()
{
Scanner trish=new Scanner(System.in);
int marks,total = 0;
for(int i=1;i<=5;i++)
{
System.out.println("Enter marks of student "+i);
marks=trish.nextInt();
total= total+marks;
}
System.out.println("Class Avg; "+total/5);
}
}
OUTPUT:
ANSWER:
import java.util.*;
public class Eshop
{
String name;
double price;
void accept()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the item name:");
name=sc.nextLine();
System.out.println("Enter item price");
price=sc.nextDouble();
}
void calculate()
{
if(price>=1000 && price<=25000)
{
price=price*0.95;
}
else if(price>25000 && price<=57000)
{
price=price*0.925;
}
else if(price>=57000 && price<=100000)
{
price=price*0.9;
}
else if(price>=100000)
{
price=price*0.85;
}
else{
System.out.println("Error");
}
}
void display()
{
System.out.println("Item name:"+name);
System.out.println("Payable Amount:"+price);
}
public static void main()
{
Eshop shop=new Eshop();
shop.accept();
shop.calculate();
shop.display(); }}
OUTPUT:
18. Design a class with the following specifications:
Class name: Student
Member variables:
name — name of student
age — age of student
mks — marks obtained
stream — stream allocated
(Declare the variables using appropriate data types)
Member methods:
void accept() — Accept name, age, and marks using methods of Scanner class.
void allocation() — Allocate the stream as per the following criteria on marks obtained:
void print() – Display student name, age, mks, and stream allocated.
Call all the above methods in the main method using an object.
ANSWER:
import java.util.*;
String name;
int age;
double mks;
String stream;
void Accept()
System.out.println("Enter Name!");
name = sc.next();
System.out.println("Enter Age!");
age = sc.nextInt();
System.out.println("Enter Marks");
mks = sc.nextDouble();
void Calculate()
stream="Try Again";
void display()
System.out.println("Name:" + name);
System.out.println("Age:"+ age);
System.out.println("Marks:" + mks);
System.out.println("Stream:" + stream);
obj.Accept();
obj.Calculate();
obj.display();
}
OUTPUT:
1. Data Input:
- Accept `n` names and corresponding marks of students enrolled in a Computer Science course.
- Store the names in one one-dimensional array and the marks in a parallel one-dimensional array.
2. Grade Assignment:
- Based on the marks obtained, assign grades to the students and store these in a separate array named `Grades[]`
according to the following criteria:
- Distinction : Marks 75 and above.
- Merit : Marks between 60 and 74.
- Pass : Marks between 40 and 59.
- Fail : Marks below 40.
3. Output:
- Display each student’s name, marks, and corresponding grade in the format:
- "Anuj scored 95 and a Distinction."
4. Additional Outputs:
- Display the names of students who achieved distinctions.
- Display the name(s) of the highest scorer(s).
- Display the name(s) of students who failed the exam.
Note: Ensure the program is well-structured and easy to read, with appropriate comments and clear output
formatting.
ANSWER:
import java.util.*;
public class arrays
{
public static void main()
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number of students: ");
int n = sc.nextInt();//to accept the strength of the class
String names[] = new String [n];//array for storing the names
int marks[] = new int [n];//array for storing the marks
int highest = 0;//variable for storing the highest marks
int lowest = 100;//variable for storing the lowest marks
String highests = "";//variable for storing the name of the highest scorer
String gas[] = new String [n];//for storing the grade assignments of each student
for (int i = 0; i<n; i++)//this loop takes the inputs
{
System.out.println("Enter the name of the student: ");
names[i] = sc.next();
System.out.println("Enter the marks of the student: ");
marks[i] = sc.nextInt();
if (marks[i]>highest)//checks if the current students marks is the highest
{
highest = marks[i];//if yes the value of highest gets changed
highests = names[i];//stores the name of the highest scorer
}
if (marks[i]<lowest)
lowest = marks [i];
}
for (int t = 0; t<n; t++)//this loop assigns the grade assignment
{
if (marks[t]>74)
gas[t] = "Distinction";
else if (marks[t]>59&&marks[t]<75)
gas [t] = "Merit";
else if (marks[t]>39&&marks[t]<60)
gas [t] = "Pass";
else
gas [t] = "Fail";
}
for(int w = 0; w<n; w++)//this loop prints the required output
{
System.out.println(names[w]+" scored "+marks[w]+" and a "+gas[w]);
}
System.out.println("Highest scorer is: "+highests);
System.out.println("Students who failed: ");
for (int e = 0; e<n; e++)
{
if (gas[e]=="Fail")
System.out.println(names[e]);
}
}
}
OUTPUT:
20. Write a Java program that performs the following tasks:
1. Data Input and Calculations:
- Accept the input for n students enrolled in a Computer Science course, including each student's name and marks
for two papers: theory and practical (each out of 100).
- Store the students' names in a one-dimensional array called Names[ ] .
- Store the marks for the theory and practical papers in two separate parallel one-dimensional arrays:
TheoryMarks[ ] and PracticalMarks[ ].
- Calculate the total marks and the average marks for each student and store these values in two different arrays:
StudentTotal[ ] and StudentAverage[ ].
2. Grade Assignment:
- Based on the average marks obtained, assign grades to the students and store these in an array named Grades[ ] ,
using the following criteria:
- Distinction: Average marks of 75 and above.
- Merit: Average marks between 60 and 74.
- Pass: Average marks between 40 and 59.
- Fail: Average marks below 40.
3. Output:
- Display each student’s name, total marks, average marks, and the corresponding grade in the following format:
- Example: "Anuj scored a total of 190 marks, an average of 95%, and a Distinction."
4. Additional Outputs:
- Display the names of students who scored above the class average.
- Display the names of students who achieved a Distinction.
- Display the name(s) of the highest scorer(s).
- Count and display the number of students in each grade category (Distinction, Merit, Pass, Fail).
Note: Ensure that the program is well-organised, with clear and readable code, appropriate comments, and neatly
formatted output.
ANSWER:
import java.util.*;
public class comp{
public static void main(){
Scanner sc = new Scanner(System.in);
System.out.println("Enter The Number Of Students enrolled in the course");
int n = sc.nextInt();
String Name[]= new String[n];
String Grade[]= new String[n];
int TheoryMarks[]= new int[n];
int PracticalMarks[]= new int[n];
int StudentTotal[]= new int[n];
int StudentAverage[]= new int[n];
int ClsAvg=0;
int x=0;
int h=0;
int a=0;
int b=0;
int c=0;
int d=0;
for(int i=0; i<n;i++){
System.out.println("Enter Student's Name");
Name[i]=sc.next();
System.out.println("Enter Student's Theory Paper Marks out of 100");
TheoryMarks[i]= sc.nextInt();
System.out.println("Enter Student's Practical Paper Marks out of 100");
PracticalMarks[i]=sc.nextInt();
StudentTotal[i]=TheoryMarks[i]+PracticalMarks[i];
StudentAverage[i]=StudentTotal[i]/2;
if(StudentAverage[i]>h){
h=StudentAverage[i];
}
if(StudentAverage[i]>=75){
Grade[i]="Distinction";
a=a+1;
}
else if(StudentAverage[i]>=60 && StudentAverage[i]<75){
Grade[i]="Merit";
b=b+1;
}
else if(StudentAverage[i]>=40 && StudentAverage[i]<60){
Grade[i]="Pass";
c=c+1;
}
else if(StudentAverage[i]<40){
Grade[i]="Fail";
d=d+1;
}
x=x+StudentAverage[i];
}
ClsAvg=x/n;
for(int i=0;i<n;i++){
System.out.println(Name[i]+" scored a total of "+StudentTotal[i]+" marks, an average of
"+StudentAverage[i]+"%, and a "+Grade[i]);
}
System.out.println("Student/s who scored above the class average");
for(int i=0;i<n;i++){
if(StudentAverage[i]>=ClsAvg){
System.out.print(Name[i]);
}
System.out.println();
}
System.out.println("Distinction was Achieved by");
for(int i=0;i<n;i++){
if(StudentAverage[i]>=75){
System.out.print(Name[i]);
}
System.out.println();
}
System.out.println("Student/s who scored the highest");
for(int i=0;i<n;i++){
if(StudentAverage[i]==h){
System.out.println(Name[i]);
}
}
System.out.println("Distinction was Achieved by "+a+" Students");
System.out.println("Merit was Achieved by "+b+" Students");
System.out.println("Student's who got Pass "+c);
System.out.println("Student/s who failed "+d);
}
}
OUTPUT: