0% found this document useful (0 votes)
12 views5 pages

Java Programs for Number Operations

Uploaded by

raphaelshullai64
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)
12 views5 pages

Java Programs for Number Operations

Uploaded by

raphaelshullai64
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

[Link] a program in java to input a positive number from the user.

If the entered number is


negative or zero, print a suitable message. If the entered number is positive then print the
multiplication table of the number in the given format.

Sample input: enter a positive number-6

Sample output-

6x1=6

6 x 2 = 12

import [Link].*;

class Q_1

public static void main()

Scanner sc=new Scanner([Link]);

int n,i,p;

[Link]("enter a number");

n=[Link]();

if(n>0)

for(i=1;i<=20;i++)

p=n*i;

[Link](n+"x"+i+"="+p);

else

[Link]("the number is zero or negative");

}
[Link] a program to generate and print all prime numbers from 1 to n. the value of n must be taken
as input from the user.

Sample input: 20

Sample output: the prime number from 1 to n are: 2,3,5,7,11,13,17,19

import [Link].*;

class Q_2

public static void main()

Scanner sc=new Scanner([Link]);

int n,i,j,c;

[Link]("enter the last number");

n=[Link]();

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

c=0;

for(j=1;j<=i;j++)

if(i%j==0)

c++;

if(c==2)

[Link]("the prime numbers from 1 to " +n+ " are " +i);

}
[Link] switch case, write a program to perform the following task:

i) to covert a decimal number to binary

ii)to convert a binary number to decimal

import [Link].*;

class Q_3

public static void main()

Scanner sc=new Scanner([Link]);

String s1="";

int n,r,ch,p=0,s=0;

[Link]("enter 1 for decimal to binary and 2 for binary to decimal");

ch=[Link]();

switch(ch)

case 1:

[Link]("enter the decimal number");

n=[Link]();

while(n>0)

r=n%2;

s1=[Link](r)+s1;

n=n/2;

[Link]("binary eqivalent="+s1);

break;

case 2:

[Link]("enter the binary number");

n=[Link]();

while(n>0)

{
r=n%10;

s=s+(int)[Link](2,p)*r;

n=n/10;

p++;

[Link]("decimal equivalent="+s);

break;

default:[Link]("wrong choice");

}
4.

You might also like