0% found this document useful (0 votes)
21 views18 pages

Java Programs: Basics to Advanced

The document contains solutions to 10 Java programming assignments involving printing output, taking user input to perform basic math operations like addition and subtraction, checking if a number is even/odd or prime, reversing a number, using command line arguments, checking if a number is Armstrong or palindrome, printing the Fibonacci series, and demonstrating the 'this' keyword. Each assignment includes the full code solution and sample output.

Uploaded by

camoleb835
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views18 pages

Java Programs: Basics to Advanced

The document contains solutions to 10 Java programming assignments involving printing output, taking user input to perform basic math operations like addition and subtraction, checking if a number is even/odd or prime, reversing a number, using command line arguments, checking if a number is Armstrong or palindrome, printing the Fibonacci series, and demonstrating the 'this' keyword. Each assignment includes the full code solution and sample output.

Uploaded by

camoleb835
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

ASSIGNMENT - 1

1. Write a java program to print “ Hello World . “


Ans.
import [Link].*;
class a2
{
public static void main(String args[])
{
[Link]("Hello World");
}
}

Output:

2. Write a java program to take input from user and perform operation
Addition , Subtraction , Multiplication , Division .
Ans.
import [Link].*;
class add
{
public static void main(String args[]) throws IOException
{
int a,b,c;
DataInputStream x=new DataInputStream([Link]);
[Link](" Enter the value of a:");
a=[Link]([Link]());
[Link](" Enter the value of b:");
b=[Link]([Link]());
c=a+b;
[Link]("Addition of :"+c);
}
}

Output:

Subtration:
import [Link].*;
class sub
{
public static void main(String args[]) throws IOException
{
int a,b,c;
DataInputStream x=new DataInputStream([Link]);
[Link](" Enter the value of a:");
a=[Link]([Link]());
[Link](" Enter the value of b:");
b=[Link]([Link]());
c=a-b;
[Link]("Subtration of :"+c);
}
}

Output:

Multiplication :
import [Link].*;
class mul
{
public static void main(String args[]) throws IOException
{
int a,b,c;
DataInputStream x=new DataInputStream([Link]);
[Link](" Enter the value of a:");
a=[Link]([Link]());
[Link](" Enter the value of b:");
b=[Link]([Link]());
c=a*b;
[Link]("Multiplication of :"+c);
}
}

Output:

Division :
import [Link].*;
class div
{
public static void main(String args[]) throws IOException
{
int a,b,c;
DataInputStream x=new DataInputStream([Link]);
[Link](" Enter the value of a:");
a=[Link]([Link]());
[Link](" Enter the value of b:");
b=[Link]([Link]());
c=a/b;
[Link]("Division of :"+c);
}
}

Output:

3. Write a java Program to take input from user to check whether


number is even or odd .
Ans .
import [Link].*;
class condition
{
public static void main(String args[])throws IOException
{
int n;
DataInputStream x=new DataInputStream([Link]);
[Link](" Enter the value of n:");
n=[Link]([Link]());
if(n%2==0)
{
[Link]("Number is even:"+n);
}
else
{
[Link]("Number is odd:"+n);
}
}
}

Output:

4. Write a java program to take input from user check wheather number
is prime or not .
Ans.
import [Link].*;
class prime
{
public static void main(String args[])throws IOException
{
int n;
DataInputStream x=new DataInputStream([Link]);
[Link](" Enter the value of n:");
n=[Link]([Link]());
if(isprime(n))
{
[Link]("Number is Prime:"+n);
}
else
{
[Link]("Number is not prime:"+n);
}
}
public static boolean isprime(int n)
{
if(n<=1)
{
return false;
}
for(int i=2;i<=[Link](n);i++)
{
if(n%i==0)
{
return false;
}
}
return true;
}
}

5. Write a java program to take input from user any number and reverse
it .
Ans.
import [Link].*;
class rev
{
public static void main(String args[])throws IOException
{
int n,r,rev;
rev=0;
DataInputStream x=new DataInputStream([Link]);
[Link](" Enter the value of n:");
n=[Link]([Link]());
while(n!=0)
{
r=n%10;
rev=rev*10+r;
n=n/10;
}
[Link]("Reverse Number is :" +rev);
}
}

Output:

6. Write a java program to take input from user and perform addition
using command Line argument.
Ans.
import [Link].*;
class cmdl
{
public static void main(String args[])
{
int a=[Link](args[0]);
int b=[Link](args[1]);
int sum=a+b;
[Link]("Sum of two numbers:"+sum);
}
}
Output:
7. Write a java Program to take input from user and check whether the
number is Armstrong or not.
Ans.
import [Link].*;
class armstr
{
public static void main(String args[]) throws IOException
{
int n,count=0,a,b,c,sum=0;
DataInputStream x=new DataInputStream([Link]);
[Link]("enter value of n:");
n=[Link]([Link]());
a=n;
c=n;
while(a>0)
{
a = a / 10;
count++;
}
while(n > 0)
{
b = n % 10;
sum = (int) (sum+[Link](b, count));
n = n / 10;
}
if(sum == c)
{
[Link](c+ " is an Armstrong number");
}
else
{
[Link](c+ " is not an Armstrong number");
}
}
}

Output

8. Write a java Program to take input from user and check whether the
number is Palindrome or not.
Ans.
import [Link].*;
class palindrom
{
public static void main(String args[]) throws IOException
{
int r,rev=0,n,s;
DataInputStream x=new DataInputStream([Link]);
[Link]("Enter N");
n=[Link]([Link]());
s=n;
while(n!=0)
{
r=n%10;
rev=rev*10+r;
n=n/10;
}
if(rev==s)
{
[Link]("Palindrom");
}
else
{
[Link]("Not Palindrom");
}
}
}
Output:

9. Write a java Program to take input from user and print Fibonacci Series.
Ans.
import [Link].*;
class febo
{
public static void main(String args[]) throws IOException
{
int a=0, b=1,c,n,i;
DataInputStream x=new DataInputStream([Link]);
[Link]("Enter N:");
n=[Link]([Link]());
for(i=1;i<=n;i++)
{
[Link]("" +a);
c=a+b;
a=b;
b=c;
}
}
}
Output:

10. Write a java program to demonstrate this keyword.


Ans.
import [Link].*;
class a
{
int rollno;
String name;
void get(int rollno,String name)
{
[Link]=rollno;
[Link]=name;
}
void display()
{
[Link]("Enter Rollno:"+rollno);
[Link]("Enter Name:"+name);
}
}
class This
{
public static void main(String args[])
{
a a1=new a();
[Link](116,"Vijay");
[Link]();
}
}
OR Constructor This()
import [Link].*;
class add
{
int r;
String n;
add()
{
[Link]("Default constructor");
}
add(int r,String n)
{
this();
this.r=r;
this.n=n;
}
void display()
{
[Link]("Rollno:"+r);
[Link]("Name:"+n);
}
}
class c
{
public static void main(String args[])
{
add a1=new add(138,"Vijay");
[Link]();
}
}

Output:

You might also like