Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
3 views

Java Lecture-2 27-July Operations

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)
3 views

Java Lecture-2 27-July Operations

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

Safaan Hashmi

Lecture 2 27-July-2024 Sat

Practice Problems

1. Print sum of digits of a positive number

import java.util.*;

class SumofDigits {

public static void main(String[] args) {

long n,m, sumOfDigits;

Scanner sc = new Scanner(System.in);

n = sc.nextInt();

m=n;

for(sumOfDigits = 0 ; n!=0 ; n/=10) {

sumOfDigits += n%10;

System.out.println("Sum of the Digits of "+m+" is = "+sumOfDigits);

OUTPUT -:
2. WAP to check whether a given number is palindrome

import java.util.*;

class Palindrome {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

int n = sc.nextInt();

int r,sum=0,temp;

temp=n;

while(n>0){

r=n%10;

sum=(sum*10)+r;

n=n/10;

if(temp==sum)

System.out.println("palindrome number ");

else

System.out.println("not palindrome");

OUTPUT -:
3. Fibonacci series using a function

import java.util.*;

public class fibonacci{

static void Fibonacci(int N)

int num1 = 0, num2 = 1;

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

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

int num3 = num2 + num1;

num1 = num2;

num2 = num3;

public static void main(String args[])

// Given Number N

Scanner sc = new Scanner(System.in);

int N = sc.nextInt();

Fibonacci(N);

}
4. WAP to generate all the prime numbers between 1 and n where n is a value

import java.util.*;

class gfg {

static void prime_N(int N)

int x, y, flg;

System.out.println(

"All the Prime numbers within 1 and " + N

+ " are:");

for (x = 1; x <= N; x++) {

if (x == 1 || x == 0)

continue;

flg = 1;

for (y = 2; y <= x / 2; ++y) {

if (x % y == 0) {

flg = 0;

break;

if (flg == 1)

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

public static void main(String[] args)

{
Scanner sc = new Scanner(System.in);

int N = sc.nextInt();

prime_N(N);

5. Wap to sort an integer array using bubble sort

class BubbleSort {

void bubbleSort(int arr[])

int n = arr.length;

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

for (int j = 0; j < n - i - 1; j++)

if (arr[j] > arr[j + 1]) {

// swap temp and arr[i]

int temp = arr[j];

arr[j] = arr[j + 1];

arr[j + 1] = temp;

}
void printArray(int arr[])

int n = arr.length;

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

System.out.print(arr[i] + " ");

System.out.println();

public static void main(String args[])

BubbleSort ob = new BubbleSort();

int arr[] = { 64, 34, 25, 12, 22, 11, 90 };

ob.bubbleSort(arr);

System.out.println("Sorted array");

ob.printArray(arr);

You might also like