0% found this document useful (0 votes)
27 views

Java Assignment-1

The document contains solutions to 10 Java programming questions including finding the sum of digits of a number, calculating electricity bill according to usage, printing a number in words, printing palindrome numbers up to a given value, printing patterns of stars and numbers, finding the second largest number in an array, removing duplicate elements from an array, and checking if a given matrix is a null matrix.

Uploaded by

Titiksha Shukla
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views

Java Assignment-1

The document contains solutions to 10 Java programming questions including finding the sum of digits of a number, calculating electricity bill according to usage, printing a number in words, printing palindrome numbers up to a given value, printing patterns of stars and numbers, finding the second largest number in an array, removing duplicate elements from an array, and checking if a given matrix is a null matrix.

Uploaded by

Titiksha Shukla
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 11

que-1.Find the Sum of Digits of a Number in Java.

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;

Scanner sc=new Scanner(System.in);

System.out.println("enter number of 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;

System.out.println("Bill to pay : " + billpay);


}
}
-----------------------------------------------------------------------------------
---------------
Que-3.Print given number in words. (E.g. 123 = “One thousand Two hundred Three” )
Soln:
import java.util.Scanner;

public class NumberToWords


{
public void pw(int n, String ch)
{
String one[] = { " ", " One", " Two", " Three", " Four", " Five", " Six", "
Seven", " Eight", " Nine", " Ten",
" Eleven", " Twelve", " Thirteen", " Fourteen", "Fifteen", " Sixteen", "
Seventeen", " Eighteen",
" Nineteen" };

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);
}

public static void main(String[] args)


{
int n = 0;
Scanner sc = new Scanner(System.in);
System.out.println("Enter an integer number: ");
n = sc.nextInt();

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;

public class program5ii


{
public static void main(String[] args)
{
int rows = 5;
System.out.println("## Printing the pattern ##");

// Print i number of stars


for (int i=1; i<=rows; i++)
{
for (int j = i; j <= rows; j++)
{
System.out.print("*");
}
for (int k = 1; k <= i*2-2; k++)
{
System.out.print(" ");
}
for (int l = i; l <= rows; l++)
{
System.out.print("*");
}
System.out.println();
}

for (int i = 1; i <= rows; i++)


{
for (int j = 1; j <= i; j++)
{
System.out.print("*");
}
for (int k = i*2-2; k < rows*2-2; k++)
{
System.out.print(" ");
}
for (int l = 1; l <= i; l++)
{
System.out.print("*");
}

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;

int[] temp = new int[n];


int j = 0;
for (int i=0; i<n-1; i++)
if (arr[i] != arr[i+1])
temp[j++] = arr[i];

temp[j++] = arr[n-1];
for (int i=0; i<j; i++)
arr[i] = temp[i];

return j;
}

public static void main (String[] args)


{
int arr[] = {10,20,20,30,30,40,50,50};
int length = arr.length;
length = removeDuplicateElement(arr, length);
for (int i=0; i<length; i++)
System.out.print(arr[i]+" ");
}
}
-----------------------------------------------------------------------------------
------------
que-10.WAP to check given matrix is null matrix.
soln:
class program10
{
public static void main(String args[])
{
int[][] a = new int[][] { { 0, 0, 0},{ 0, 0, 0},{ 0, 0, 0} };
boolean setValue = true;

abc: for(int i = 0;i < a.length;i++)


{
for(int j = 0;j < a[i].length;j++)
{
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 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 {

public static void main(String[] args) {

String str1, str2;

Scanner sc = new Scanner(System.in);


System.out.println("\nEnter String1 to split by comma: ");
str1 = sc.nextLine();

System.out.println("\nEnter String2 to split by comma: ");


str2 = sc.nextLine();

String[] str1Array = str1.split(", ");

String[] str2Array = str2.split(", ", 3);


System.out.println("\nElements after break "+str1+":");

for (int i = 0; i < str1Array.length; i++){


System.out.println((i+1)+" element: "+str1Array[i]);
}

System.out.println("\nElements after break "+str2+":");


for (int i=0; i < str2Array.length; i++){
System.out.println((i+1)+" element: "+str2Array[i]);
}
}
}
----------------------------------
que 13
import java.util.*;
class Bank {
static int acc_no = 10001;
float amt;
public void display() {
System.out.println("Account no :" + acc_no);
System.out.println("Current Amount :" + amt);
}
public bank() {
amt = 1000;
System.out.println("Ur account no is " + acc_no);
acc_no++;
}
public void getamt() {
System.out.println("Current balance :" + amt);
}
public void withdraw(float x) {
if (amt == 1000 || amt <= x) {
System.out.println("Sorry u can't withdraw");
} else {
amt = amt - x;
System.out.println("amount withdrawn :" + x);
System.out.println("After withdrawl");
getamt();
}
}
public void deposit(float x) {
if (x == 0.0)
System.out.println("OOPS 0 can't be deposited");
else {
amt += x;
System.out.println("After deposition");
getamt();
}
}
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
Bank b1 = new Bank();
b1.deposit(0);
b1.withdraw(120.5f);
b1.display();
System.out.println("\n");
Bank b2 = new Bank();
b2.deposit(1000.0f);
b2.withdraw(150.5f);
}
}

-----------------------------------------------------------------------------------
-------------
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));

System.out.println(main.reverse(str1+" \n" +str2+ "\n"+str3));


System.out.println(main.toLowercase(str1+" \n" +str2+" \n"+str3));
System.out.println(main.toUppercase(str1+" \n" +str2+" \n"+str3));

}
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy