WAP to display the sum of factors of a number...
Answers
/*
* Author: TheSchoolProgrammer
* www.theschoolprogrammer.com
* Question:Write a program to accept a number and print sum of factors of that number.
*/
import java.util.*;
public class sumFact
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("ENTER A NUMBER");
int n=sc.nextInt();
int i,sum=0;
for(i=1;i<=n;i++)
{
if(n%i==0)
sum=sum+i;
}
System.out.print("SUM = "+sum);
}
}
Answer:
import java.util.*;
class factorSum
{
public static void main(String[] args)
{
int sum = 0;
System.out.print("Enter a number:");
Scanner scanner = new Scanner(System.in);
int inp = scanner.nextInt();
for(int i = 1; i < inp;i++)
{
if(inp%i == 0)
{
sum = sum + i;
}
}
System.out.println("The sum of the factors of " + inp + " is " + sum);
}
}
Explanation: