0% found this document useful (0 votes)
51 views

Java Lab

The document contains 8 Java programs with their aims, programs, and outputs. Program 1 finds the sum of n numbers. Program 2 finds the sum of digits of a number. Program 3 reverses a given number. Program 4 checks if a number is odd or even. Program 5 finds Armstrong numbers in a given range. Program 6 finds prime numbers within a range. Program 7 creates a menu to run the previous programs. Program 8 generates a Fibonacci series up to a given limit.

Uploaded by

Akash
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)
51 views

Java Lab

The document contains 8 Java programs with their aims, programs, and outputs. Program 1 finds the sum of n numbers. Program 2 finds the sum of digits of a number. Program 3 reverses a given number. Program 4 checks if a number is odd or even. Program 5 finds Armstrong numbers in a given range. Program 6 finds prime numbers within a range. Program 7 creates a menu to run the previous programs. Program 8 generates a Fibonacci series up to a given limit.

Uploaded by

Akash
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
You are on page 1/ 16

PROGRAM NO :1

DATE:

AIM :Write a program to find the sum of n numbers

PROGRAM

import java.util.Scanner;
class Summ
{
public static void main(String args[])
{
int a,i=1;
int s=0;
System.out.println("enter the number of integers");
Scanner scn=new Scanner(System.in);
a=scn.nextInt();
{
for(i=1;i<=a;i++)
{
s=s+i;
}
System.out.println("The Sum is" + s);
}
}
}

OUTPUT
PROGRAM NO :2
DATE:

AIM : Write a program to find the sum of digits of a number

PROGRAM

import java.util.Scanner;
class Sdigit
{
public static void main(String args[])
{
int a,n,s=0;
System.out.println("enter the digit is");
Scanner b=new Scanner(System.in);
a=b.nextInt();
{
while(a>0)
{
n=a%10;
s=s+n;
a=a/10;
}
System.out.println("the sum of digit is\n"+s);
}
}
}

OUTPUT
PROGRAM NO :3
DATE:

AIM : To reverse a given number

PROGRAM

import java.util.Scanner;
class Rev
{
public static void main(String args[])
{
int n,re=0,dig;
System.out.println("enter a number to reverse");
Scanner r=new Scanner(System.in);
n=r.nextInt();
while(n!=0)
{
dig=n%10;
re=re*10+dig;
n=n/10;
}
System.out.println("the reverse number is"+re);
}
}

OUTPUT
PROGRAM NO :4
DATE:

AIM : Write a program to check whether a given number is odd or even.

PROGRAM

import java.util.Scanner;
class Even
{
public static void main(String[] args)
{
int a;
System.out.println("enter the digit is");
Scanner b=new Scanner(System.in);
a=b.nextInt();
if(a%2==0)
{
System.out.print("the number is even\n"+a);
}
else
{
System.out.print("the number is odd\n"+a);
}
}
}

OUTPUT
PROGRAM NO :5
DATE:

AIM : Find the Armstrong numbers in a given range.

PROGRAM

import java.util.*;
public class Arms
{
public static void main(String args[])
{
int n,a,b,c,c1=0,sum=0;
Scanner s=new Scanner(System.in);
System.out.println("Enter the range:");
n=s.nextInt();
b=1;
while(b<=n)
{
sum=0;
c=b;
while(c>0)
{
a=c%10;
sum=sum+(a*a*a);
c=c/10;
}
if(sum==b)
{
System.out.println(b+" is a Armstrong Number");
c1=c1+1;
}
b++;
}
System.out.println("Total Armstrong Number in a given range is"+c1);
}
}
OUTPUT
PROGRAM NO :6
DATE:

AIM : Find the prime numbers within a range.

PROGRAM

import java.util.Scanner;
class Prime{
public static void main(String args[])
{
int i,sum,r,j,num,flag;
Scanner d = new Scanner(System.in);
System.out.println("enter the limit");
int n = d.nextInt();

if (n < 2)

System.out.println("no prime numbers");


}
System.out.println("prime numbers are");
for (i = 1; i <= n; i = i + 2)

flag = 0;

for (j = 2; j <= i / 2; j++)

if ((i % j) == 0)

flag = 1;

break;

if (flag == 0)

System.out.println(i);
}

}
}

OUTPUT
PROGRAM NO :7
DATE:

AIM : Write a program to create a menu with the choices –sum on n numbers, sum of digits
of a number, reverse of a number, check odd or even, Armstrong numbers in a given range,
prime numbers within a range

PROGRAM

import java.util.*;
public class Menu
{
public static void main(String []args)
{
Scanner sc=new Scanner(System.in);
int ch,k,i,n,sum,r,num,j;
while(true)
{
System.out.println("1. sum of n numbers");
System.out.println("2. sum of digits");
System.out.println("3. reverse");
System.out.println("4. odd or even");
System.out.println("5. amstrong");
System.out.println("6. prime numbers");
System.out.println("7. exit");

ch=sc.nextInt();
switch(ch)
{
case 1:

int s=0;
Scanner d = new Scanner(System.in);
System.out.println("enter the limit");
n = d.nextInt();
for(i=1;i<=n;++i)
{
s=s+i;
}
System.out.println("sum is "+s);

break;
case 2:

int n1,n2,m=0;

Scanner e = new Scanner(System.in);


System.out.println("enter the number");
n = e.nextInt();
while (n > 0)
{
n2=n%10;
m=m+n2;
n=n/10;
}
System.out.println("sum of "+m);
break;
case 3:
int rev=0;
Scanner f = new Scanner(System.in);
System.out.println("enter the number");
n = f.nextInt();

while (n > 0)
{
rev=rev*10;
rev=rev+n%10;
n=n/10;
}
System.out.println("reverse is"+rev);
break;
case 4:

Scanner g= new Scanner(System.in);


System.out.println("enter the number");
n = g.nextInt();
if(n%2==0)
{
System.out.println("number is even");
}
else
{
System.out.println("number is odd");
}
break;
case 5:

Scanner h = new Scanner(System.in);


System.out.println("enter the limit");
n = h.nextInt();
System.out.println("amstrong numbers are");
for(i=1;i<=n;i++)

{
num=i;
sum=0;
while(num>0)
{
r=num % 10;
sum=sum+(r*r*r);
num=num/10;
}
if(sum==i)
System.out.println(i);
}
break;
case 6:

int flag;
Scanner o = new Scanner(System.in);
System.out.println("enter the limit");
n = o.nextInt();

if (n < 2)

System.out.println("no prime numbers");


}
System.out.println("prime numbers are");
for (i = 1; i <= n; i = i + 2)

flag = 0;

for (j = 2; j <= i / 2; j++)

if ((i % j) == 0)

flag = 1;

if (flag == 0)

System.out.println(i);

}
break;

case 7:
System.exit(0);
break;

default:
System.out.println("Error");

}
}
}
}
OUTPUT
PROGRAM NO :8
DATE:

AIM :Generate Fibonacci series up to a limit.

PROGRAM

import java.util.*;
import java.io.*;
class Fib
{
public static void main(String arg[])
{
int a=0,b=1,c=a+b,i,l;
Scanner ob=new Scanner(System.in);
System.out.println("Enter limit");
l=ob.nextInt();
System.out.println("Fibonacci Series");
System.out.println(a);
System.out.println(b);
for(i=3;i<=l;i++)
{
System.out.println(c);
a=b;
b=c;
c=a+b;
}
}}

OUTPUT

You might also like