Java Util Follout Void String Int: Import Class Public Static
Java Util Follout Void String Int: Import Class Public Static
DEVADHARSHANI A
2248327
1. Write a Java program to print the following output using for loops.
Code:
import java.util.*;
class FollOut
{
public static void main(String args[])
{
int i,j;
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
System.out.print("$ ");
}
System.out.println();
}
}
}
Output:
--------------------------------------------------------------------------------------------------------
2. Write a Java program that reads a positive integer and count the number of
digits and its sum using for loop.
Code:
import java.util.*;
class CountSumDig
{
public static void main(String args[])
{
Scanner s=new Scanner(System.in);
System.out.println("Enter a number: ");
int num=s.nextInt();
int r,a,count=0,sum=0;
for(a=num;a!=0;a=a/10)
{
r=a%10;
sum=sum+r;
count++;
}
System.out.println("Number of digits: "+count);
System.out.println("Sum of the digit: "+sum);
s.close();
}
}
Output:
---------------------------------------------------------------------------------------------------------
3)Write a java program to find the sum and average of integers ranging from
the given lowerbound to an upperbound using the following control structures.
a) while loop
Code:
import java.util.*;
class SuAvgWhile
{
public static void main(String args[])
{
Scanner s=new Scanner(System.in);
System.out.println("Enter the lowerbound: ");
float l=s.nextInt();
System.out.println("Enter the upperbound: ");
float u=s.nextInt();
float count=0,sum=0;
float avg;
while(l<=u)
{
sum=sum+l;
count++;
l++;
}
avg=sum/count;
System.out.println("Sum="+sum);
System.out.println("Average="+avg);
s.close();
}
}
Output:
--------------------------------------------------------------------------------------------------------
b)do..while loop
Code:
import java.util.*;
class SuAvgDoWhile
{
public static void main(String args[])
{
Scanner s=new Scanner(System.in);
System.out.println("Enter the lowerbound: ");
float l=s.nextInt();
System.out.println("Enter the upperbound: ");
float u=s.nextInt();
float count=0,sum=0;
float avg;
do
{
sum=sum+l;
count++;
l++;
}
while(l<=u);
avg=sum/count;
System.out.println("Sum="+sum);
System.out.println("Average="+avg);
s.close();
}
}
Output:
c) for loop
Code:
import java.util.*;
class SuAvgFor
{
public static void main(String args[])
{
Scanner s=new Scanner(System.in);
System.out.println("Enter the lowerbound: ");
float l=s.nextInt();
System.out.println("Enter the upperbound: ");
float u=s.nextInt();
float count=0,sum=0;
float avg;
float i;
for(i=l;i<=u;i++)
{
sum=sum+i;
count++;
}
avg=sum/count;
System.out.println("Sum="+sum);
System.out.println("Average="+avg);
s.close();
}
}
Output:
---------------------------------------------------------------------------------------------------------
4. Write a Java program to determine the sum of odd numbers and sum of
even numbers from 1 to 100 and also the difference between these sums - USE
for loop.
Code:
import java.util.*;
class SumOddEven
{
public static void main(String args[])
{
int diff,sumOdd=0,sumEven=0;
for(int i=0;i<101;i++)
{
if(i%2==0)
{
sumEven=sumEven+i;
}
if(i%2!=0)
{
sumOdd=sumOdd+i;
}
}
diff=sumEven-sumOdd;
System.out.println("Sum of odd numbers: "+sumOdd);
System.out.println("Sum of even numbers: "+sumEven);
System.out.println("Difference between the sum: "+diff);
}
}
Output:
---------------------------------------------------------------------------------------------------------
5. Write a java program to print the numbers from 1 to 100 in separate lines
but the code has to display “Christ” in the place of multiples of 5.
Code:
import java.util.*;
class Christ
{
public static void main(String args[])
{
for(int i=0;i<=100;i++)
{
if(i%5==0)
{
System.out.println("christ");
}
else
{
System.out.println(i);
}
}
}
}
Output:
-------------------------------------------------------------------------------------------------------
6. Write a Java program to print the first 10 numbers in the Lucas series using
classes and objects.
Code:
import java.util.*;
class Lucas
{
void func()
{
int a=2,b=1,i,temp;
System.out.print(a+" "+b+" ");
for(i=1;i<=10;i++)
{
temp=a+b;
System.out.print(temp+" ");
a=b;
b=temp;
}
}
}
class LucasM
{
public static void main(String args[])
{
Lucas obj=new Lucas();
obj.func();
}
}
Output:
---------------------------------------------------------------------------------------------------------
7. Write a java program to print the perfect numbers from 1 to n using classes
and objects.
Code:
import java.util.Scanner;
class Perfectnum {
}
}
}
Output:
---------------------------------------------------------------------------------------------------------
8. Write a Java Program to Print the following Series
a)1,2,9,28,65
b)1+2/2!+3/3!+…+n/n!
using classes and objects.
Code:
import java.util.Scanner;
class calc {
void series1(int n) {
for (int i = 0; i < n; i++) {
int result = (i * i * i) + 1;
System.out.print(result + " ");
}
}
void series2(int n) {
for (int i = 1; i <= n; i++) {
int fact = 1;
for (int j = 1; j <= i; j++) {
fact = fact * j;
}
System.out.print(i + "/" + fact + ", ");
}
}
}
Output:
---------------------------------------------------------------------------------------------------------
9. Write a Java program to calculate a) diameter b)circumference of a given
circle using classes and objects.
Code:
import java.util.*;
class CircleM
{
float dia(float radius)
{
float diameter=2*radius;
return diameter;
}
void circum(float radius)
{
double cir=2*3.14*radius;
System.out.println("Circumference: "+cir);
}
}
class Circle
{
public static void main(String args[])
{
Scanner s=new Scanner(System.in);
System.out.println("Enter radius: ");
float r=s.nextFloat();
CircleM obj=new CircleM();
float diam=obj.dia(r);
System.out.println("Diameter: "+diam);
obj.circum(r);
}
}
Output:
---------------------------------------------------------------------------------------------------------
10. Write a Java program to calculate the tax to be payable by a salaried
individual. The current income tax mandate slab for the financial year 2022-
2023 is given as follows.
Code:
import java.util.*;
class IncomeR
{
double tax(double income)
{
double amount;
if(income<=250000)
{
amount=0;
return amount;
}
else if(income>=250001 && income<=500000){
amount=income*0.05;
return amount;
}
else if(income>=500001 && income<=750000)
{
amount=12500 +(income*0.1);
return amount;
}
else if(income>=750001 && income<=1000000)
{
amount=37500 +(income*0.15);
return amount;
}
else if(income>=1000001 && income<=1250000)
{
amount=75000+(income*0.2);
return amount;
}
else if(income>=1250000 && income<=1500000)
{
amount=125000+(income*0.25);
return amount;
}
else
{
amount=187500+(income*0.2);
return amount;
}
}
}
class Income
{
public static void main(String args[])
{
Scanner s=new Scanner(System.in);
System.out.println("Enter annual income: ");
double in=s.nextFloat();
IncomeR obj=new IncomeR();
double val=obj.tax(in);
System.out.println("Your tax amount is: "+val);
}
}
Output:
________________________________________________________________