WAPP to print the sum of first n natural numbers, each of which is neither multiples of 7, nor of 6. using for loop
Answers
Answered by
2
Code:
import java.util.Scanner;
public class NaturalNumbers {
public static void main(String[ ] args) {
System.out.print("Enter N - ");
int N = new Scanner(System.in).nextInt( ), sum = 0;
for (int i = 1; i <= N; i++)
if (!(i % 7 == 0 || i % 6 == 0))
sum += i;
System.out.println("Sum - " + sum);
}
}
Sample I/O:
Enter N - 30
Sum - 305
Answered by
2
def _sum(n):
_sum = 0
for i in range(1, n + 1):
if i % 6 != 0 and i % 7 != 0:
print(i)
_sum += i
return _sum
print(_sum(int(input("n: "))))
Similar questions