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

New Java

The document contains 15 programming problems and their solutions in Java. The problems cover topics such as: - Adding two numbers - Finding the square root of a number - Sorting arrays in increasing and decreasing order - Comparing salaries and numbers - Performing operations on 2D and 3D arrays - Using the static keyword - Finding ASCII values - Performing all binary operations - Converting strings to integers - Counting letters, spaces, numbers in a string - Calculating results of operations For each problem, the code to solve it is provided.

Uploaded by

Nishita Waghmare
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)
253 views

New Java

The document contains 15 programming problems and their solutions in Java. The problems cover topics such as: - Adding two numbers - Finding the square root of a number - Sorting arrays in increasing and decreasing order - Comparing salaries and numbers - Performing operations on 2D and 3D arrays - Using the static keyword - Finding ASCII values - Performing all binary operations - Converting strings to integers - Counting letters, spaces, numbers in a string - Calculating results of operations For each problem, the code to solve it is provided.

Uploaded by

Nishita Waghmare
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
You are on page 1/ 27

COMPUTER WORKSHOP CS 306

1.Write a Program to add two numbers in JAVA….

import java.util.Scanner;
class Sum
{
public static void main(String args[])
{
int x,y,z;
System.out.println("Enter the value of a & b");
Scanner obj1= new Scanner(System.in);
x=obj1.nextInt();
y=obj1.nextInt();
z=x+y;
System.out.println("Sum of the integer ="+z);
}
}

NANDINI BHAVSAR CS03 BATCH 1


COMPUTER WORKSHOP CS 306

2.Write a Program to find Square root of a given number…


import java.util.Scanner;

class Root
{
public static void main(String args[])
{
double x,y;
System.out.println("Enter the value of x ");

Scanner sq= new Scanner(System.in);

x=sq.nextInt();
y=Math.sqrt(x);

System.out.println("Square root is ="+y);


}
}

NANDINI BHAVSAR CS03 BATCH 1


COMPUTER WORKSHOP CS 306

3. Write a Program to sort array in increasing order…


import java.util.Scanner;

public class InsertSort


{
public static void main (String [] args)
{
int [] array = {45,12,85,32,89,39,69,44,42,1,6,8};
int temp;
for (int i = 1; i < array.length; i++)
{
for (int j = i; j > 0; j--)
{
if (array[j] < array [j - 1])
{
temp = array[j];
array[j] = array[j - 1];
array[j - 1] = temp;

NANDINI BHAVSAR CS03 BATCH 1


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

System.out.println(array[i]);
}
} }

NANDINI BHAVSAR CS03 BATCH 1


COMPUTER WORKSHOP CS 306

4.Write a Program to sort array in Decreasing order


import java.util.Scanner;
public class InsertSort1
{
public static void main (String [] args)
{
int [] array = {45,12,85,32,89,39,69,44,42,1,6,8};
int temp;
for (int i = 1; i < array.length; i++)
{
for (int j = i; j > 0; j--)
{
if (array[j] > array [j - 1])
{
temp = array[j];
array[j] = array[j - 1];
array[j - 1] = temp;
}
}
}
for (int i = 0; i < array.length; i++)
{
System.out.println(array[i]);
}
}
}

NANDINI BHAVSAR CS03 BATCH 1


COMPUTER WORKSHOP CS 306

5.Write a Program to compare the salary of two Employers


import java.util.Scanner;
class compare
{
public static void main(String args[])
{
int x,y;
System.out.println("Enter two integers for calculation");
Scanner obj1 = new Scanner(System.in);
x=obj1.nextInt();
y=obj1.nextInt();
if(x>y)
{
System.out.println("The larger is x and x = " +x );
}
else
{
System.out.println("The larger is y and y = "+ y );
}
}
}

NANDINI BHAVSAR CS03 BATCH 1


COMPUTER WORKSHOP CS 306

6. Write a Program for multiplication in 2D Array


import java.util.Scanner;
class Aarray
{
public static void main(String args[])
{
int myArray [] [] = new int [3] [4];
int i,j;
for (i=0;i<3;i++)
for(j=0;j<4;j++)
myArray[i][j] = i*j;

for (i=0;i<3;i++) {

for(j=0;j<4;j++)
System.out.print(myArray[i][j] + " ");
System.out.println();
}
System.out.println();
}
}

NANDINI BHAVSAR CS03 BATCH 1


COMPUTER WORKSHOP CS 306

7.Write a Program for summation in 3D Array


import java.util.Scanner;
class a3DArray
{
public static void main(String args[])
{
int my3DArray [] [] [] = new int [2] [3] [4];
int i,j,k;
for (i=0;i<2;i++)
for(j=0;j<3;j++)
for(k=0;k<4;k++)
my3DArray[i][j][k] = i+j+k;
for (i=0;i<2;i++)
{
for(j=0;j<3;j++)
{
for(k=0;k<4;k++)
System.out.print(my3DArray[i][j][k] + " ");
System.out.println();
}
System.out.println();
}
}
}

NANDINI BHAVSAR CS03 BATCH 1


COMPUTER WORKSHOP CS 306

8 Write a Program to using Static KeyWord…


import java.util.Scanner;
class Emp
{
static double salary;
static String name = "Punit";
}
public class EmpDemo
{
public static void main(String args[])
{
Emp.salary=25000;
System.out.println(Emp.name+"s average salary:"+Emp.salary);
}
}

9. Write a Program to find Ascii value


public class AsciiValue {
public static void main(String[] args) {

NANDINI BHAVSAR CS03 BATCH 1


COMPUTER WORKSHOP CS 306

char ch = 'a';
int ascii = ch;

int convertAscii = (int) ch;


System.out.println("The ASCII value of " + ch + " is: " + ascii);
System.out.println("The ASCII value of " + ch + " is: " + convertAscii);
}
}

10. Write a Program to perform all binary operation


import java.util.Scanner;
class calci
{
public static void main(String args[])
{
int a,b,di,s,m,mu;
System.out.println("Enter two integers for calculation");
Scanner obj1 = new Scanner(System.in);
a=obj1.nextInt();
b=obj1.nextInt();
s=a+b;
m=a-b;

NANDINI BHAVSAR CS03 BATCH 1


COMPUTER WORKSHOP CS 306

mu=a*b;
di=a/b;
System.out.println("Sum of two number will be \n" + s);
System.out.println("Subraction of two number will be \n" + m);
System.out.println("Multipication of two number will be\n" + mu);
System.out.println("Division of two number will be \n" + di); }}

11. Write a Program to compare the numbers


import java.util.Scanner;
class compare
{
public static void main(String args[])
{
int x,y;
System.out.println("Enter two integers for calculation");
Scanner obj1 = new Scanner(System.in);
x=obj1.nextInt();
y=obj1.nextInt();
if(x>y)
{
System.out.println("The larger is x and x = " +x );

NANDINI BHAVSAR CS03 BATCH 1


COMPUTER WORKSHOP CS 306

}
else
{
System.out.println("The larger is y and y = "+ y );
}
}
}

12. Write a Program for Division of two number..


import java.util.Scanner;
class div
{
public static void main(String args[])
{
int a,b,z;
System.out.println("Enter two integers for division");
Scanner obj1 = new Scanner(System.in);
a=obj1.nextInt();
b=obj1.nextInt();
z=a/b;
System.out.println("Division of two number will be " + z);
}

NANDINI BHAVSAR CS03 BATCH 1


COMPUTER WORKSHOP CS 306

13. Write a Program to convert String to Integer

public class String_to_Integer {

public static void main(String args[])


{
String s="333";
int i=Integer.parseInt(s);

NANDINI BHAVSAR CS03 BATCH 1


COMPUTER WORKSHOP CS 306

System.out.println(i);
}
}

14. Write a Program to count the letters, spaces, numbers & other character of String
import java.util.Scanner;
public class Counts_variable
{

public static void main(String[] args)


{

NANDINI BHAVSAR CS03 BATCH 1


COMPUTER WORKSHOP CS 306

String test = "ajv ivbjsdbvi uvbskjb ?? sknvsi 3245 bsdnbio =vnei";


Count(test);

}
public static void Count(String x)
{
char[] ch = x.toCharArray();
int letter = 0;
int space = 0;
int num = 0;
int other = 0;
for(int i = 0; i < x.length(); i++){
if(Character.isLetter(ch[i])){
letter ++ ;
}
else if(Character.isDigit(ch[i])){
num ++ ;
}
else if(Character.isSpaceChar(ch[i])){
space ++ ;
}
else{
other ++;
}
}
System.out.println("The string is : ajv ivbjsdbvi uvbskjb ?? sknvsi 3245
bsdnbio=vnei");
System.out.println("letter: " + letter);
System.out.println("space: " + space);

NANDINI BHAVSAR CS03 BATCH 1


COMPUTER WORKSHOP CS 306

System.out.println("number: " + num);


System.out.println("other: " + other);
}
}

15. Write a Program for result of operation

NANDINI BHAVSAR CS03 BATCH 1


COMPUTER WORKSHOP CS 306

import java.util.Scanner;
class Operations
{
public static void main(String args[])
{
int a,b,c,d;
a=5*10-36;
b=(85-20)%9;
c=20+(-3*5)/8;
d=5+15/3*2-8%3;
System.out.println("output of two number will be " + a);
System.out.println("output of two number will be " + b);
System.out.println("output of two number will be " + c);
System.out.println("output of two number will be " + d);
}
}

16. Write a Program to print Hello World in applet

NANDINI BHAVSAR CS03 BATCH 1


COMPUTER WORKSHOP CS 306

import java.applet.Applet;
import java.awt.Graphics;

public class HelloWorld extends Applet


{
public void paint(Graphics g)
{
g.drawString("Hello World!",150,150);
}
}

NANDINI BHAVSAR CS03 BATCH 1


COMPUTER WORKSHOP CS 306

1. Complete the code segment to call the method print() of class Student first and then
call print() method of class School.

import java.util.Scanner;
class School {

public void print() {


System.out.println("Hi! This is class SCHOOL.");
}
}
class Student {

public void print() {


System.out.println("Hi! This is class STUDENT");
}
}
public class Question212{
public static void main(String[] args) {
School write = new School();
write.print();
Student show = new Student();
show.print();
}
}

NANDINI BHAVSAR CS03 BATCH 1


COMPUTER WORKSHOP CS 306

2. Complete the code segment to call print() method of class Question by creating a
method named ‘studentMethod()’.

import java.util.Scanner;
public class Question213
{
public static void main(String[] args)
{

Question213 q = new Question213();


q.studentMethod();
}

void studentMethod()
{
Question213 q = new Question213();
q.print();
}

void print()
{
System.out.print("Well Done!");
}
}

NANDINI BHAVSAR CS03 BATCH 1


COMPUTER WORKSHOP CS 306

3. Complete the code segment to call default constructor first and then any
other constructor in the class Answer
import java.util.Scanner
public class Question214
{
public static void main(String[] args)
{
Answer a=new Answer();
Answer b = new Answer(10,"MCQ");
}
}
class Answer
{
Answer()
{
System.out.println("You got nothing.");
}
Answer(int marks, String type)
{
System.out.println("You got "+marks+" for an "+ type);
}
}

NANDINI BHAVSAR CS03 BATCH 1


COMPUTER WORKSHOP CS 306

4. Complete the code segment to find the perimeter and area of a circle given a value
of radius.You should use Math.PI constant in your program.If radius is zero or less
than zero then print " please enter non zero positive number "

import java.util.Scanner;
public class Exercise1_1
{
public static void main(String args[])
{
int a,b,c;
Scanner s=new Scanner(System.in);
double radius=s.nextDouble();
double perimeter;
double area;
if(radius<=0)
{
System.out.println("Please enter non zero positive number");
}
else
{
perimeter=2*Math.PI*radius;
area=Math.PI*radius*radius;
System.out.println(perimeter);
System.out.println(area);
}
}

NANDINI BHAVSAR CS03 BATCH 1


COMPUTER WORKSHOP CS 306

5. Complete the code segment to find the largest among three numbers x, y, and
z. You should use if-then-else construct in Java.

import java.util.Scanner;
public class Exercise1_2
{
public static void main(String args[])
{
Scanner s=new Scanner(System.in);
int x=s.nextInt();
int y=s.nextInt();
int z=s.nextInt();
int result=0;
if(x>y && x>z)
{
System.out.println("Biggest Number is"+" "+x);
}
else if(y>x && y>z)
{
System.out.println("Biggest Number is"+" "+y);
}
else
{
System.out.println("Biggest Number is"+" "+z);
}

NANDINI BHAVSAR CS03 BATCH 1


COMPUTER WORKSHOP CS 306

6. Complete the code segment to check whether the number is an Armstrong


number or not.

import java.util.Scanner;
public class Exercise1_3
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int sum=0;
int cal=0;
for(int i=0;;i+=2)
{
if((cal==n+1))
{
break;
}
if(i%3==0)
{
sum+=i;
}
cal+=1;

}
System.out.println(sum);
}
}

NANDINI BHAVSAR CS03 BATCH 1


COMPUTER WORKSHOP CS 306

7. Complete the code segment to help Ram , find the highest mark and average mark
secured by him in "s" number of subjects.

import java.util.Scanner;
import java.lang.Math;
public class Exercise1_4
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int n2=n;
int result=0;
int cal=0;
int n1=n;
int sum=0;
int re;
while(n1>0)
{
n1=n1/10;
cal++;
}
while(n>0)
{
re=n%10;

result+=Math.pow(re,cal);
n=n/10;
}
if(result==n2)
{
System.out.println("Number is an Armstrong Number");
}
else
{
System.out.println("Number is not an Armstrong Number");
}
}
}

NANDINI BHAVSAR CS03 BATCH 1


COMPUTER WORKSHOP CS 306

8. Consider First n even numbers starting from zero(0).Complete the code segment to calculate
sum of all the numbers divisible by 3 from 0 to n.Print the sum.

import java.util.Scanner;
public class Exercise1_5
{
public static void main(String args[])
{
Scanner input=new Scanner(System.in);
double mark_avg=0;
int result;
int i;
int s,high=0;
s=input.nextInt();
int[] arr=new int[s];
for(i=0;i<arr.length;i++)
{
arr[i]=input.nextInt();
}
for(i=0;i<arr.length;i++)
{
if(high<arr[i])
{
high=arr[i];
}
}
for(i=0;i<arr.length;i++)
{

NANDINI BHAVSAR CS03 BATCH 1


COMPUTER WORKSHOP CS 306

mark_avg+=arr[i];
}
System.out.println(high);
System.out.println(mark_avg/arr.length);
}
}

NANDINI BHAVSAR CS03 BATCH 1

You might also like