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

Revision Java Programs

Uploaded by

kartik.gupta9094
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)
20 views

Revision Java Programs

Uploaded by

kartik.gupta9094
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/ 7

#1 Factorial generation

class FactorialExample

public static void main(String args[])

int i,fact=1;

int number=5;//It is the number to calculate factorial

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

fact=fact*i; }
System.out.println("Factorial of "+number+" is: "+fact);

} }

----
#2 sum of natural
numbers

public class SumNatural {

public static void main(String[] args) {

int num = 100, sum = 0;

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


{
// sum = sum + i;
sum += i;
}

System.out.println("Sum = " + sum);


}
}
# 3 pattern program

1
12
123
1234
12345
123456
1234567
import java.util.Scanner;

public class MainClass


{ public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);

//Taking rows value from the user

System.out.println("How many rows you want in this pattern?");

int rows = sc.nextInt();

System.out.println("Here is your pattern....!!!");

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


{ for (int j = 1; j <= i; j++)
{ System.out.print(j+" ");
}
System.out.println(); }
//Close the resources

sc.close(); }}
# 4 Pattern program
1
22
333
4444
55555

import java.util.Scanner;

public class MainClass


{ public static void main(String[] args)
{ Scanner sc = new Scanner(System.in);

//Taking rows value from the user

System.out.println("How many rows you want in this pattern?");

int rows = sc.nextInt();

System.out.println("Here is your pattern....!!!");

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


{ for (int j = 1; j <= i; j++)
{ System.out.print(i+" "); }

System.out.println(); }

//Close the resources

sc.close(); }}
# 5 pattern
program
1234567
123456
12345
1234
123
12
1
1t to 7
upto
1

pattern-

import java.util.Scanner;

public class MainClass


{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);

//Taking rows value from the user

System.out.println("How many rows you want in this pattern?");

int rows = sc.nextInt();

System.out.println("Here is your pattern....!!!");


for (int i = rows; i >= 1; i--)
{
for (int j = 1; j <= i; j++)
{
System.out.print(j+" ");
}

System.out.println();
}

//Closing the resources

sc.close();
}
}

prime number
using for loop

public class Main {

public static void main(String[] args) {

int num = 29;


boolean flag = false;
for (int i = 2; i <= num / 2; ++i) {
// condition for nonprime number
if (num % i == 0) {
flag = true;
break;
} }

if (!flag)
System.out.println(num + " is a prime number.");
else
System.out.println(num + " is not a prime number."); }}
Inside the for loop, we check if the number is divisible by any number in the given range (2...num/2).

 If num is divisible, flag is set to true and we break out of the loop. This determines num is not a prime number.

 If num isn't divisible by any number, flag is false and num is a prime number.

prime number -
using while( ) loop

public class Main {

public static void main(String[] args) {

int num = 33, i = 2;


boolean flag = false;
while (i <= num / 2) {
// condition for nonprime number
if (num % i == 0) {
flag = true;
break; }
++i; }

if (!flag)
System.out.println(num + " is a prime number.");
else System.out.println(num + " is not a prime number."); }}

Table generation of any given number


upto given number of times

// To Find The Multiplication Of N Till The M rows...


import java.util.*;
class Program
{ public static void main(String args[])
{ int table,times,i,result;
Scanner sc=new Scanner(System.in);
System.out.println("Enter The Table:");
table=sc.nextInt();
System.out.println("Enter The Times:");
times=sc.nextInt();
System.out.println("The Output Is:");
for(i=1;i<=times;i++) {
result=table*i;
System.out.println(" "+result); } }}

multiplicaton table

public class MultiplicationTable {

public static void main(String[] args) {

int num = 5;
for(int i = 1; i <= 10; ++i)
{
System.out.printf("%d * %d = %d \n", num, i, num * i);
} }}

while loop - table generation

public class MultiplicationTable {

public static void main(String[] args) {

int num = 9, i = 1;
while(i <= 10)
{
System.out.printf("%d * %d = %d \n", num, i, num * i); i++; } }}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy