New Java
New 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);
}
}
class Root
{
public static void main(String args[])
{
double x,y;
System.out.println("Enter the value of x ");
x=sq.nextInt();
y=Math.sqrt(x);
System.out.println(array[i]);
}
} }
for (i=0;i<3;i++) {
for(j=0;j<4;j++)
System.out.print(myArray[i][j] + " ");
System.out.println();
}
System.out.println();
}
}
char ch = 'a';
int ascii = ch;
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); }}
}
else
{
System.out.println("The larger is y and y = "+ y );
}
}
}
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 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);
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);
}
}
import java.applet.Applet;
import java.awt.Graphics;
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 {
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)
{
void studentMethod()
{
Question213 q = new Question213();
q.print();
}
void print()
{
System.out.print("Well Done!");
}
}
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);
}
}
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);
}
}
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);
}
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);
}
}
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");
}
}
}
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++)
{
mark_avg+=arr[i];
}
System.out.println(high);
System.out.println(mark_avg/arr.length);
}
}