0% found this document useful (0 votes)
6 views13 pages

Shubham Java

The document contains multiple Java programming exercises, including printing 'Hello Java', calculating the area of a rectangle, evaluating expressions, and manipulating numbers through various operations. Each program is presented with its corresponding input code and expected output. The exercises cover basic programming concepts such as loops, conditionals, and arithmetic operations.

Uploaded by

anshsharma619420
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)
6 views13 pages

Shubham Java

The document contains multiple Java programming exercises, including printing 'Hello Java', calculating the area of a rectangle, evaluating expressions, and manipulating numbers through various operations. Each program is presented with its corresponding input code and expected output. The exercises cover basic programming concepts such as loops, conditionals, and arithmetic operations.

Uploaded by

anshsharma619420
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

Program 1

a). Write a java program to print “Hello Java”.


Input:
class hello
{
public static void main(String[] args)
{
[Link]("Hello Java");
}
}

Output:

Program 2
a) Write a java program to find the area of a rectangle.
Input:
class arearectangle
{
public static void main(String args[])
{
Subham Prajapati 1
22CS41
int length=19;
int width=26;
int area=length*width;
[Link]("Area of rectangle="+area);
}
}
Output:

Program 2
b). Write a java program to find the result of the following expressions
i). (a<<2) + (b>>2)
ii). (a+b*100)/10
iii). a & b

Input:
class Threeexp
{
public static void main(String[] args)
{
int a=10;
int b=20;
int c=((a<<2)+(b>>2));

Subham Prajapati 2
22CS41
int d=(a+(b*100)/10);
int e=(a&b);
[Link](c);
[Link](d);
[Link](e);
}
}

Output:

Subham Prajapati 3
22CS41
Program 2
c). Write a java program to print the individual digits of a 3 digit number using
Command line arguments.
Input:
import [Link];
class threedigits
{
public static void main(String args[])
{
Scanner sc=new Scanner([Link]);
[Link]("Enter a Number:");
int n=[Link](),d;
while (n!=0)
{
d=n%10;
n=n/10;
[Link](d);
}
}
}

Subham Prajapati 4
22CS41
Output:

Program 3
a). Write a java program to read two integers and print the larger number
followed by the words “is larger”. If the numbers are equal print the message
“These numbers are equal”.
Input:
import [Link].*;
class largernumber
{
public static void main(String args[])
Subham Prajapati 5
22CS41
{
int dig1,dig2;
Scanner sc= new Scanner([Link]);
[Link]("Enter the first digit:");
dig1=[Link]();
[Link]("Enter the second digit:");
dig2=[Link]();
if (dig1>dig2)
[Link](dig1+" is larger");
else if (dig1<dig2)
[Link](dig2+"is larger");
else
[Link]("The given numbers are equal");
}}}

Output:

Subham Prajapati 6
22CS41
Program3

b). Write a java program to read an integer and find whether the number is odd
or even.
Input:
import [Link].*;
class evenodd
{
public static void main(String Args[])
{
int num1,;
Scanner sc= new Scanner([Link]);
[Link]("Enter the number:");
num1=[Link]();
if(num1 %2!= 0 ){
[Link]("The provided number is odd");
}
else{
[Link]("The provided number is even");
}
}
}
Subham Prajapati 7
22CS41
Output:

Program 4
a). Write a java program to find the sum of digits of a given number.
Input:
import [Link].*;
class Sum
{
public static void main(String args[])
Subham Prajapati 8
22CS41
{
Scanner sc=new Scanner([Link]);
[Link]("Enter a number");
int num=[Link]();
int sum=0;
while(num!=0)
{
int ld=num%10;
sum=sum+ld;
num=num/10;
}
[Link]("Sum of Digits="+sum);
}
}

Output:

Subham Prajapati 9
22CS41
Program 4
b). Write a java program to find the first 15 terms of Fibonacci series.
Input:
class Fibonacci15
{
public static void main(String args[])
{
int a=0;
int b=1;
[Link](a);
[Link](b);
for(int i=1;i<=13;i++)
{
int c=a+b;
a=b;
b=c;

Subham Prajapati 10
22CS41
[Link](c);
}
}
}

Output:

Program 4
c). Given a number, write a program using while loop to reverse
the digits of the number.
Input:
Subham Prajapati 11
22CS41
import [Link].*;
class Reverse
{
public static void main(String args[])
{
Scanner sc=new Scanner([Link]);
[Link]("Enter a number");
int num=[Link]();
int rev=0;
while(num!=0)
{
int ld=num%10;
rev=rev*10+ld;
num=num/10;
}
[Link]("Reverse of the number="+rev);
}
}

Output:

Subham Prajapati 12
22CS41
Subham Prajapati 13
22CS41

You might also like