0% found this document useful (0 votes)
6 views13 pages

Counter Program (Solved)

The document outlines multiple Java programs designed to perform various tasks such as counting numbers, calculating averages, and processing employee data. Each program includes a class definition, a main method, and logic to handle user input and calculations. The tasks range from simple arithmetic operations to more complex data handling for employee and student records.

Uploaded by

Piyush Sarswat
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views13 pages

Counter Program (Solved)

The document outlines multiple Java programs designed to perform various tasks such as counting numbers, calculating averages, and processing employee data. Each program includes a class definition, a main method, and logic to handle user input and calculations. The tasks range from simple arithmetic operations to more complex data handling for employee and student records.

Uploaded by

Piyush Sarswat
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Counter Program

1. Design a class to enter 10 numbers. Calculate and print the following:


(a) count of odd numbers
(b) count of even numbers
(c) count of multiple of 5 and 6

import [Link].*;
class counter1
{
public static void main()
{
Scanner sc=new Scanner([Link]);
int N,ev=0,od=0,ml=0;
for(int i=1;i<=10;i++)
{
[Link]("Enter any number");
N=[Link]();
if(N%2==0)
++ev;
if(N%2==1)
++od;
if(N%5==0 && N%6==0)
++ml;
}
[Link]("Total number of Even = "+ev);
[Link]("Total number of Odd = "+od);
}
}
2. Design a class to enter 20 alphabets. Calculate and print the following:
(a) Count of vowels
(b) count of consonants

import [Link].*;
class counter2
{
public static void main()
{
Scanner sc=new Scanner([Link]);
char ch;
int vw=0,cs=0;
for(int i=1;i<=20;i++)
{
[Link]("Enter any character");
ch=[Link]().charAt(0);
if(ch=='A'||ch=='E'||ch=='I'||ch=='O'||ch=='U')
++vw;
else
++cs;
}
[Link]("No. of vowels = "+vs);
[Link]("[Link] consonants = "+cs);
}
}
3. Design a class to enter 10 numbers. Calculate and print the following:
(a) Sum of all odd numbers
(b) sum of all even numbers
(c) Average of all the odd numbers
(d) Average of all the even numbers

import [Link].*;
class counter3
{
public static void main()
{
Scanner sc=new Scanner([Link]);
int N,Esum=0,Osum=0,ev=0,od=0;
double Eavg,Oavg;
for(int i=1;i<=10;i++)
{
[Link]("Enter any number");
N=[Link]();

if(N%2==0)
{
++ev;
Esum=Esum+N;
}
else
{
++od;
Osum=Osum+N;
}
}
Eavg=(double)Esum/ev;
Oavg=(double)Osum/od;

[Link]("Sum of even number = "+Esum);


[Link]("Sum of odd number = "+Osum);
[Link]("Average of even number = "+Eavg);
[Link]("Average of odd number = "+Oavg);
}
}

4. Design a class to enter a name, gender and salary of 50 employee. Calculate and print
the following:
(a) No. Of male employee
(b) No. Of female employee
(c) Average salary of all employee
(d) Print all information of every employee

import [Link].*;
class counter4
{
public static void main()
{
Scanner sc=new Scanner([Link]);
int mEmp=0,fEmp=0;
char gender;
double salary,TotalSalry=0.0,AvgSalary;
for(int i=1;i<=50;i++)
{
[Link]("Enter the salary");
salary=[Link]();
[Link]("Enter the gender");
gender=[Link](0);

if(gender=='M' ||gender=='m')
++mEmp;
else if(gender=='F' ||gender=='f')
++fEmp;

TotalSalary=TotalSalary+salary;

[Link]("Gender of employee = "+gender);


[Link]("Salary of employee = "+salary);
}
AvgSalary=TotalSalary/50;
[Link]("No. of male employee = "+mEmp);
[Link]("No. of female employee = "+fEmp);
[Link]("Average salary = "+AvgSalary);
}
}
5. Design a class to enter the product number, quantity and rate of 30 products.
Calculate and print the product number and the cost of all the products and print
the total bill.

import [Link].*;
class counter5
{
public static void main()
{
Scanner sc=new Scanner([Link]);
int Product_No,Qty;
double rate,cost,Total_Cost=0.0;
for(int i=1;i<=30;i++)
{
[Link]("Enter the product number");
product_No=[Link]();
[Link]("Enter the quantity");
Qty=[Link]();
[Link]("Enter the rate");
rate=[Link]();
cost=Qty*rate;
[Link]("Cost of product = "+cost);
Total_cost=Total_cost+cost;
}
[Link]("Total Bill = "+Total_cost);
}
}
6. Design a class to enter the marks in 3 subjects of 60 students.
Calculate the average marks of each students depending on the average marks.
Print the message for each students as follow:

Average Marks Message


>=90 Excellent
>=80 but <90 Very good
>=70 but <80 Good
>=60 but <70 Satisfactory
<40 Poor

import [Link].*;
class counter6
{
public static void main()
{
Scanner sc=new Scanner([Link]);
int sub1,sub2,sub3;
double Avg;
for(int i=1;i<=60;i++)
{
[Link]("Enter marks of 3 subject");
sub1=[Link]();
sub2=[Link]();
sub3=[Link]();

Avg=(sub1+sub2+sub3)/3.0;

if(Avg>=90)
[Link]("Excellent");
else if(Avg>=80 && Avg<90)
[Link]("Very good");
else if(Avg>=70 && Avg<80)
[Link]("Good");
if(Avg>=60 && Avg<70)
[Link]("Satisfactory");
else
[Link]("Poor");
}
}
}
7. Design a class to enter 10 numbers.
Find and print the largest and the smallest number.
import [Link].*;
class counter7
{
public static void main()
{
Scanner sc=new Scanner([Link]);
int N,small,great;
[Link]("Enter the first number");
N=[Link]();
small=N,great=N;
for(int i=1;i<=9;i++)
{
[Link]("Enter the number");
N=[Link]();
if(N<small)
small=N;
else if(N>great)
grate=N;
}
[Link]("Smallest number = "+small);
[Link]("Greatest number = "+great);
}}
8. Design a class to enter 10 positive real numbers. Calculate and print the sum of the
integer parts of the numbers and the product of the decimal part of the number.

import [Link].*;
class counter8
{
public static void main()
{
Scanner sc=new Scanner([Link]);
double N,I,D,Sum_INT=0.0,Pro_DECI=0.0;
for(int i=1;i<=10;i++)
{
[Link]("Enter any number");
N=[Link]();

I=(int)N;
D=N-I;

Sum_INT=Sum_INT+I;
Pro_DECI=Pro_DECI+D;
}

[Link]("Sum of integer part = "+Sum_INT);


[Link]("Product of decimal part = "+Pro_DECI);
}
}

9. Design a class to enter the 3 sides of 10 triangles. Calculate and print the area of each
triangle also find and print the types of triangle.

import [Link].*;
class counter9
{
public static void main()
{
Scanner sc=new Scanner([Link]);
int A,B,C;
for(int i=1;i<=10;i++)
{
[Link]("Enter the 3 sides of triangle");
A=[Link]();
B=[Link]();
C=[Link]();
S=(A+B+C)/2.0;
Area=[Link](S*(S-A)*(S-B)*(S-C));
[Link]("Area of triangle = "+Area);
if(A==B && B==C)
[Link]("Equilateral triangle");
else if(A==B || B==C ||C==A)
[Link]("Isosceles triangle");
else if(A!=B && B!=C && C!=A)
[Link]("Scalene triangle");
}
}
}

10. Design a class to enter the sale s and target of 20 salesman. Calculate and print the
commission. The commission is calculate as follows:

If sales is > than target – commission is 20% of the sales


If sales is == to the target – commission is 10% of the sales
If sales is < than target – commission is zero

import [Link].*;
class counter10
{
public static void main()
{
Scanner sc=new Scanner([Link]);
double sales,target,comm=0.0;
for(int i=1;i<=20;i++)
{
[Link]("Enter the sales");
sales=[Link]();

[Link]("Enter the target");


target=[Link]();

if(sales>target)
comm=sales*20.0/100.0;
else if(sales==target)
comm=sales*10.0/100.0;
else if(sales<target)
comm=0.0;

[Link]("Commission = "+comm);
}
}
}

11. Design a class to enter10 characters. Calculate and print the following:
a. Count of upper case alphabets
b. count of lower case alphabets
c. count of digits
import [Link].*;
class counter11
{
public static void main()
{
Scanner sc=new Scanner([Link]);
char ch;
int Upper=0,Lower=0,Digits=0;
for(int i=1;i<=10;i++)
{
[Link]("Enter the character");
ch=[Link]().charAt(0);
if(ch>='A' && ch<='Z')
++Upper;
else if(ch>='a' && ch<='z')
++Lower;
else if(ch>='0' && ch<='9')
++Digit;
}
[Link]("No. of upper case = "+Upper);
[Link]("No. of lower case = "+Lower);
[Link]("No. of digits = "+Digit);
}
}

[Link] a class to enter radius of 20 circles. Calculate and print the following for each
circle.
a. Diameter = 2*radius
b. Area = π2
c. Circumference = 2πr

import [Link].*;
class counter12
{
public static void main()
{
Scanner sc=new Scanner([Link]);
double r,Circle_diameter,Area_circle,Cicle_circum;
for(int i=1;i<=10;i++)
{
[Link]("Enter the radius");
r=[Link]();

Circle_diameter=2*r;
Area_circle=3.14*r*r;
Cicle_circum=2*3.14*r;

[Link]("Diameter of circle = "+Circle_diameter);


[Link]("Area of circle = "+Circle_circle);
[Link]("Circumference of circle = "+Circle_circum);
}
}
}
13. Design a class to enter the purchase cost of 30 products. Depending on the purchase
cost of each product a discount is given as follows:
Purchase cost Discount
Less than 5000 5%
5000 but less than 20000 10%
20000 but less than 50000 15%
50000 but less than 100000 20%
Greater than equal to 100000 25%
Calculate and print the total of all the 30 products.

import [Link].*;
class counter13
{
public static void main()
{
Scanner sc=new Scanner([Link]);
double cost,dis,Toatal_cost=0.0,Net_amount;
for(int i=1;i<=30;i++)
{
[Link]("Enter the cost");
cost=[Link]();
if(cost<5000)
dis=cost*5.0/100.0;
else if(cost>=5000 && cost<=20000)
dis=cost*10.0/100.0;
else if(cost>20000 && cost<=50000)
dis=cost*15.0/100.0;
else if(cost>50000 && cost<=100000)
dis=cost*20.0/100.0;
else
dis=cost*25.0/100.0;

Net_amount=cost-dis;
Total_cost=Total_cost+Net_amount;
}
[Link]("Total cost of all 30 products = "+Total_cost);
}
}

14. Design as class to enter the grades(A,B,C) in computer project of 50 students.


Calculate and print the following:
a. Total no. of students having grade A
b. Total no. of students having grade B
c. Total no. of students having grade C

import [Link].*;
class counter14
{
public static void main()
{
Scanner sc=new Scanner([Link]);
char Grades;
int c1=0,c2=0,c3=0;
for(int i=1;i<=10;i++)
{
[Link]("Enter the Grades");
Grades=[Link]().charAt(0);
if(Grades=='A')
++c1;
else if(Grades=='B')
++c2;
else if(Grade=='C')
++C3;
}

[Link]("Total no of students having grade A "+c1);


[Link]("Total no of students having grade B "+c2);
[Link]("Total no of students having grade C "+c3);
}
}

15. Write a program to input any 50 numbers (including positive and negative).
Perform the following tasks:
(a) Count the positive numbers
(b) Count the negative numbers
(c) Sum of positive numbers
(d) Sum of negative numbers
import [Link].*;
class counter15
{
public static void main()
{
Scanner sc=new Scanner([Link]);
int N,Psum=0,Nsum=0,pv=0,nv=0;
for(int i=1;i<=50;i++)
{
[Link]("Enter any number");
N=[Link]();
if(N>0)
{
++pv;
Psum=Psum+N;
}
else
{
++nv;
Nsum=Nsum+N;
}
}
[Link]("Sum of positive number = "+Psum);
[Link]("Sum of negative number = "+Nsum);
[Link]("Total no. of positive = "+pv);
[Link]("Total no. of negative = "+nv);
}
}
16. Write a program to enter any 50 numbers and check whether they are divisible by
5 or not.
If divisible then perform the following tasks:
(a) Display all the numbers ending with the digit 5.
(b) Count those numbers ending with 0 (zero).
import [Link].*;
class counter16
{
public static void main()
{
Scanner sc=new Scanner([Link]);
int N,c=0;
for(int i=1;i<=10;i++)
{
[Link]("Enter any number");
N=[Link]();

if(N%5==0)
{
if(N%10==5)
[Link](N);
else
++c;
}
}
[Link]("Total no. of ending with zero "+c);
}
}
17. Write a program to input marks in English, Maths and Science of 40 students who
have passed ICSE Examination 2014. Now, perform the following tasks:
(a) Number of students, who have secured 95% or more in all the subjects.
(b) Number of students, who have secured 90% or more in English, Maths and Science

import [Link].*;
class counter17
{
public static void main()
{
Scanner sc=new Scanner([Link]);
int Eng,Maths,Sci,c1=0,c2=0;
double Avg=0.0;
for(int i=1;i<=40;i++)
{
[Link]("Enter the marks of 3 subjects");
Eng=[Link]();
Maths=[Link]();
Sci=[Link]();

Avg=(Eng+Maths+Sci)/3.0;
if(Avg>=95)
++c1;
else if(Eng>=90 && Maths>=90 && Sci>=90)
++c2;
}

[Link]("No. of students, who have secured 95% or more = "+c1);


[Link]("No. of students, who have secured 90% or more = "+c2);
}
}

18. Write a program to input each of height of 10 students. Count and print How
many of them have height more than 5 feet.

import [Link].*;
class counter18
{
public static void main()
{
Scanner sc=new Scanner([Link]);
int Height,c=0;
for(int i=1;i<=10;i++)
{
[Link]("Enter the Height");
Height=[Link]();

if(Height>5)
++c:
}
[Link]("NO. of students = "+c);
}
}

19. Write a program to input the age of 10 student. Count and print the following:
(a) Number of children
(b) Number of adults

import [Link].*;
class counter19
{
public static void main()
{
Scanner sc=new Scanner([Link]);
int Age,chl=0,adl=0;
for(int i=1;i<=10;i++)
{
[Link]("Enter the age");
Age=[Link]();

if(Age<18)
++chl;
else
++adl;
}

[Link]("No. of children = "+chl);


[Link]("No. of adults = "+adl);
}
}

20. Write a program to input any 10 numbers. Count and print the following:
(a) Multiple of 5
(b) Multiple of 7
(c) Multiple of 9

import [Link].*;
class counter20
{
public static void main()
{
Scanner sc=new Scanner([Link]);
int N,c1=0,c2=0,c3=0;
for(int i=1;i<=10;i++)
{
[Link]("Enter the number");
N=[Link]();

if(N%5==0)
++c1;
else if(N%7==0)
++c2;
else if(N%9==0)
++c3;
}
[Link]("Multiple of 5 = "+c1);
[Link]("Multiple of 7 = "+c2);
[Link]("Multiple of 9 = "+c3);
}
}

21. Write a program to input any 10 numbers. Calculate and print the following:
(a) sum
(b) product
(c) Average

import [Link].*;
class counter21
{
public static void main()
{
Scanner sc=new Scanner([Link]);
int N,sum=0,pro=1;
double Avg;
for(int i=1;i<=10;i++)
{
[Link]("Enter the number");
N=[Link]();

sum=sum+N;
pro=pro*N;
}

Avg=(double)sum/N;

[Link]("Sum = "+sum);
[Link]("Product = "+pro);
[Link]("Average = "+Avg);
}
}

22. Design a class to print the following format:

A 97 65 a
B 98 66 b
C 99 67 c
. . . .
. . . .
Z 122 90 z

class counter22
{
public static void main()
{
for(char i='A',j='a';i<='Z' && j<='z';i++,j++)
{
[Link](i+" "+(int)j+" "+(int)i+" "+j);
}
}
}

You might also like