0% found this document useful (0 votes)
24 views68 pages

Sem 5

question

Uploaded by

Om Pandey
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)
24 views68 pages

Sem 5

question

Uploaded by

Om Pandey
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/ 68

1

Program1 – List of even numbers


/*
List Even Numbers Java Example
This List Even Numbers Java Example shows how to
find and list even numbers between 1 and any given
number.
*/

public class ListEvenNumbers {

public static void main(String[] args) {

//define limit
int limit = 50;

System.out.println("Printing Even numbers between


1 and " + limit);

for(int i=1; i <= limit; i++){

// if the number is divisible by 2 then it is even


if( i % 2 == 0){
System.out.print(i + " ");
}
}
}
}

/*
Output of List Even Numbers Java Example would be
Printing Even numbers between 1 and 50
2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38
40 42 44 46 48 50
*/

2
Program2 - Factorial of a number
/*
This program shows how to
calculate Factorial of a number.
*/

public class NumberFactorial {

public static void main(String[] args) {

int number = 5;

/*
* Factorial of any number is! n.
* For example, factorial of 4 is 4*3*2*1.
*/

int factorial = number;

for(int i =(number - 1); i > 1; i--)


{
factorial = factorial * i;
}

System.out.println("Factorial of a number is " +


factorial);
}
}

/*
Output of the Factorial program would be
Factorial of a number is 120
*/

3
Program3 - Compare Two Numbers using else-if
/*

Compare Two Numbers Java Example This Compare Two

Numbers Java Example shows how to compare two

numbers using if else if statements.

*/ public class

CompareTwoNumbers { public

static void main(String[]

args) {

//declare two numbers to

compare int num1 =

324; int num2 = 234;

if(num1 > num2){

System.out.println(num1 + " is greater

than " + num2); } else if(num1 < num2){

System.out.println(num1 + " is less

than " + num2); } else{

4
System.out.println(num1 + " is equal to " +
num2);

/* Output of Compare Two Numbers

Java Example would be 324 is greater

than 234

*/

Program4 - Determine If Year Is Leap Year


/*

Determine If Year Is Leap Year Java Example

This Determine If Year Is Leap Year Java

Example shows how to determine whether the

given year is leap year or not.

5
public class

DetermineLeapYearExample {

public static void

main(String[] args) { //year

we want to check int year =

2004;

//if year is divisible by 4, it is a leap


year

if(year % 400 == 0) || ((year % 4 == 0) &&


(year % 100 != 0))

System.out.println("Year " + year + " is

a leap year"); else

System.out.println("Year " + year + " is not a leap


year");

/*

6
Output of the example

would be Year 2004 is a

leap year

*/

Program5 - Fibonacci Series


/* Fibonacci Series Java Example
This Fibonacci Series Java Example shows how to
create and print Fibonacci Series using Java.
*/

public class JavaFibonacciSeriesExample {

public static void main(String[] args) {

//number of elements to generate in a


series int limit = 20;
long[] series = new
long[limit];

//create first 2 series elements


series[0] = 0;
series[1] = 1;

//create the Fibonacci series and store it


in an array for(int i=2; i < limit; i++){
series[i] = series[i-1] + series[i-2];
}

//print the Fibonacci series numbers

7
System.out.println("Fibonacci Series upto
" + limit); for(int i=0; i< limit; i++){
System.out.print(series[i] + " ");
}
}
}

/*
Output of the Fibonacci Series Java Example would be
Fibonacci Series upto 20
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597
2584 4181
*/

Program6 - Palindrome Number


/*
This program shows how to check for in the given
list of numbers
whether each number is palindrome or not
*/
public class JavaPalindromeNumberExample {

public static void main(String[] args) {

//array of numbers to be checked


int numbers[] = new int[]{121,13,34,11,22,54};

//iterate through the numbers


for(int i=0; i < numbers.length;
i++){

int number = numbers[i];


int reversedNumber = 0;
int temp=0;

8
/*
* If the number is equal to it's reversed number, then
* the given number is a palindrome number.
*
* For ex,121 is a palindrome number while 12 is not.
*/
//reverse the number
while(number > 0){ temp =
number % 10;
number = number
/ 10;
reversedNumber = reversedNumber * 10
+ temp;
}
if(numbers[i] == reversedNumber)
System.out.println(numbers[i] + " is a palindrome");
else
System.out.println(numbers[i] + " not a palindrome
");
}

}
}

/*
Output of Java Palindrome Number Example would be
121 is a palindrome number
13 is not a palindrome number
34 is not a palindrome number
11 is a palindrome number
22 is a palindrome number
54 is not a palindrome number
*/

9
Program7- Generate prime numbers
between 1 & given number
/*
Prime Numbers Java Example
This Prime Numbers Java example shows how to
generate prime numbers between 1 and given
number using for loop.
*/

public class GeneratePrimeNumbersExample {

public static void main(String[] args) {

//define limit
int limit = 100;

System.out.println("Prime numbers between 1 and " +


limit);

//loop through the numbers one by one


for(int i=1; i < 100; i++){

boolean isPrime = true;

//check to see if the number is prime


for(int j=2; j < i ; j++){

if(i % j == 0){ isPrime


= false;
break;
}
}
// print the number
if(isPrime)
System.out.print(i + " ");
}
}

10
}

/*
Output of Prime Numbers example would be
Prime numbers between 1 and 100
1 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61
67 71 73 79 83 89 97
*/

Program8- Pyramid of stars using nested for loops


/*

Java Pyramid 1 Example


This Java Pyramid example shows how to generate
pyramid or triangle like given below using for
loop.

*
**
***
****
*****
*/

public class JavaPyramid1 {

public static void main(String[] args) {

for(int i=1; i<= 5 ;i++){

for(int j=0; j < i; j++){


System.out.print("*");
}
//generate a new line
System.out.println("");

11
}
}
}

/*
Output of the above program would be
*
**
***
****
*****
*/
Program9 – Reversed pyramid using for
loops & decrement operator.
/*
Java Pyramid 5 Example
This Java Pyramid example shows how to generate
pyramid or triangle like given below using for
loop.

12345
1234
123
12
1

*/

public class JavaPyramid5 {

public static void main(String[] args) {

for(int i=5; i>0 ;i--){

for(int j=0; j < i; j++){


System.out.print(j+1);
12
}

System.out.println("");
}

}
}

/*

Output of the example would be


12345
1234
123
12
1

*/

Program10 - Nested Switch


/*

Statements Example This example shows how

to use nested switch statements in a java

program.

*/

public class

NestedSwitchExample { public

13
static void main(String[]

args) {

/*

* Like any other Java statements,

switch statements * can also be

nested in each other as given in *

below example.

*/

int i = 0;

int j =

1;

switch(i)

case 0:

switch(j)

case 0:

System.out.println("i is 0, j is 0");

14
break;

case 1:

System.out.println("i is 0, j is 1");

break;

default:

System.out.println("nested
default case!!");

break;

default:

System.out.println("No matching case


found!!");

15
/* Output

would be, i

is 0, j is

*/
Program11 - Calculate Circle Area
using radius
/*
This program shows how to
calculate area of circle
using it's radius.
*/

import
java.io.BufferedReader;
import
java.io.IOException;
import
java.io.InputStreamRead
er;

public class CalculateCircleAreaExample {


public static void
main(String[] args) {
int radius = 0;
System.out.println("Please enter radius of a
circle");
try {
//get the radius from console
BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
radius =

16
Integer.parseInt(br.readLine(
));
}
//if invalid value was entered
catch(NumberFormatException ne)
{
System.out.println("Invalid radius value"
+ ne);
System.exit(0);
}
catch(IOException ioe)
{
System.out.println("IO Error :" + ioe);
System.exit(0);
}

/*
* Area of a circle is
* pi * r * r * where r is a radius
of a circle.
*/

//NOTE : use Math.PI constant to get


value of pi double area = Math.PI *
radius * radius;

System.out.println("Area of a circle is " +


area);
}
}

/*
Output of Calculate Circle Area using Java Example
would be
Please enter radius of a circle
19
Area of a circle is 1134.1149479459152
*/
17
Program12 - Factorial of a number using recursion
/*
This program shows how to
calculate Factorial of a number
using recursion function.
*/
import
java.io.BufferedReader;
import
java.io.IOException;
import
java.io.InputStreamReader
;

public class JavaFactorialUsingRecursion {


public static void main(String args[]) throws
NumberFormatException,
IOException{

System.out.println("Enter the number: ");

//get input from the user


BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
int a = Integer.parseInt(br.readLine());

//call the recursive function to generate factorial


int result= fact(a);

System.out.println("Factorial of the number is: " +


result);
}

static int fact(int b)


{

18
if(b <= 1)
//if the number is 1 then return 1
return 1;
else
//else call the same function with the value -
1 return b * fact(b-1);
}
}

/*
Output of this Java example would be
Enter
the number:
5
Factorial of the number is: 120
*/
Program13 – pyramid of numbers using
for loops
/*

Generate Pyramid For a Given Number Example This

Java example shows how to generate a pyramid of

numbers for given number using for loop example.

*/ import java.io.BufferedReader; import

java.io.InputStreamReader; public class

GeneratePyramidExample { public static

void main (String[] args) throws

Exception{

19
BufferedReader keyboard = new
BufferedReader (new
InputStreamReader(System.in));

System.out.println("Enter Number:");

int as= Integer.parseInt

(keyboard.readLine());

System.out.println("Enter X:");

int x= Integer.parseInt

(keyboard.readLine());

int y = 0;

for(int i=0; i<= as ;i++){

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

System.out.print(y

+ "\t"); y = y + x;

System.out.println("");

20
}

/* Output of this

example would be

Enter

Number:

Enter

X:

1 2
3 4 5
6 7 8 9
10 11 12 13 14
--------------------------------

-------------Enter Number:

21
5

Enter

X:

2 4
6 8 10

12 14 16 18
20 22 24 26 28
--------------------------------

--------------

Enter

Number:

Enter

X:

3 6
22
9 12 15

18 21 24 27
30 33 36 39 42
*/
Program14 – To Find Maximum of Two Numbers.
/*

To Find Maximum of 2 Numbers using if else

*/ class Maxoftwo{ public static

void main(String args[]){

//taking value as command line

argument. //Converting String

format to Integer value int i =

Integer.parseInt(args[0]); int j

= Integer.parseInt(args[1]);

if(i > j)

System.out.println(i+" is greater than

"+j); else

System.out.println(j+" is greater than

"+i);

23
}

Program15 – To Find Minimum of Two


Numbers using conditional operator
/*

To find minimum of 2 Numbers using ternary


operator

*/ class Minoftwo{

public static void main(String

args[]){ //taking value as

command line argument.

//Converting String format to

Integer value int i =

Integer.parseInt(args[0]);

int j =

Integer.parseInt(args[1]); int

result = (i<j)?i:j;

System.out.println(result+" is a

minimum value");

24
Program 16
/* Write a program that will read a float type value
from the keyboard and print the following output.
->Small Integer not less than the number.

->Given Number.

->Largest Integer not greater

than the number. */ class

ValueFormat{ public static void

main(String args[]){ double i

= 34.32; //given number

System.out.println("Small Integer not greater


than the number :
"+Math.ceil(i));

System.out.println("Given Number
: "+i);

System.out.println("Largest Integer not greater


than the number :
"+Math.floor(i));

Program 17 - Write a program to


generate 5 Random nos. between 1 to
100, and it should not follow with
decimal point.
25
class RandomDemo{ public static void

main(String args[]){ for(int

i=1;i<=5;i++){

System.out.println((int)(Math.random()*100

));

}
}

Program 18 - Write a program to


display a greet message according to
Marks obtained by student.
class SwitchDemo{ public

static void main(String args[]){

int marks = Integer.parseInt(args[0]);

//take marks as command line argument.

switch(marks/10){ case 10:

case 9: case 8:

System.out.println("Excellent");

break; case 7:

26
System.out.println("Very Good");

break; case 6:

System.out.println("Good");

break; case 5:

System.out.println("Work Hard");

break; case 4:

System.out.println("Poor");

break; case 3:

case 2: case 1:

case 0:

System.out.println("Very Poor");

break; default:

System.out.println("Invalid value Entered");

Program 19 - Write a program to find


SUM AND PRODUCT of a given Digit.

27
class Sum_Product_ofDigit{

public static void main(String

args[]){ int num =

Integer.parseInt(args[0]);

//taking value as command line argument.

int temp = num,result=0;

//Logic for sum of digit while(temp>0){

result = result + temp; temp--;

} System.out.println("Sum of Digit for

"+num+" is : "+result); //Logic for

product of digit temp = num;

result = 1; while(temp > 0){

result = result * temp; temp--;

} System.out.println("Product of Digit for

"+num+" is : "+result);

28
Program 20 - Write a program to find
sum of all integers greater than 100
and less than 200 that are divisible
by 7
class SumOfDigit{ public static void

main(String args[]){ int result=0;

for(int i=100;i<=200;i++){

if(i%7==0) result+=i;

} System.out.println("Output of

Program is : "+result);

Program 21 - Write a program to


concatenate string using for Loop
Example:

Input - 5
Output - 1 2 3 4 5 */

class Join{ public static

void main(String args[]){

int num =

29
Integer.parseInt(args[0]);

String result = " ";

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

result = result + i + " ";

System.out.println(result);

Program 22 - Program to Display


Multiplication Table
class MultiplicationTable{ public

static void main(String args[]){ int

num = Integer.parseInt(args[0]);

System.out.println("*****MULTIPLICATION

TABLE*****"); for(int

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

j=1;j<=num;j++){

System.out.print(" "+i*j+" ");

30
}

System.out.print("\n");

Program 23 - Write a program to Swap


the values
class Swap{ public static void

main(String args[]){ int num1 =

Integer.parseInt(args[0]); int

num2 = Integer.parseInt(args[1]);

System.out.println("\n***Before

Swapping***");

System.out.println("Number 1 :

"+num1);

System.out.println("Number 2 :

"+num2); //Swap logic

num1 = num1 + num2; num2 =

num1 - num2; num1 = num1 -

31
num2;

System.out.println("\n***After

Swapping***");

System.out.println("Number 1 :

"+num1);

System.out.println("Number 2 : "+num2);

Program 24 - Write a program to


convert given no. of days into months
and days.(Assume that each month is of
30 days)
Example :

Input - 69 Output - 69 days =

2 Month and 9 days */ class DayMonthDemo{

public static void main(String args[]){ int

num = Integer.parseInt(args[0]); int days =

num%30; int month = num/30;

System.out.println(num+" days = "+month+" Month and

"+days+" days");
32
}

Program 25 - Write a program to


Display Invert Triangle using while
loop.
Example:

Input -

Output :

5 5 5 5 5

4 4 4 4

3 3 3

2 2

1
*/ class InvertTriangle{

public static void main(String

args[]){ int num =

Integer.parseInt(args[0]);

while(num > 0){

33
for(int j=1;j<=num;j++){

System.out.print(" "+num+" ");

System.out.print("\n");

num--; }

Program 26 - Write a program to find


whether given no. is Armstrong or not.
Example :

Input - 153 Output - 1^3 +

5^3 + 3^3 = 153, so it is Armstrong no. */ class

Armstrong{ public static void main(String

args[]){ int num =

Integer.parseInt(args[0]); int n = num;

//use to check at last time int

check=0,remainder; while(num > 0){

remainder = num % 10;

34
check = check +

(int)Math.pow(remainder,3); num

= num / 10;

if(check

== n)

System.out.println(n+" is an

Armstrong Number"); else

System.out.println(n+" is not a Armstrong

Number");

Program 27 - switch case demo


Example :

Input - 124

Output - One Two Four */ class

SwitchCaseDemo{ public static

void main(String args[]){

35
try{ int num =

Integer.parseInt(args[0]);

int n = num; //used at last time

check int

reverse=0,remainder;

while(num > 0){

remainder = num % 10;

reverse = reverse * 10 + remainder;

num = num / 10;

}
String result=""; //contains the

actual output while(reverse > 0){

remainder = reverse % 10;

reverse = reverse / 10;

switch(remainder){

case 0 :

result =

result + "Zero ";

break; case 1 :

36
result =

result + "One ";

break; case 2 :

result =

result + "Two ";

break; case 3 :

result =

result + "Three ";

break; case 4 :

result =

result + "Four ";

break; case 5 :

result =

result + "Five ";

break; case 6 :

result =

result + "Six ";

break;

37
case 7 :

result =

result + "Seven ";

break; case 8 :

result = result + "Eight ";

break; case 9 :

result =

result + "Nine ";

break; default:

result="";

System.out.println(result);

}catch(Exception e){

System.out.println("Invalid Number

Format");

}
38
}

Program 28 - Write a program to


generate Harmonic Series.
Example :

Input - 5 Output - 1 + 1/2 +

1/3 + 1/4 + 1/5 = 2.28 (Approximately) */ class

HarmonicSeries{

public static void main(String args[]){

int num = Integer.parseInt(args[0]); double

result = 0.0; while(num > 0){

result = result + (double) 1 / num;

num--; }

System.out.println("Output of Harmonic Series

is "+result);

39
Program 29 - Write a program to find
average of consecutive N Odd no. and
Even no.
class EvenOdd_Avg{ public static void

main(String args[]){ int n =

Integer.parseInt(args[0]); int

cntEven=0,cntOdd=0,sumEven=0,sumOdd=0;

while(n > 0){ if(n%2==0){

cntEven++; sumEven = sumEven +

n; } else{

cntOdd++; sumOdd = sumOdd + n;

} n--; } int

evenAvg,oddAvg; evenAvg =

sumEven/cntEven; oddAvg = sumOdd/cntOdd;

System.out.println("Average of first N Even no

is "+evenAvg);

System.out.println("Average of first N Odd no

is "+oddAvg);

40
}

Program 30 - Display Triangle as


follow.
1

2 3

4 5 6 7 8 9 10 ... N */ class

Output1{ public static void

main(String args[]){ int

c=0; int n =

Integer.parseInt(args[0]);

loop1: for(int i=1;i<=n;i++){

loop2: for(int j=1;j<=i;j++){

if(c!=n){

c++;

System.out.print(c+" ");

} else

break loop1; }

System.out.print("\n");

41
}

Programs 31 - Write a program to Find


whether number is Prime or Not.
class PrimeNo{ public static void

main(String args[]){ int num =

Integer.parseInt(args[0]); int flag=0;

for(int i=2;i<num;i++){

if(num%i==0) {

System.out.println(num+" is not a Prime

Number"); flag = 1;

break; } }

if(flag==0)

System.out.println(num+" is a Prime Number");

Program 32 - Write a program to find


whether no. is palindrome or not.

42
Example :

Input - 12521 is a palindrome no.

Input - 12345 is not a palindrome no. */

class Palindrome{ public static void

main(String args[]){ int num =

Integer.parseInt(args[0]); int n =

num; //used at last time check int

reverse=0,remainder; while(num > 0){

remainder = num % 10; reverse =

reverse * 10 + remainder; num =

num / 10; } if(reverse ==

n) System.out.println(n+" is a

Palindrome Number"); else

System.out.println(n+" is not a Palindrome

Number");

}
Program 33 - Display Triangle as
follow

43
0

1 0

1 0 1 0 1 0 1 */ class Output2{

public static void main(String args[]){

for(int i=1;i<=4;i++){

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

System.out.print(((i+j)%2)+" ");

System.out.print("\n");

44
Program 34 Display odd
numbers between1--100
class OddNumber {
public static void main(String args[]) {
System.out.println("The Odd Numbers
are:"); for (int i = 1; i <= 100; i++) {
if (i % 2 != 0) {
System.out.print(i + " ");
}
}
}
}

Program 35 Sum of odd numbers


between 1 -100

class SumOfNum
{
public static void main(String args[])
{ int
sum = 0;
for (int i = 1; i <= 100; i++)
{
if (i % 2 != 0)
{

45
sum = sum + i;
}
}
System.out.println("The Sum Of 100 Odd Numbers are:" + sum);
}
}

Total number of odd numbers


between 1 -100

class TotalNumOfOddNum
{
public static void main(String args[])
{ int count = 0;
for(int i = 1;i <= 100;i++)
{
if(i % 2 != 0)
{
count++;
}
}
System.out.println("The Count Of Odd Numbers are:" + count);
}
}

46
Program 36 Find sum of first n
numbers
class SumOfNum
{
public static void main(String args[])
{ int
sum = 0; int
n=10;
for(int i = 1;i <= n;i++)
{
sum = sum + i;
}
System.out.println("The Sum Of "+n+" Numbers are:" + sum);
}
}

Program 37 Find the sum


of the digits of a number
public class DigitsSum
{

public static void main(String[] args)


{
int num=251025, rem = 0, sum = 0, temp;
temp = num;

47
while (num > 0)
{
rem = num % 10;
sum = sum + rem;
num = num / 10;
}

System.out.print("Sum of Digits of " + temp + " is " + sum);


}

Program 38 Calculate electricity bill


Public class ElectricBill
{
public static void main(String args[])
{ int
units = 123;
int bill = 0;

if (units > 100)


{
if (units >= 200)
{
if (units > 300)
{
bill = units * 8;

48
}
else
bill = units * 6;
}
else
bill = units * 5;
}

System.out.println("CHENNAI ELECTRICITY LTD, CHENNAI");


System.out.println("Units Consumed : " + units);
System.out.println("Total Bill : " + bill);
}
}

Program39 to find Armstrong number


public class ArmstrongNumber
{
public static void main(String args[])
{
int n, arg, sum = 0, r;

n = 153; // input
value arg = n;
for (int i = 1; i < n; i++)
{
while (n > 0)

49
{
r = n % 10;
sum = sum + (r * r * r);
n = n / 10;

}
if (arg == sum)
{
System.out.println("Given number is armstrong number: " + arg);
}
else
{
System.out.println("Given number is not armstrong number: " +
arg);
}
}

Program 40 to print Armstrong


number between 1 to 1000
public class ArmstrongNumbers
{

public static void main(String[] args)

50
{

int num,rem,limit=1000, sum = 0;


System.out.print("Armstrong numbers
from 1 to N:"); for (int i = 1; i <= limit;
i++)
{
num = i;
while (num > 0)
{
rem = num % 10;
sum = sum + (rem*rem*rem);
num = num / 10;
}

if (sum == i)
{
System.out.print(i + " ");
}
sum = 0;
}
51
}

}
Program 41 Print
given number in
words
public class NumberToWords
{

public void pw(int n, String ch)


{
String one[] = { " ", " One", " Two", " Three", " Four", " Five", " Six", " Seven", "
Eight", "
Nine", " Ten"," Eleven", " Twelve", " Thirteen", " Fourteen", "Fifteen", " Sixteen", "
Seventeen", " Eighteen"," Nineteen" };

String ten[] = { " ", " ", " Twenty", " Thirty", " Forty", " Fifty", " Sixty", "Seventy",
" Eighty", " Ninety" };

if (n > 19)
{
System.out.print(ten[n / 10] + " " + one[n % 10]);
}
else
{

52
System.out.print(one[n]);
}
if (n > 0)
System.out.print(ch);
}

public static void main(String[] args)


{

int n=28;
System.out.print(n);
if (n <= 0)
{
System.out.println("Enter numbers greater than 0");
}
else
{
NumberToWords a = new NumberToWords();
a.pw((n / 1000000000), " Hundred");
a.pw((n / 10000000) % 100, " crore");
a.pw(((n / 100000) % 100), " lakh");
a.pw(((n / 1000) % 100), " thousand");
a.pw(((n / 100) % 10), " hundred");
a.pw((n % 100), " ");
}
}
53
Program 42 to check the given
number is Palindrome or not
public class PalindromeNumberCheck
{

public static void main(String[] args)


{
int n=121,pal,r,rev=0;
pal = n;

while (n > 0)
{
r = n % 10;
rev = rev * 10 + r;
n = n / 10;

if (rev == pal)
{
System.out.println(" The given no is palindrome "+ rev);
}
else
{
System.out.println("The given no is not palindrome " + rev);

54
}

Program 43 to print palindrome


number upto N numbers

public class PalindromeUptoN


{

public static void main(String[] args)


{
int n, b, rev = 0;
int limit=50;
System.out.print("Palindrome
numbers from 1 to N:");
for (int i = 1; i <= limit; i++)
{
n = i;
while (n > 0)
{
b = n % 10;
rev = rev * 10 + b;
n = n / 10;
}
if (rev == i)

55
{
System.out.print(i + " ");
}
rev = 0;
}

}
Program 44 to print N prime numbers
and find sum and average
public class PrimeNumberUptoN
{

public static void main(String[] args)


{
int num =0, i =0;
System.out.println("Prime numbers from 1 to 100
are :"); for (i = 1; i <= 100; i++)
{
int counter=0;
for(num =i; num>=1; num--)
{
if(i%num==0)
{
counter = counter + 1;
}
56
}
if (counter ==2)
{
System.out.print(i+" ");
}
}
}
}

Program 45 to print patterns of


numbers and stars
*
* *
* * *
* * * *

public class PyramidPattern1


{
public static void main(String[] args)
{
int n=4;
for(int i=0;i<n;i++)
{
System.out.println("\n");
for(int j=0;j<=i;j++)
{
System.out.print(" * ");
57
}
}

Program 46
*
**
***
****
*****

public class PyramidPattern2


{
public static void main(String args[])
{ int i,
j, k=8;
for(i=0; i<5; i++)
{
for(j=0; j<k; j++)
{
System.out.print(" ");

}
k=k-
2;
for(j=0; j<=i; j++)
{

58
System.out.print("* ");
}
System.out.println();
}

Program 47

***************
******* *******
****** ******
***** *****
**** ****
*** ***
** **
* *
* *
** **
*** ***
**** ****
***** *****
****** ******
******* *******
59
***************

public class DifferentPatternPrograms1


{
public static void main(String args[])
{
int n,i,j,k,l,m,p,q,r,s;
Scanner sc=new Scanner(System.in);
System.out.println("Enter the n values");
n=sc.nextInt();
p=n;
q=n;
for(i=n;i>=1;i--)
{
for(j=1;j<=i;j++)
{
System.out.print("*");
}
for(k=p*2;k<n*2-1;k++)
{
System.out.print(" ");
}
for(l=i;l!=0;l--)
{
if(l==n)

60
{
continue;
}
System.out.print("*");
}
p--;
System.out.println();
}
for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
{
System.out.print("*");
}
for(k=q*2-2;k>1;k--)
{
System.out.print(" ");
}
for(m=i;m!=0;m--)
{
if(m==n)
{
continue;
}

61
System.out.print("*");
}
System.out.println();
q--;
}

Program 48
Print Floyds
triangle
import
java.util.Scanner;

class FloydsTriangle
{
public static void main(String args[])
{

Scanner scan = new Scanner(System.in);


System.out.println("Enter the number of rows\n");

int rows = scan.nextInt();


System.out.println("Floyd's Triangle Generated\n");
int count = 1;
for ( int i = 1 ; i <= rows ; i++ )

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

count++;
}

System.out.println(); 53
2 58
456 789 Error! Bookmark not defined.
11 12 13 14 Error! Bookmark not defined.

}
}
}
1
16 17 18 19 20 21

63
Program 49 to remove duplicate
words in given string
public class RemoveDuplicate
{

public static void main(String[] args)


{
String input="Welcome to Java Session Java Session Session Java";
String[] words=input.split(" ");
for(int i=0;i<words.length;i++)
{
if(words[i]!=null)
{

for(int j=i+1;j<words.length;j++)
{

if(words[i].equals(words[j]))
{
words[j]=null;
}
}
}
}
for(int k=0;k<words.length;k++)

64
{
if(words[k]!=null)
{
System.out.println(words[k]);
}

}
Program 50 to count each words and total
number of words in given string

import java.io.IOException;

public class FindTtalCountWords


{

public static void main(String args[]) throws IOException


{
countWords("apple banna apple fruit fruit apple hello hi hi hello hi");

static void countWords(String st)


{

65
String[] words =
st.split("\\s"); int[] fr = new
int[words.length];
for (int i = 0; i < fr.length; i++)
fr[i] = 0;
for (int i = 0; i < words.length; i++)
{
for (int j = 0; j < words.length; j++)
{
if (words[i].equals(words[j]))
{

fr[i]++;

}
}
}

for (int i = 0; i < words.length; i++)


{
for (int j = 0; j < words.length; j++)
{
if (words[i].equals(words[j]))
{
if (i != j)
{

66
words[i] = "";
}
}
}
int total = 0;
System.out.println("Words and words
count:"); for (int i = 0; i < words.length; i++)
{
if (words[i] != "")
{
System.out.println(words[i] + "=" + fr[i]);

total += fr[i];

}
}
System.out.println("Total words counted: " + total);
}
}

67
}

68

You might also like