Java Program For Performing Sum Of Last Four Digits Of A Number

import java.util.Scanner;

public class sumOfLastFourDigitsOfRollNumber {

public static void main(String [] args){

Scanner scan = new Scanner(System.in);
System.out.println("ENTER YOUR ROLL NUMBER:"):
int n = scan.nextInt();

// now we will assign our value to any other number, so that the value of that number can be change
int temp =n;
int sum =0;
// we can chnage any number to string to calculate it's length
for (int i = String.valueOf(n).length() ; i>4 ; i--){

int lastDigit = temp %10;
temp =temp /10;
sum +=lastDigit;

}
System.out.println(sum);
}
}
OR,
import java.util.Scanner;

public class sumOfLastFourDigitsOfRollNumber {

public static void main(String[] args) {

Scanner scan = new Scanner(System.in);
System.out.println("ENTER YOUR ROLL NUMBER:");
int n = scan.nextInt();
int count = String.valueOf(n).length();
// now we will assign our value to any other number, so that the value of that number can be change

int temp = n;
int sum = 0;
if(count <8){
System.out.println("ENTER YOUR ROLL NUMBER ATLEAST OF 8 DIGITS.");}

//
else {

// we can chnage any number to string to calculate it's length
for (int i = count; i > 4; i--) {

int lastDigit = temp % 10;
temp = temp / 10;
sum += lastDigit;

}

System.out.println(sum);
}
}
}


Comments

Popular posts from this blog

PROGRAM OF CALCULATING INTERSECTION OF TWO ARRAYS

JAVA PROGRAM TO CREATE A DATABASE (SAMPLE PICTURE IS GIVEN)

Craze for Fashion