Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
C Program for n-th even number
Given a number N we have to find the N-th even number.
Even numbers are the numbers which are completely divided by 2 and their remainder is zero. Like 2, 4, 6, 8, 10, and so on.
If we closely observe the list of even numbers we can also represent them as −
2*1=2, 2*2=4, 2*3=6, 2*4=8, ?.2*N.
So, to solve the problem we can simply multiply the number N with 2, so the result will be the number which is divisible by 2, i.e. the even number.
Syntax
nth_even_number = n * 2;
Example
Input: n = 4 Output: 8 The first 4 even numbers will be 2, 4, 6, 8, .. Input: n = 10 Output: 20
Algorithm
START STEP 1-> DECLARE AND SET n AS 10 STEP 2-> PRINT n*2 NUMBER STOP
Example: Finding nth Even Number
#include <stdio.h>
int main() {
int n = 10;
int nthEven = n * 2;
printf("The %d-th even number is: %d<br>", n, nthEven);
return 0;
}
The 10-th even number is: 20
Example: Interactive Program
Here's a program that calculates the nth even number for different values −
#include <stdio.h>
int main() {
int numbers[] = {1, 4, 7, 15, 25};
int size = 5;
printf("n\tn-th Even Number<br>");
printf("--\t----------------<br>");
for (int i = 0; i < size; i++) {
int n = numbers[i];
int nthEven = n * 2;
printf("%d\t%d<br>", n, nthEven);
}
return 0;
}
n n-th Even Number -- ---------------- 1 2 4 8 7 14 15 30 25 50
Key Points
- The nth even number is always 2 * n.
- Time complexity is O(1) as we only perform one multiplication.
- Space complexity is O(1) as we use constant extra space.
Conclusion
Finding the nth even number in C is straightforward using the formula 2*n. This mathematical approach provides an efficient solution with constant time complexity.
Advertisements
