Loops in Java (Exercise 3)

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

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

#JavaByNeso #JavaProgramming #Loops
Комментарии
Автор

I like your videos neso academy, keep going as am learning Java from your tutorials. Thanks a lot 😍

abdishakur
Автор

Hi Neso ! I have a suggestion for this part of exercise when finding the strict divisor and the sum of divisors. First of all when you used n/2 on the for loop you made it too complicated I think, here my suggestion:
1: we prompt the user to enter the number....[code]= (System.out.println("Enter your number")); ---- 2: We get the number from the user ....[ code]= ( int number= input.nextInt();):---- 3: we declare the variable both sum and the divisor . [code ]= ( int sum=0; and int divisor =1);---- 4: we write our for loop. ( ; divisor<=number; divisor++); ---- 5: inside the loop. we write our if condition [code ]= if(number % divisor==0){ System.out.print(divisor + " "); and sum+= divisor; ---6 : Outside of our for loop we can print the sum of the stric divisor [code ]= System.out.println(sum). Notice that if we want to display all the divisors number we have to change divisor< number instead of divisor<= number in our for loop. Thanks I think it can help someone here to understand more better.

NZIT
Автор

Thanks for your best educate you are awesome Ali💥

chiyasarbarz
Автор

for(int i=1;i<num;i++)
{
if(num%i==0)
sum+=i;
}

HOPE_-lomz
Автор

import java.util.Scanner;

class Divisor
{
public static void main(String args[])
{
Scanner s = new Scanner(System.in);
int x =s.nextInt(), sum = 0;
//To continue the loop
for(int i=1;i<x;i++){
//To calculate the divisor
if((x%i== 0)){
System.out.print(i);
sum +=i;
//To avoid the + sign after last digit
if(i<x/2){
System.out.print(" + ");
}
}
}
System.out.print(" = "+ sum);
}
}

IMRANsLU
Автор

Scanner in = new Scanner(System.in);
System.out.println("type a number :");
int num = in.nextInt();

int i;
int sum = 0;
for(i = 1; i<=num/2 ; i++){
if(num % i == 0){
System.out.print(i + " ");
sum = sum + i;
}
}
System.out.println();
System.out.println("sum = " + sum);

rayaraya
Автор

Great content mate. Just to put it out there, 1 is not a strict divisor since it divides every number :)

Just read this on Wikipedia :P

advitiayanand
Автор

u could just change the ending condition inside for loop with n instead of n/2 to get all divisors instead of strict

tabassumpriasha
Автор

public static void main(String args[]) {

Scanner num=new Scanner (System.in);
int sum=0;
int n=num.nextInt();
for (int i=1;i<=n/2;i++)
{
if (n%i==0)
{
sum+=i;

}

}
System.out.println(sum);

}
}

thivagarmurugan
Автор

import java.util.Scanner;

public class Main {
public static void main(String[] args) {




Scanner input = new Scanner(System.in);
: ");
int i = input.nextInt();
int n = i / 2;
int sum = 0;

while (n >= 1) {

if (i % n == 0){
System.out.print(n + ", ");
sum += n;
}
n--;

}
System.out.println("sum is equals to " + (sum + i));

}

}

tarunwadhwani
Автор

How does the last case work for n = 2.

suryamj
Автор

Please try uploading 2-3 videos per day .
As everyone are free at home

n.hemanthkumar
Автор

what if we take a number which is a square of some number like 4= 1+2+2, to include this condition in this program?

vishalchoubey
Автор

I'm using VScode, I'm getting the error "i cannot be resolved", and if I declare it outside the loop, it's not showing any error but I'm getting the result wrong.
Any idea, how to fix it?

sairamnomula
Автор

How to make Intellij print in the same row like your 1 2 4 5 10 in 4:20??

yevgeni
Автор

System.out.print("Enter a number: ");
int n = s.nextInt();

int sum = 0;
for(int i = 1; i <= n; i++)
if(n % i == 0)
//sum += i;
System.out.print(i + " ");
//System.out.println(sum);

}
} // I tried this method before you tell and also got the result😅

aaageneral
Автор

bro a strict divisor cant be equal to 1 so u should start the iteration from 2 not 1

bhaigaming-gm
Автор

Your solution doesnt work for negative integers. Shouldve mentioned positive integers at the first place, or add Math.abs()

jejewite
Автор

Scanner scanner5 = new Scanner(System.in);
System.out.print("Divisor Calculator, Pick an integer to determin it's strict divisors: ");
int num = scanner5.nextInt();
int numbers = 0;
for(int i = 1; i <= num; i++){
if(num % i == 0 && i <= num/2){
numbers += i;
if(i>1){
System.out.print(" + " + i);
}
else{
System.out.print(i);
}
}

}
System.out.println(" = " + numbers);

la
Автор

import java.util.Scanner;

class Check {
public static void main(String[] args) {

System.out.print("enter an integer: ");
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();

// For Loop
int sum1 = 0;
for(int i = 1; i <= n/2; i++) {
if(n % i == 0) {
sum1 += i;
System.out.print(i + " + ");
}
}

System.out.println("0 = " + sum1);

// While Loop
int sum2 = 0;
int i = 1;
while(i <= n/2) {
if(n % i == 0) {
sum2 += i;
System.out.print(i + " + ");
}

i++;
}

System.out.println("0 = " + sum2);

// Do-While Loop
int sum3 = 0;
int j = 1;
do {
if(n % j == 0) {
sum3 += j;
System.out.print(j + " + ");
}

j++;
} while(j <= n/2);

System.out.println("0 = " + sum3);
}
}

suswithcherry