if-else Statement (Exercise 2)

if-else Statement (Exercise 2)

Neso Academy

4 года назад

41,901 Просмотров

Ссылки и html тэги не поддерживаются


Комментарии:

Grown Girl
Grown Girl - 04.11.2023 13:27

I saved the four digits as String in num then use the num.charAt() in the if statement

Ответить
Prajwal R V
Prajwal R V - 30.10.2023 08:16

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?

Ответить
Eshha :]
Eshha :] - 26.10.2023 17:06

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

Ответить
عييش البودكاست
عييش البودكاست - 05.09.2023 15:14

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)

Ответить
Avinash
Avinash - 14.07.2023 13:32

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");
}
}

Ответить
Daily.Motivation
Daily.Motivation - 25.06.2023 11:43

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");
}
}

Ответить
Nchaukeni Zulu
Nchaukeni Zulu - 17.04.2023 22:06

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

Ответить
Keval Shah
Keval Shah - 04.04.2023 14:30

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 !"); }
}}

Ответить
Fatma Adatepe
Fatma Adatepe - 16.01.2023 22:10

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.");

Ответить
Sravan Sunkara
Sravan Sunkara - 12.01.2023 16:00

My mind was blown away🤯

Ответить
shivam
shivam - 03.08.2022 22:26

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");
}
}

Ответить
Sk Shaid Hossain
Sk Shaid Hossain - 29.06.2022 18:13

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

Ответить
ThatsDeep
ThatsDeep - 15.06.2022 22:06

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");
}

Ответить
ayushi mukherjee
ayushi mukherjee - 16.03.2022 13:56

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");
   

}
}

Ответить
Dipesh Samrawat
Dipesh Samrawat - 03.03.2022 17:45

I love you Neso Academy. ❤️

Ответить
Toan Nguyen
Toan Nguyen - 07.02.2022 17:21

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!");
}
}

Ответить
Lasa MD
Lasa MD - 03.02.2022 09:03

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");
}
}

Ответить
Khanh Nguyen
Khanh Nguyen - 25.12.2021 03:07

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.");
}
}

Ответить
shahida ibrahim
shahida ibrahim - 27.09.2021 18:36

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");
}

}
}

Ответить
Hikmət Həsənov
Hikmət Həsənov - 31.08.2021 02:39

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");
}
}

Ответить
Rohan Deb
Rohan Deb - 29.08.2021 04:28

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");

}}

Ответить
Brian Maina
Brian Maina - 25.05.2021 00:53

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.

Ответить
Monhaa Jaywant
Monhaa Jaywant - 24.04.2021 19:08

Can you put some simple examples instead of modulo

Ответить
Sunny Raj
Sunny Raj - 18.03.2021 22:43

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

Ответить
DNLK
DNLK - 21.02.2021 17:50

great stuff

Ответить
Ahmed Nabil
Ahmed Nabil - 13.01.2021 12:51

You have made java easy language to learn, thanks alot.

Ответить
Mohamed Khamis
Mohamed Khamis - 04.11.2020 14:23

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 ");

Ответить
SAURAV KUMAR
SAURAV KUMAR - 08.10.2020 21:30

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");

}

Ответить
sandeep singh
sandeep singh - 06.05.2020 07:18

Can we read the number as a string type and then add and check using charAt () method?

Ответить
Mr. Nobody
Mr. Nobody - 04.05.2020 23:43

So I have a question please, Why you choose the % 10 for example instead why you didn’t choose % 8 or any other number ?

Ответить
dio joestar
dio joestar - 15.03.2020 21:33

you are trying to improve your accent right?

Ответить
all-rounder India
all-rounder India - 15.03.2020 21:09

Bro simple calculator do

Ответить
Ravikumar
Ravikumar - 15.03.2020 20:56

Voice is sharp bro not good for that much

Ответить
Bot
Bot - 15.03.2020 20:52

Again the same video ! And made the replica of this video posted earlier private ! Seriously ?!?

Ответить
MURALI BALA
MURALI BALA - 15.03.2020 20:49

Nice bro

Ответить