0% found this document useful (0 votes)
4 views15 pages

Hemant Java

The document contains several Java programming exercises including printing 'Hello', calculating the area of a rectangle, performing mathematical operations, printing individual digits of a number, comparing two integers, determining if a number is odd or even, summing the digits of a number, generating Fibonacci series, and reversing the digits of a number. Each program is provided with input code and expected output. The exercises are aimed at practicing basic Java programming concepts.

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)
4 views15 pages

Hemant Java

The document contains several Java programming exercises including printing 'Hello', calculating the area of a rectangle, performing mathematical operations, printing individual digits of a number, comparing two integers, determining if a number is odd or even, summing the digits of a number, generating Fibonacci series, and reversing the digits of a number. Each program is provided with input code and expected output. The exercises are aimed at practicing basic Java programming concepts.

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”.


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

Output:

Program 2
a) Write a java program to find the area of a rectangle.
Input:
class arearectangle
{
public static void main(String args[])
{
int length=8;
int width=23;
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 math
{
public static void main(String[] args)
{
int a=10;
int b=20;
int c=((a<<2)+(b>>2));
int d=(a+(b*100)/10);
int e=(a&b);
[Link](c);
[Link](d);
[Link](e);
}
}

Output:
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 digits
{
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);
}
}
}

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 greaterinteger
{
public static void main(String args[])
{
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]("first one is largest");
else if (dig1<dig2)
[Link]("first one is largest");
else
[Link]("The given numbers are equal");
}}}

Output:
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 given number is odd");
else [Link]("The given number is even");
}
}

Output:
Program 4
a). Write a java program to find the sum of digits of a given number.
Input:
import [Link].*;
class sumofdigits
{
public static void main(String args[])
{
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:
Program 4
b). Write a java program to find first 15 terms of fibonacci series.
Input:
class fibonacci
{
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;
[Link](c);
}
}
}
Output:
Program 4
c). Given a number, write a program using while loop to reverse
the digits of the number.
Input:
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:

You might also like