
- C Programming Tutorial
- C - Home
- C - Overview
- C - Environment Setup
- C - Program Structure
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Constants
- C - Storage Classes
- C - Operators
- C - Decision Making
- C - Loops
- C - Functions
- C - Scope Rules
- C - Arrays
- C - Pointers
- C - Strings
- C - Structures
- C - Unions
- C - Bit Fields
- C - Typedef
- C - Input & Output
- C - File I/O
- C - Preprocessors
- C - Header Files
- C - Type Casting
- C - Error Handling
- C - Recursion
- C - Variable Arguments
- C - Memory Management
- C - Command Line Arguments
- C Programming useful Resources
- C - Questions & Answers
- C - Quick Guide
- C - Useful Resources
- C - Discussion
Average of first n odd naturals numbers?
Here we will see how to get the average of n odd natural numbers. The n is given by the user. To get the ith odd number we can use this formula 2*i+1. Here also we are using this formula. Let us see the algorithm to get the clear idea.
Algorithm
avgOddNaturalNumber(n)
Begin sum := 0 for i in range 0 to n-1, do sum := sum + (2i + 1) done return sum/n End
Example
#include<iostream> using namespace std; float asciiAverage(string str){ int sum = 0; for(int i = 0; i<str.size(); i++){ sum += int(str[i]); } return sum/str.size(); } main() { string str; cout << "Enter a string: "; cin >> str; cout << "ASCII average is: " << asciiAverage(str); }
Output
Enter a string: Hello ASCII average is: 100
- Related Articles
- Average of first n even natural numbers?
- Sum of square of first n odd numbers
- Find the sum of first $n$ odd natural numbers.
- Average of odd numbers till a given odd number?
- Find the average of first N natural numbers in C++
- Java Program to Find Sum of First N Odd numbers and Even numbers
- Program to find sum of first N odd numbers in Python
- Swift Program to calculate the sum of first N odd numbers
- Program to find the sum of first n odd numbers in Python
- Average of Squares of n Natural Numbers?
- N consecutive odd numbers JavaScript
- Squared sum of n odd numbers - JavaScript
- PHP program to find the average of the first n natural numbers that are even
- Find the mean of first 7 odd natural numbers.
- 8086 program to find average of n numbers

Advertisements