Комментарии:
I saved the four digits as String in num then use the num.charAt() in the if statement
ОтветитьSystem.out.println("enter your for digit lucky number one by one :");
int a = input.nextInt();
int b = input.nextInt();
int c = input.nextInt();
int d = input.nextInt();
if(a + b == c + d)
System.out.println("The number " + a + b + c + d + " is a lucky number since " + (a + "+" + b + "=" + c + "+" + d ));
else
System.out.println("The number " + a + b + c + d + " is not a lucky number since " + (a + "+" + b + "!=" + c + "+" + d ));
OUTPUT :
enter your for digit lucky number one by one :
3
7
1
9
The number 3719 is a lucky number since 3+7=1+9
Process finished with exit code 0
was this optimised code or not?
package loopies;
import java.util.Scanner;
public class Questionn1 {
public static void main(String[] args) {
Scanner scanner = new Scanner (System.in);
System.out.println("Enter a number");
int num1 = scanner.nextInt();
System.out.println("Enter a number");
int num2 = scanner.nextInt();
System.out.println("Enter a number");
int num3 = scanner.nextInt();
System.out.println("Enter a number");
int num4 = scanner.nextInt();
if ( num1 + num2 == num3 + num4) {
System.out.println("LUCKYY");
}
else {
System.out.println("nope");
}
scanner.close();
}
}
ORR u could do this its way easier
import java.util.Scanner;//call the scanner
public class Main {// class
public static void main(String[] args) {//main method
try( Scanner uScanner = new Scanner(System.in)){
System.out.println("give me a four-digit number");
String fourDigits = uScanner.next();
if( fourDigits.charAt(4)!= -1)
System.out.println("this is not a four-digit number");
else
if(fourDigits.charAt(0) + fourDigits.charAt(1) == fourDigits.charAt(2) + fourDigits.charAt(3))
System.out.println(fourDigits + " is a lucky number ");
else
System.out.println(fourDigits +" is not a lucky number");
}
}
} (another way to solve the exercise)
import java.util.Scanner;
class Main {
public static void main (String[] args){
Scanner input = new Scanner(System.in);
System.out.println("Enter four number ");
String num = input.nextLine();
char a = num.charAt(0);
char b = num.charAt(1);
char c = num.charAt(2);
char d = num.charAt(3);
if ((a+b)==(c+d))
System.out.println("The given number is Lcuky ");
else
System.out.println("The number is not Lucky");
}
}
It is very simple by using this
import java.util.Scanner;
public class luckynumber {
public static void main(String[] args) {
System.out.println("enter the four digit number");
Scanner sc= new Scanner(System.in);
int a= sc.nextInt();
int b= sc.nextInt();
int c= sc.nextInt();
int d= sc.nextInt();
if(a+b==c+d)
System.out.println("it is a lucky number");
else
System.out.println("It is not a lucky number");
}
}
So i solved this exercise very differently, i captured the four digit integer and converted it to String by concatenating it with an empty string, then I used the substring to extract each number one by one..... overall this lessons are so good
Ответитьimport java.util.Scanner;
public class Main { public static void main(String[] args) {
Scanner num = new Scanner(System.in); System.out.println("please enter a four digit positive number to find if its lucky : ");
int n1 = num.nextInt(); if (n1>9999 || n1<0) {
int d1 = n1 / 1000; int d2 = (n1 / 100) % 10;
int d3 = (n1 / 10) % 10; int d4 = n1 % 10;
if (d1 + d2 == d3 + d4) { System.out.println("Congrats its a lucky number !!!");
} else {
System.out.println("its not a lucky number"); }
}
else {
System.out.println("Invalid number !"); }
}}
I know it is not perfect solution but I do it this way .
System.out.print("4 adet rakam giriniz :");
Scanner ds= new Scanner(System.in);
int a = ds.nextInt();
int b = ds.nextInt();
int c = ds.nextInt();
int d = ds.nextInt();
if(a+b== c+d)
System.out.println("Girdiğiniz sayı bir Lucky Number ");
else if(!(a+b== c+d))
System.out.println("Girdiğiniz sayı bir Lucky Number değildir.");
My mind was blown away🤯
Ответитьimport java.util.Scanner;
public class Luckyno {
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
System.out.print("enter an integer: ");
String a=in.next();
if((a.charAt(0))+(a.charAt(1))==(a.charAt(2))+(a.charAt(3)))
System.out.println("thats a lucky number");
else
System.out.println("not lucky number");
}
}
import java.util.Scanner;
public class lucky_number {
public static void main(String[] args) {
System.out.print("Enter a 4 digit number: ");
Scanner s=new Scanner(System.in);
int a = s.next().charAt(0);
int b = s.next().charAt(0);
int c = s.next().charAt(0);
int d = s.next().charAt(0);
if(a + b == c + d) {
System.out.print("It is a lucky number");
}
else {
System.out.print("not a lucky number");
}
}
}
IS IT VALID OR NOT
int n1 = s.nextInt();
int n2 = s.nextInt();
int n3 = s.nextInt();
int n4 = s.nextInt();
if ( (n1 + n2 == (n3 + n4))) {
System.out.println("Lucky number");
}
else {
System.out.println("unlucky");
}
package ayushi;
import java.util.Scanner;
public class Main {
public static void main(String[] arg) {
Scanner intput = new Scanner(System.in);
System.out.println("enter your number:");
int a =intput.nextInt();
int fdn=a/10;
int tdn=(a/10)%10;
int sdn=(a/100)%10;
int fd=(a/1000)%10;
if (fd+sdn==tdn+fdn)
System.out.println("lucky");
else
System.out.println("uncluky");
}
}
I love you Neso Academy. ❤️
Ответитьmy solution:
public class LuckyNumber {
public static void main(String[] args) {
System.out.print("Please input a four-digit number (1000 - 9999): ");
Scanner input = new Scanner(System.in);
String s = input.next();
if (s.charAt(0) + s.charAt(1) == s.charAt(2) + s.charAt(3))
System.out.println(s + " is a lucky number");
else
System.out.println(s + " is not a lucky number!");
}
}
public class Main {
public static void main(String[] args) {
System.out.print("Enter 4 Digit Numbers : ");
Scanner input = new Scanner(System.in);
String s1 = input.next();
int s2 = s1.length();
if (s2 == 4) {
int s3 = (s1.charAt(0) + s1.charAt(1));
int s4 = (s1.charAt(2) + s1.charAt(3));
if (s3==s4)
System.out.println("Numbers Are Luckey Numbers");
else
System.out.println("Numbers Are Not Luckey Numbers");}
else
System.out.println("Please Enter 4 Digit Number");
}
}
import java.util.Scanner;
public class Main{
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter a four digit number: ");
String num = input.next();
char first = num.charAt(0);
char sec = num.charAt(1);
char third = num.charAt(2);
char fourth = num.charAt(3);
if (num.length() == 4 && first != '0') {
if (first + sec == third + fourth)
System.out.println("You're lucky baby!");
else
System.out.println("Not so lucky!");
}
else
System.out.println("Invalid number.");
}
}
import java.util.Scanner;
public class a{
public static void main(String[]args){
Scanner n=new Scanner(System.in);
System.out.println("Enter a four digit number");
String a=n.next();
if((a.charAt(0)+a.charAt(1))==(a.charAt(2)+a.charAt(3)))
{
System.out.println("Lucky");
}
else
{
System.out.println(" not Lucky");
}
}
}
Here is my solution, which worked. Now I am going to check your solution:
public class Main {
public static void main(String[] args) {
System.out.print("enter a four-digit number :");
Scanner s = new Scanner(System.in);
int number = s.nextInt();
int d1 = number/1000;
number = number - d1*1000;
int d2 = number/100;
number = number - d2*100;
int d3 = number/10;
number = number - d3*10;
int d4 = number;
if ((d1+d2) == (d3+d4))
System.out.println("Your number is lucky number");
else
System.out.println("Your number is not a lucky number");
}
}
import java.util.Scanner;
public class Main
{
public static void main(String[] args) {
System.out.println("Enter a 4 Digit Number ");
Scanner sc = new Scanner(System.in);
String i = sc.next();
char num1 = i.charAt(0);
char num2 = i.charAt(1);
char num3 = i.charAt(2);
char num4 = i.charAt(3);
if(num1+num2 == num3+num4)
System.out.println("Number is Lucky");
else
System.out.println("Number is Not Lucky");
}}
The condition " < 999" is not the perfect one since you can use a 4 digit number less than 999, for example, 0123. The video was great though.
ОтветитьCan you put some simple examples instead of modulo
ОтветитьI just took a very different approach to solve this and it does works fine. Please have a look🙏🏻
import java.util.Scanner;
public class App {
public static void main(String[] args) throws Exception {
System.out.print("Enter a four digit number -: ");
Scanner input = new Scanner(System.in);
String x = input.next();
if ((x.length()) != 4) {
System.out.println(x + " is not a four digit number");
} else {
int a = ((int) x.charAt(0)) - 48;
int b = ((int) x.charAt(1)) - 48;
int c = ((int) x.charAt(2)) - 48;
int d = ((int) x.charAt(3)) - 48;
if ((a + b) == (c + d))
System.out.println(x + " is a lucky number since " + a + " + " + b + " = " + c + " + " + d);
else
System.out.println(x + " is not a lucky number since " + a + " + " + b + " != " + c + " + " + d);
}
}
}
Please let me know if i did some mistake
great stuff
ОтветитьYou have made java easy language to learn, thanks alot.
ОтветитьSystem.out.print("Enter 4 Numbers: ");
int n1 = sc.nextInt();
int n2 = sc.nextInt();
int n3 = sc.nextInt();
int n4 = sc.nextInt();
int sum1 = n1 + n2;
int sum2 = n3 + n4;
if (sum1 == sum2)
System.out.println("lucky number ");
else
System.out.println("is not a lucky number ");
Or you and do this way :
public static void main(String[] args)
{
Scanner obj = new Scanner(System.in);
int num;
System.out.println("Enter a 4-digit no : ");
num = obj.nextInt();
String s = String.valueOf(num);
if(num>999&&num<10000)
{
if((s.charAt(0)+ s.charAt(1)) == (s.charAt(2)+ s.charAt(3)))
System.out.println(num+" is a lucky no");
else
System.out.println(num+" is not a lucky no");
}
else
System.out.println(num+" is not a valid 4-digit no");
}
Can we read the number as a string type and then add and check using charAt () method?
ОтветитьSo I have a question please, Why you choose the % 10 for example instead why you didn’t choose % 8 or any other number ?
Ответитьyou are trying to improve your accent right?
ОтветитьBro simple calculator do
ОтветитьVoice is sharp bro not good for that much
ОтветитьAgain the same video ! And made the replica of this video posted earlier private ! Seriously ?!?
ОтветитьNice bro
Ответить