write a program to input 10 different numbers . Display the sum of only odd numbers by using for loop .
write program in java ✔
no spam ✔
Answers
Answered by
81
Required program:
import java.util.*;
public class SeriesSum
{
public static void main ( Sting [] args)
{
Scanner sc = new Scanner (System.in);
int num , sum;
for(int i= 1; i<=10;i++)
{
System. out.print("Input the number one by one:" );
n= sc.nextInt();
if(n%2!=0)
sum=sum+n;
}
System. out.println ("Sum of 10 odd numbers =" +sum);
}
}
Input :
n=1
Output:
Sum of 10 odd numbers =100
Must know:
Syntax of for loop:-
for(initialization/declaration; test condition; increment/decrement)
{
//Statements
//note: if you have only one Statement then you can omit currely brackes ({})
}
Know more about loop:-
Loop is a special statement which is used to repeat a statement or block of statements unless the condition comes false.
There are three types of loops:-
- for loop
- while loop
- do while loop
Similar questions