Write a program to calculate & print the sum of even & odd integers of the first n natural
numbers.
Answers
Answered by
4
def sum(n):
n = int(input("Enter number count:")
k = n + (n-1)
while n>1:
print(Sum(n-1))
Change indentations accordingly.
n = int(input("Enter number count:")
k = n + (n-1)
while n>1:
print(Sum(n-1))
Change indentations accordingly.
Answered by
5
Answer:
public class Numbers
{
public void sumOfNaturalNos(int n)
{
int sum_even = 0, sum_odd = 0, ctr = 1 ;
while (ctr <= n)
{
if ( ctr % 2 = = 0)
sum_even += ctr ;
else
sum_odd += ctr ;
ctr ++ ;
}
System.out.println("Sum of even integers" + "upto" + n + "is" + sum_even) ;
System.out.println("Sum of odd integers" + "upto" + n + "is" + sum_odd) ;
}
}
Output produced is:
Sum of even integers upto 25 is 156
Sum of odd integers upto 25 is 169
Similar questions