Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.
Login
Login
OnlineGDB Q&A
Questions
Unanswered
Tags
Ask a Question
Ask a Question
find average of numbers between 0-50.(enter by user). logical error.anyone help me to find it out.
–2
votes
asked
Aug 11, 2019
by
Deepti Mehra
(
100
points)
// File name: Main.java ;
import java.util.Scanner;
public class Main
{
public static void main (String[]args)
{
Scanner d = new Scanner (System.in);
int n;
int sum =0;
double avg = 1.0;
int i;
int max;
int min;
max=Integer.MIN_VALUE;
min=Integer.MAX_VALUE;
while(true)
{
//an infinite loop, use Ctrl-C to quit
System.out.println ("Enter an integer:");
n = d.nextInt ();
if(n < -0 || n>50)break;
sum = sum+n;
for(i=2;i<n;i++)
avg = sum/i;
if(n<min)
{
min=n;
}
if(n>max)
{
max=n;
}
} //end of while
System.out.println("the avg is" +avg );
System.out.println("maximum num is :" +max);
System.out.println("minimum num is:" +min);
}
} //end of main
-help-plz
Your answer
Your name to display (optional):
Email me at this address if my answer is selected or commented on:
Email me if my answer is selected or commented on
Privacy: Your email address will only be used for sending these notifications.
Anti-spam verification:
To avoid this verification in future, please
log in
or register.
4 Answers
0
votes
answered
Aug 11, 2019
by
keerthana
(
180
points)
import java.util.Scanner;
public class Main
{
void average(int n)
{
int i,a=0,b;
for(i=1;i<=n;i++)
{
a+=i;
}
b=a/n;
System.out.print(b);
}
public static void main(String[] args) {
int n;
Scanner sc=new Scanner(System.in);
n=sc.nextInt();
Main k=new Main();
k.average(n);
}
}
Your comment on this answer:
Your name to display (optional):
Email me at this address if a comment is added after mine:
Email me if a comment is added after mine
Privacy: Your email address will only be used for sending these notifications.
Anti-spam verification:
To avoid this verification in future, please
log in
or register.
0
votes
answered
Aug 11, 2019
by
anonymous
In if condintion remove the minus sign in zero
Your comment on this answer:
Your name to display (optional):
Email me at this address if a comment is added after mine:
Email me if a comment is added after mine
Privacy: Your email address will only be used for sending these notifications.
Anti-spam verification:
To avoid this verification in future, please
log in
or register.
0
votes
answered
Aug 12, 2019
by
anonymous
#include<stdio.h>
main()
{
float n, I, avg, sum=0;
printf("enter n value:");
Scanf("%f",&n);
For(i=0;i<=n; i++);
Sum=sum+i;
Avg=sum/n;
Printf("avg is %f",avg);
}
Your comment on this answer:
Your name to display (optional):
Email me at this address if a comment is added after mine:
Email me if a comment is added after mine
Privacy: Your email address will only be used for sending these notifications.
Anti-spam verification:
To avoid this verification in future, please
log in
or register.
0
votes
answered
Aug 12, 2019
by
MURALIIDHARNAIDU
(
140
points)
#include <iostream>
using namespace std()
int main()
{ int n,i,x,avg;
cin>>n;
for(i=1;i<=n;i++)
{ x=x+i;
}
avg=x/n;
cout<<avg;
}
Your comment on this answer:
Your name to display (optional):
Email me at this address if a comment is added after mine:
Email me if a comment is added after mine
Privacy: Your email address will only be used for sending these notifications.
Anti-spam verification:
To avoid this verification in future, please
log in
or register.
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...