- Sum Of Even Or Odd Numbers Based On The N value DoWhile Loop
import java.util.Scanner;
public class Demo {
public static void main(String[] args) {
int x=1,n,sum=0;
Scanner s=new Scanner(System.in);
System.out.println("Enter n value");
n=s.nextInt();
do
{
if(x%2==0 && n%2==0)
{
sum=sum+x;
}
else if(x%2!=0 && n%2!=0)
{
sum=sum+x;
}
x++;
}while(x<=n);
System.out.println("sum is " +sum);
}
}
output:
Enter n value 5
sum is 9
Enter n value 6
sum is 12
import java.util.Scanner;
public class Demo {
public static void main(String[] args) {
int x=1,n,sum=0;
Scanner s=new Scanner(System.in);
System.out.println("Enter n value");
n=s.nextInt();
do
{
if(x%2==0 && n%2==0)
{
sum=sum+x;
}
else if(x%2!=0 && n%2!=0)
{
sum=sum+x;
}
x++;
}while(x<=n);
System.out.println("sum is " +sum);
}
}
output:
Enter n value 5
sum is 9
Enter n value 6
sum is 12
No comments:
Post a Comment