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.

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

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);
    }
}
0 votes
answered Aug 11, 2019 by anonymous
In if condintion remove the minus sign in zero
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);
}
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;

}
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.
...