[Go to site: main page, start]

0% found this document useful (0 votes)
11 views8 pages

Class 8 Java Programs

The document contains ten Java programs that demonstrate various algorithms, including swapping variables with and without a third variable, checking for leap years, buzz numbers, even or odd numbers, factorial calculation, prime number verification, digit counting, Niven number checking, and neon number checking. Each program utilizes the Scanner class for user input and outputs the results based on the specified conditions. These examples serve as practical exercises for understanding basic programming concepts in Java.

Uploaded by

Sayak Sharma
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)
11 views8 pages

Class 8 Java Programs

The document contains ten Java programs that demonstrate various algorithms, including swapping variables with and without a third variable, checking for leap years, buzz numbers, even or odd numbers, factorial calculation, prime number verification, digit counting, Niven number checking, and neon number checking. Each program utilizes the Scanner class for user input and outputs the results based on the specified conditions. These examples serve as practical exercises for understanding basic programming concepts in Java.

Uploaded by

Sayak Sharma
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

1. Program of swapping two variables with the help a third variable.

import [Link];

class Swapping {

public static void main(String[] args) {

Scanner sc=new Scanner([Link]);

[Link]("Enter first number:");

int a=[Link]();

[Link]("Enter second number:");

int b=[Link]();

[Link]("Before swapping:a="+a+",b="+b);

int t=a;

a=b;

b=t;

[Link]("Before swapping:a="+a+",b="+b);

2. Swapping of two variables without using third variable.

import [Link];

class Swapping {

public static void main(String[] args) {


Scanner sc=new Scanner([Link]);

[Link]("Enter first number:");

int a=[Link]();

[Link]("Enter second number:");

int b=[Link]();

[Link]("Before swapping:a="+a+",b="+b);

b=a+b;

a=b-a;

b=b-a;

[Link]("Before swapping:a="+a+",b="+b);

3. Program to check a year is leap year not not.

import [Link];

class LeapYear{

public static void main(String[] args) {

Scanner sc=new Scanner([Link]);

[Link]("Enter year:");

int a=[Link]();

if((a%400==0)&&((a%4==0)||(a%100!=0)))

[Link](a+" is a leap year.");

else
[Link](a+" is not a leap year.");

4. Program to check a number is buzz number or not.

import [Link];

class Buzz{

public static void main(String[] args) {

Scanner sc=new Scanner([Link]);

[Link]("Enter a number:");

int a=[Link]();

if((a%10==7)||(a%7==0))

[Link](a+" is a buzz number.");

else

[Link](a+" is not a buzz number.");

5. Program to check a number is even or odd.

import [Link];

class EvenOdd{

public static void main(String[] args) {

Scanner sc=new Scanner([Link]);

[Link]("Enter a number:");
int a=[Link]();

if(a%2==0)

[Link](a+" is an even number.");

else

[Link](a+" is an odd number.");

6. Program to find the factorial of a number.

import [Link];

class Factorial{

public static void main(String[] args) {

Scanner sc=new Scanner([Link]);

[Link]("Enter a number:");

int n=[Link]();

int p=1;

for(int i=1;i<=n;i++)

p=p*i;

[Link]("The factorial of "+n+" is="+p);

7. Program to check a number is prime or not.

import [Link];
class Primr{

public static void main(String[] args) {

Scanner sc=new Scanner([Link]);

[Link]("Enter a number:");

int n=[Link]();

int c=0;

for(int i=1;i<=n;i++)

if(n%i==0)

c++;

if(c==2)

[Link](n+ " is a prime number.");

else

[Link](n+ " is not a prime number.");

8. Program to count the number of digits in a number.

import [Link];

class CountDigit{

public static void main(String[] args) {


Scanner sc=new Scanner([Link]);

[Link]("Enter a number:");

int n=[Link]();

int num=n,c=0,d;

while(num>0){

d=num%10;

c++;

num=num/10;

[Link]("The number of digits in "+n+" is="+c);

9. Program to check a number is niven or not. (The number is


divisible by its sum of digits)

import [Link];

class Niven{

public static void main(String[] args) {

Scanner sc=new Scanner([Link]);

[Link]("Enter a number:");

int n=[Link]();

int num=n,s=0,d;

while(num>0){

d=num%10;
s=s+d;

num=num/10;

if(n%s==0)

[Link](n+" is a niven number.");

else

[Link](n+" is not a niven number.");

10. Program to check a number is neon number or not.(The


number is equal to the sum of digits of its square. Example- 9→
92→81→ 8+1=9

import [Link];

class Neon{

public static void main(String[] args) {

Scanner sc=new Scanner([Link]);

[Link]("Enter a number:");

int n=[Link]();

int sq=n,s=0,d;

while(sq>0){

d=sq%10;

s=s+d;

sq=sq/10;
}

if(n==s)

[Link](n+" is a neon number.");

else

[Link](n+" is not a neon number.");

You might also like