import java.util.*;
public class SumOfOdd Numbers {
public static void main(String[] args) {
Scanner s=new Scanner(System.in);
System.out.println("enter number");
int n=s.nextInt();
System.out.println("The Odd Numbers are:");
for (int i = 1; i <= n; i++) {
if (i % 2 != 0) {
System.out.println(i + " ");
}
}
int sum = 0;
for (int i = 1; i <= n; i++)
{
if (i % 2 != 0)
{
sum = sum + i;
}
}
System.out.println("sum of " + n +" odd numbers:" +sum);
}
}
OUTPUT:
enter number
10
The Odd Numbers are:
1
3
5
7
9
sum of 10 odd numbers:25
public class SumOfOdd Numbers {
public static void main(String[] args) {
Scanner s=new Scanner(System.in);
System.out.println("enter number");
int n=s.nextInt();
System.out.println("The Odd Numbers are:");
for (int i = 1; i <= n; i++) {
if (i % 2 != 0) {
System.out.println(i + " ");
}
}
int sum = 0;
for (int i = 1; i <= n; i++)
{
if (i % 2 != 0)
{
sum = sum + i;
}
}
System.out.println("sum of " + n +" odd numbers:" +sum);
}
}
OUTPUT:
enter number
10
The Odd Numbers are:
1
3
5
7
9
sum of 10 odd numbers:25
No comments:
Post a Comment