给定一个偶数n,求从1到n的偶数的平均值。 例如:
null
Input : 10Output : 6Explanation:(2 + 4 + 6 + 8 + 10 )/5= 30/5= 6 Input : 100Output : 51
方法1 我们可以通过将每个偶数相加到n,然后用计数除以和来计算平均数。
C++
// Program to find average of even numbers // till a given even number. #include <stdio.h> // Function to calculate the average // of even numbers int averageEven( int n) { if (n % 2 != 0) { printf ( "Invalid Input" ); return -1; } int sum = 0, count = 0; while (n >= 2) { // count even numbers count++; // store the sum of even numbers sum += n; n = n - 2; } return sum / count; } // driver function int main() { int n = 16; printf ( "%d" , averageEven(n)); return 0; } |
JAVA
// Program to find average of even numbers // till a given even number. import java.io.*; class GFG { // Function to calculate the average // of even numbers static int averageEven( int n) { if (n % 2 != 0 ) { System.out.println( "Invalid Input" ); return - 1 ; } int sum = 0 , count = 0 ; while (n >= 2 ) { // count even numbers count++; // store the sum of even numbers sum += n; n = n - 2 ; } return sum / count; } // driver function public static void main(String args[]) { int n = 16 ; System.out.println(averageEven(n)); } } /*This code is contributed by Nikita Tiwari.*/ |
Python3
# Program to find average of # even numbers till a given # even number. # Function to calculate the # average of even numbers def averageEven(n) : if (n % 2 ! = 0 ) : print ( "Invalid Input" ) return - 1 sm = 0 count = 0 while (n > = 2 ) : # count even numbers count = count + 1 # store the sum of even # numbers sm = sm + n n = n - 2 return sm / / count # driver function n = 16 print (averageEven(n)) # This code is contributed by Nikita Tiwari. |
C#
// C# Program to find average // of even numbers till a // given even number. using System; class GFG { // Function to calculate the // average of even numbers static int averageEven( int n) { if (n % 2 != 0) { Console.Write( "Invalid Input" ); return -1; } int sum = 0, count = 0; while (n >= 2) { // count even numbers count++; // store the sum of even numbers sum += n; n = n - 2; } return sum / count; } // driver function public static void Main() { int n = 16; Console.Write(averageEven(n)); } } /*This code is contributed by vt_m.*/ |
PHP
<?php // PHP Program to find average of even // numbers till a given even number. // Function to calculate the // average of even numbers function averageEven( $n ) { if ( $n % 2 != 0) { echo ( "Invalid Input" ); return -1; } $sum = 0; $count = 0; while ( $n >= 2) { // count even numbers $count ++; // store the sum of // even numbers $sum += $n ; $n = $n - 2; } return $sum / $count ; } // Driver Code $n = 16; echo (averageEven( $n )); //This code is contributed by vt_m. ?> |
Javascript
<script> // javascript Program to find average of even numbers // till a given even number. // Function to calculate the average // of even numbers function averageEven( n) { if (n % 2 != 0) { document.write( "Invalid Input" ); return -1; } let sum = 0, count = 0; while (n >= 2) { // count even numbers count++; // store the sum of even numbers sum += n; n = n - 2; } return sum / count; } // driver function let n = 16; document.write( averageEven(n)); // This code is contributed by todaysgaurav </script> |
输出:
9
方法2 偶数的平均值只能通过一步计算出来 通过使用以下公式:- [n+2]/2其中n是最后一个偶数。 这个公式是如何工作的? 我们知道在n之前有(n)/2个偶数。例如,在4之前有两个偶数,在6之前有三个偶数。 前k个偶数之和为k*(k+1) 把k=n/2,我们得到 总和 前n/2个偶数中的n/2(n/2+1) 平均的 前n/2个偶数(或直到n的偶数)=(n+2)/2
C++
// Program to find average of even numbers // till a given even nend number. #include <bits/stdc++.h> using namespace std; // Function to calculate the average // of even numbers int averageEven( int n) { if (n % 2 != 0) { cout<< "Invalid Input" ; return -1; } return (n + 2) / 2; } // driver function int main() { int n = 16; cout<<averageEven(n)<<endl; return 0; } // This code is contributed by noob2000. |
C
// Program to find average of even numbers // till a given even number. #include <stdio.h> // Function to calculate the average // of even numbers int averageEven( int n) { if (n % 2 != 0) { printf ( "Invalid Input" ); return -1; } return (n + 2) / 2; } // driver function int main() { int n = 16; printf ( "%d" , averageEven(n)); return 0; } |
JAVA
// Program to find average of even numbers // till a given even number. import java.io.*; class GFG { // Function to calculate the average // of even numbers static int averageEven( int n) { if (n % 2 != 0 ) { System.out.println( "Invalid Input" ); return - 1 ; } return (n + 2 ) / 2 ; } // driver function public static void main(String args[]) { int n = 16 ; System.out.println(averageEven(n)); } } /*This code is contributed by Nikita Tiwari.*/ |
Python3
# Python3 program to find average of even # numbers till a given even number. # Function to calculate the # average of even numbers def averageEven(n) : if (n % 2 ! = 0 ) : print ( "Invalid Input" ) return - 1 return (n + 2 ) / / 2 # Driver function n = 16 print (averageEven(n)) # This code is contributed by Nikita Tiwari. |
C#
// C# Program to find average // of even numbers till a // given even number. using System; class GFG { // Function to calculate the // average of even numbers static int averageEven( int n) { if (n % 2 != 0) { Console.Write( "Invalid Input" ); return -1; } return (n + 2) / 2; } // driver function public static void Main() { int n = 16; Console.Write(averageEven(n)); } } /*This code is contributed by vt_m.*/ |
PHP
<?php // PHP Program to find average of even // numbers till a given even number. // Function to calculate the average // of even numbers function averageEven( $n ) { if ( $n % 2 != 0) { echo ( "Invalid Input" ); return -1; } return ( $n + 2) / 2; } // Driver Code $n = 16; echo (averageEven( $n )); return 0; // This code is contributed by vt_m. ?> |
Javascript
<script> // javascript Program to find average of even numbers // till a given even number. // Function to calculate the average // of even numbers function averageEven( n) { if (n % 2 != 0) { document.write( "Invalid Input" ); return -1; } return (n + 2) / 2; } // driver function let n = 16; document.write(averageEven(n)); // This code is contributed by Rajput-Ji </script> |
输出:
9
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END