Java Assignment-1
Java Assignment-1
soln:
import java.util.Scanner;
public class SumOfDigits
{
public static void main(String args[])
{
int number, digit, sum = 0;
Scanner sc = new Scanner(System.in);
System.out.print("Enter the number: ");
number = sc.nextInt();
while(number > 0)
{
digit = number % 10;
sum = sum + digit;
number = number / 10;
}
System.out.println("Sum of Digits: "+sum);
}
}
-----------------------------------------------------------------------------------
---------------
Que-2.Calculate Electricity bill (According to units & rate slab ).
Soln:
import java.util.*;
class ComputeElectricityBill
{
public static void main(String args[])
{
long units;
units=sc.nextLong();
double billpay=0;
if(units<100)
billpay=units*1.20;
else if(units<300)
billpay=100*1.20+(units-100)*2;
else if(units>300)
billpay=100*1.20+200 *2+(units-300)*3;
String ten[] = { " ", " ", " Twenty", " Thirty", " Forty", " Fifty", " Sixty",
"Seventy", " Eighty", " Ninety" };
if (n > 19)
{
System.out.print(ten[n / 10] + " " + one[n % 10]);
}
else
{
System.out.print(one[n]);
}
if (n > 0)
System.out.print(ch);
}
if (n <= 0)
{
System.out.println("Enter numbers greater than 0");
}
else
{
NumberToWords nw = new NumberToWords();
nw.pw((n / 1000000000), " Hundred");
nw.pw((n / 10000000) % 100, " crore");
nw.pw(((n / 100000) % 100), " lakh");
nw.pw(((n / 1000) % 100), " thousand");
nw.pw(((n / 100) % 10), " hundred");
nw.pw((n % 100), " ");
}
}
}
-----------------------------------------------------------------------------------
---------------
que-4.Write a program to print palindrome number up to N numbers.
soln:
import java.util.Scanner;
public class program2
{
public static void main(String[] args)
{
int n, b, rev = 0;
Scanner sc = new Scanner(System.in);
System.out.println("Enter the Palindrome No N:");
int N = sc.nextInt();
System.out.print("Palindrome numbers from 1 to N:");
for (int i = 1; i <= N; i++)
{
n = i;
while (n > 0)
{
b = n % 10;
rev = rev * 10 + b;
n = n / 10;
}
if (rev == i)
{
System.out.print(i + " ");
}
rev = 0;
}
}
}
-----------------------------------------------------------------------------------
----------------------
5. Program to print following patterns of stars.
i) *
**
***
****
*****
Soln:
public class program5i {
public static void main(String args[]){
int n,i,j;
n = 5;
for(i = 0; i<= n; i++) {
for(j = 0; j<= i; j++){
System.out.print("*"+" ");
}
System.out.println(" ");
}
}
}
-----------------------------------------------------------------------------------
--------------------
ii))
***************
******* *******
****** ******
***** *****
**** ****
*** ***
** **
* *
* *
** **
*** ***
**** ****
***** *****
****** ******
******* *******
***************
Soln:
import java.util.Scanner;
System.out.println();
}
}
}
-----------------------------------------------------------------------------------
----------------------
Que-6.Program to print following patterns of Numbers.
1
121
12321
1234321
123454321
Soln:
import java.util.Scanner;
public class program6
{
public static void main(String[] args)
{
int rows = 5;
System.out.println("----Pattern is----");
for (int i = 1; i <= rows; i++)
{
for (int j = 1; j <= i; j++)
{
System.out.print(j+" ");
}
for (int j = i-1; j >= 1; j--)
{
System.out.print(j+" ");
}
System.out.println();
}
}
}
-----------------------------------------------------------------------------------
-----------------------
Que-7.Print Floyds Triangle.
Soln:
import java.util.Scanner;
public class program7
{
public static void main(String args[]){
int i,j,k = 1;
System.out.println("Printing Floyids Triangle: ");
for(i = 1; i <= 5; i++) {
for(j=1;j <= i; j++){
System.out.print(" "+k++);
}
System.out.println();
}
}
}
-----------------------------------------------------------------------------------
--------------------
que-8.WAP to find second largest number in an array.
soln:
public class program3
{
public static int getSecondLargest(int[] a, int total){
int temp;
for (int i = 0; i < total; i++)
{
for (int j = i + 1; j < total; j++)
{
if (a[i] > a[j])
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
return a[total-2];
}
public static void main(String args[])
{
int a[]={1,2,9,6,3,2};
int b[]={44,66,99,11,33,22,55};
System.out.println("Second Largest:"+getSecondLargest(a,6));
System.out.println("Second Largest:"+getSecondLargest(b,7));
}
}
-----------------------------------------------------------------------------------
--------
que-9.WAP to remove duplicate element in an array.
soln:
public class program9
{
public static int removeDuplicateElement(int arr[], int n)
{
if (n==0 || n==1)
return n;
temp[j++] = arr[n-1];
for (int i=0; i<j; i++)
arr[i] = temp[i];
return j;
}
if(setValue == true)
{
System.out.println("The Given Matrix is a Null Matrix");
}
else
{
System.out.println("The Given Matrix is not a Null Matrix");
}
}
}
-----------------------------------------------------------------------------------
--------------------
que-11.WAP to check given matrix is diagonal matrix.
soln:
class DiagonalMatrix
{
public static void main(String args[])
{
int[][] a = new int[][] { { 1, 0, 0},{ 0, 3, 0},{ 0, 0, 3} };
boolean setValue = true;
abc: for(int i = 0;i < a.length;i++)
{
for(int j = 0;j < a[i].length;j++)
{
if(i == j)
{
if(a[i][j] == 0)
{
setValue = false;
break abc;
}
}
else if(a[i][j] != 0)
{
setValue = false;
break abc;
}
}
}
System.out.println("The Given Matrix Value:");
for(int i = 0;i < a.length;i++)
{
for(int j = 0;j < a[i].length;j++)
{
System.out.print(a[i][j] + " ");
}
System.out.println();
}
if(setValue == true)
{
System.out.println("The Given Matrix is a Diagonal Matrix");
}
else
{
System.out.println("The Given Matrix is not a Diagonal Matrix");
}
}
}
-----------------------------------------------------------------------------------
-----------------------
que-12.WAP to split a comma – separated string.
soln:
import java.util.*;
class SplitStringExample1 {
-----------------------------------------------------------------------------------
-------------
Que-14.
public class program14
{
abstract class Shape
{
public abstract double getArea();
}
class Circle extends Shape
{
private double radius;
public Circle(double radius)
{
this.radius = radius;
}
@Override
public double getArea()
{
return Math.PI * radius * radius;
}
}
class Rectangle extends Shape
{
private double width;
private double height;
public Rectangle(double width, double height)
{
this.width = width;
this.height = height;
}
@Override
public double getArea()
{
return width * height;
}
}
public static void main(String[] args)
{
program14 main = new program14();
Shape circle = main.new Circle(2.0);
Shape rectangle = main.new Rectangle(3.0, 4.0);
System.out.println("Circle area: " + circle.getArea());
System.out.println("Rectangle area: " + rectangle.getArea());
}
}
-----------------------------------------------------------------------------------
---------------------------
Que-15.WAP to design a String Class that perform string method (Equal, Reverse the
string, Change Case, etc).
Soln:
public class program15
{
class main
{
public static boolean equalsIgnoreCase(String str1,String str2)
{
return str1.equalsIgnoreCase(str2);
}
public static String reverse(String str)
{
StringBuilder sb= new StringBuilder(str);
return sb.reverse().toString();
}
public static String toUppercase(String str)
{
return str.toUpperCase();
}
public static String toLowercase(String str)
{
return str.toLowerCase();
}
}
public static void main(String[] args)
{
String str1 , str2,str3;
str1="Hello";
str2= "World";
str3="java";
System.out.println(main.equalsIgnoreCase(str1, str2));
System.out.println(main.equalsIgnoreCase(str1, str3));
}
}