Write a program in java to read 10 numbers from keyboard and find their sum and average.

The positive numbers 1, 2, 3... are known as natural numbers and its sum is the result of all numbers starting from 1 to the given number.

For n, the sum of natural numbers is:

1 + 2 + 3 + ... + n

Example 1: Sum of Natural Numbers using for loop

public class SumNatural { public static void main(String[] args) { int num = 100, sum = 0; for(int i = 1; i <= num; ++i) { // sum = sum + i; sum += i; } System.out.println("Sum = " + sum); } }

Output

Sum = 5050

The above program loops from 1 to the given num(100) and adds all numbers to the variable sum.

You can solve this problem using a while loop as follows:

Example 2: Sum of Natural Numbers using while loop

public class SumNatural { public static void main(String[] args) { int num = 50, i = 1, sum = 0; while(i <= num) { sum += i; i++; } System.out.println("Sum = " + sum); } }

Output

Sum = 1275

In the above program, unlike a for loop, we have to increment the value of i inside the body of the loop.

Though both programs are technically correct, it is better to use for loop in this case. It's because the number of iteration (up to num) is known.

Write a C program to read 10 numbers and find their sum and average using for loop. In this example, for loop iterate from 1 to 10, add each number to the sum to find the sum of 10 numbers. Next, we calculate the average.

#include <stdio.h> int main() { int num, sum = 0; float avg; printf("Please Enter the 10 Numbers\n"); for(int i = 1; i <= 10; i++) { printf("Number %d = ", i); scanf("%d", &num); sum = sum + num; } avg = sum / 10; printf("\nThe Sum of 10 Numbers = %d", sum); printf("\nThe Average of 10 Numbers = %.2f\n", avg); }

This C program reads 10 numbers from the user input and calculates their sum and average using a while loop.

#include <stdio.h> int main() { int num, i, sum = 0; float avg; printf("Please Enter the 10 Numbers\n"); i = 1; while(i <= 10) { printf("Number %d = ", i); scanf("%d", &num); sum = sum + num; i++; } avg = (float)sum / 10.0; printf("\nThe Sum of 10 Numbers = %d", sum); printf("\nThe Average of 10 Numbers = %.2f\n", avg); }Please Enter the 10 Numbers Number 1 = 22 Number 2 = 33 Number 3 = 44 Number 4 = 55 Number 5 = 67 Number 6 = 89 Number 7 = 123 Number 8 = 234 Number 9 = 543 Number 10 = 199 The Sum of 10 Numbers = 1409 The Average of 10 Numbers = 140.90

The average is the outcome from the sum of the numbers divided by the count of the numbers being averaged.

For example: 1,2,3,4,5

Number of all elements = 5

Sum of all elements = 1+2+3+4+5 =15

Average = Sum of all elements / number of all elements = 15/5 =3

Average =3

Algorithm

  • STEP 1: START
  • STEP 2: DEFINE n.
  • STEP 3: SET count = 1
  • STEP 4: DEFINE xF, averageF
  • STEP 5: SET sumF = 0
  • STEP 6: ENTER n
  • STEP 7: REPEAT STEP 8 to 10 UNTIL count<=n
  • STEP 8: Enter xF
  • STEP 9: sumF += xF
  • SETP 10: count = count + 1
  • SETP 11: SET averageF = sumF/(n-1)
  • STEP 12: PRINT averageF
  • STEP 13: END

Java Program

Output:

Enter the value of n 5 Enter the 1 number? 1 Enter the 2 number? 2 Enter the 3 number? 3 Enter the 4 number? 4 Enter the 5 number? 5 The Average is 3.0

C program

Output:

Enter the value of n? 5 Enter the 1 number? 1 Enter the 2 number? 2 Enter the 3 number? 3 Enter the 4 number? 4 Enter the 5 number? 5 The Average is 3.0000

Python program

Output:

Enter the value of n? 5 Enter the 1 number? 1 Enter the 2 number? 2 Enter the 3 number? 3 Enter the 4 number? 4 Enter the 5 number? 5 Average using a while loop 3.0000

Php program

Output:

Enter the value of n? 5 Enter the number 1 Enter the number 2 Enter the number 3 Enter the number 4 Enter the number 5 3.0000

C# program

Output:

Enter the value of n? 5 Enter the 1 number? 1 Enter the 2 number? 2 Enter the 3 number? 3 Enter the 4 number? 4 Enter the 5 number? 5 The Average is 3.0

C program to read 10 numbers and find sum and average; Through this tutorial, we will learn how to find sum and average of 10 numbers in the c program with the help for loop and while loop.

Algorithm and Program to Read 10 Numbers and Find their Sum and Average in C

Use the following algorithm and program to read 10 numbers from keyboard and find their sum and average using for loop and while loop in c:

  • Algorithm to Read 10 Numbers and Find their Sum and Average
  • C Program to Read 10 Numbers and Find their Sum and Average using For Loop
  • C Program to Read 10 Numbers and Find their Sum and Average using While Loop

Algorithm to Read 10 Numbers and Find their Sum and Average

Use the following algorithm to write a program to read 10 numbers from keyboard and find their sum and average; as follows:

How to read 10 numbers in Java?

Answer and Explanation:.
import java. util. Scanner;.
public class Main..
public static void main(String [] args).
Scanner scn = new Scanner(System.in);.
int [] source = new int[10];.
for(int i=0; i<10; i++).

How to print 10 numbers in Java?

Java Program to Print 1 to 10 Numbers using For Loop.
public class Use_For_Loop..
public static void main(String[] args).
for(int i = 1; i <= 10; i++).
System. out. println(i);.

How to input 10 numbers in an array in Java?

For example, // declare an array double[] data; // allocate memory data = new double[10]; Here, the array can store 10 elements. We can also say that the size or length of the array is 10.

Which are the ways to read data from keyboard in Java?

You can read data from user (keyboard) using various classes such as, Scanner, BufferedReader, InputStreamReader, Console etc.

Toplist

Neuester Beitrag

Stichworte