0 points

(Average of even places)Write a function that returns the average of the digits in the even places in an integer using the following header: double avgOfEvenPlaces(int n)


dashboard

Wed Nov 11 2020 05:55:19 GMT+0000 (UTC)

Posted by @mahmoud hussein #c++

#include <iostream>
#include<cmath>
#include<ctime>
#include<string>
#include <iomanip>
#include <fstream>

using namespace std;



double avgOfEvenPlaces(int number);
int reversal(int n);


int main()
{
	
	int number;
	cin >> number;
	cout << fixed<<setprecision(3)<<avgOfEvenPlaces(reversal(number));
}
double  avgOfEvenPlaces(int n)
{
	
	int digit, count = 1.0, countOfEven = 0.0;
	double sumOfEven = 0.0;
	while (n != 0)
	{
		digit = n % 10;
		n /= 10;
		
		if (count % 2 == 0)
		{
		  
			sumOfEven += digit; 
			countOfEven++;
		}
			count++;
	}
	
		return  sumOfEven / countOfEven;
	
}
int reversal(int n)
{
	int digits, revers = 0.0;
	while (n != 0)
	{
		digits = n % 10;
		revers = revers * 10 + digits;
		n /= 10;
	}
	return revers;
 }


content_copy Copy

http://cpp.sh/