Loops in Java (Exercise 6)

preview_player
Показать описание
Java Programming: Programming Question on Loops in Java Programming
Topics Discussed:
1. Writing a program that displays the sum of digits of an integer read from the user.

Music:
Axol x Alex Skrindo - You [NCS Release]

#JavaByNeso #JavaProgramming #Loops
Рекомендации по теме
Комментарии
Автор

I'm glad I was able to do the exercise myself before watching. I've been going through a lot of problems lately and I'm not 100% focused, so that was something

randompotato
Автор

Scanner sc = new Scanner(System.in);

System.out.print("Enter a number: ");
Integer n = sc.nextInt();
int sum = 0;

for (int i = 0; i < n.toString().length(); i++) {
String ns = n.toString();
sum +=
}

System.out.println(sum);

ThePlokonChannel
Автор

thank you so much. you teach me very needful things ❤❤

bein
Автор

using for loop;

import java.util.Scanner;

public class ner {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
int sum = 0;

for (int i =1 ; (n/i)>=1; i = i*10){
sum = sum + ((n/i)%10);
}

System.out.println("sum : "+sum);


}
}

kartikrastogi
Автор

public static void main(String args[]) {

Scanner num = new Scanner(System.in);
int n = num.nextInt();
int sum = 0;
while (n != 0) {
int rem = n % 10;
sum += rem;
n = n / 10;

}
System.out.println(sum);

}
}

thivagarmurugan
Автор

public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter an integer: ");
int n = input.nextInt();
int sum = 0;
while(n > 0){
sum += n % 10;
n = n / 10;
}
System.out.println("Sum: " + sum);
}
}

caesaaar
Автор

//using while loop that is much easier to understand + choices

import java.util.Scanner;

public class Main {

public static void main(String[] args) {
Scanner input = new Scanner(System.in);

int n;
int sum;
int answer;

while (true) {
System.out.println("enter a number ");
n = input.nextInt();
int x = n % 10;
int y = n / 10 % 10;
int z = n / 100;
sum = x + y + z;

System.out.println("SUM: " + sum);
System.out.println("do you want to enter another number? 1 if yes and 2 if no");
answer = input.nextInt();
if (answer == 2)
break;
}

System.out.println("done");
}
}

lamao
Автор

import java.util.*;
public class AddDigits{
static int sum=0;
public static void main(String [] args){
Scanner sc=new Scanner(System.in);
System.out.println("enter any number");
int n=sc.nextInt();
for(int i=1;i<=n;i*=10){
sum+=(n/i)%10;
}
System.out.println("sum of digits are :"+sum);
}
}

wasimpatel
Автор

Where is DATA STRUCTURE using C language?
I am waiting from a long time sir.

ShahNawaz-cxpi
Автор

i did it by
// TODO code application logic here

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

Scanner sc=new Scanner(System.in);

int a=sc.nextInt();
int add=0;

for(int
{
char
String cts=String.valueOf(index);

int index1=Integer.valueOf(cts);
add+=index1;

}


System.out.println(add);


}

DilpreetSingh-pdco
Автор

import java.util.Scanner;

public class sixth {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int sum = 0;
int divisor = 1;
System.out.println("enter the digitS to find the sum of");
int numb = input.nextInt();
String numberString = Integer.toString(numb);
int length = numberString.length();
for(int i = 1; i <= length; i++){
sum += ((numb/divisor)%10);
divisor *= 10;


}
System.out.println("sum of the numbers are: " + sum);
}
}

random-tvsd
Автор

import java.util.Scanner;

public class Main {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);

System.out.print("enter a message: ");

int input = s.nextInt();
int result = 0;

for (int i = 0 ; i <= input ; i++) {
result += input % 10;
input = input / 10;
}
System.out.print(result);

/*while (input > 0) {
result += input % 10;
input = input / 10;
}
System.out.print(result);*/


}
}

Bibingka
Автор

Please clarify why we divide by 10 and as well use 10 for the modulo operation? What if u choose a different number ? Thank u 🙏🏾

mohamedkanu
Автор

What happens if user writes 000? and can you explain you n>0

navedrahman