Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sum of ap #3125

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions Arithmetic_Progression/Arithmetic_Progression.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,14 @@
// To find sum of the arithmetic progression
int SumOfAP(int a, int d, int n)
{
int sum = 0;
for (int i = 0; i < n; i++)
{
sum = sum + a;
a = a + d;
}
return sum;
// int sum = 0;
// for (int i = 0; i < n; i++)
// {
// sum = sum + a;
// a = a + d;
// }
// return sum;
return (n/2)*(2*a+((n-1)*d));
}

int main() {
Expand Down
15 changes: 8 additions & 7 deletions Arithmetic_Progression/Arithmetic_Progression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,14 @@ using namespace std;
// To find sum of the arithmetic progression
int SumOfAP (int a, int d, int n)
{
int sum = 0;
for (int i = 0; i < n; i++)
{
sum = sum + a;
a = a + d;
}
return sum;
// int sum = 0;
// for (int i = 0; i < n; i++)
// {
// sum = sum + a;
// a = a + d;
// }
// return sum;
return (n/2)*(2*a+((n-1)*d));
}

int main() {
Expand Down
13 changes: 7 additions & 6 deletions Arithmetic_Progression/Arithmetic_Progression.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@ import (
)

func SumOfAP(a int, d int, n int) int {
var sum = 0
for i := 0; i < n; i++ {
sum = sum + a
a = a + d
}
return sum
// var sum = 0
// for i := 0; i < n; i++ {
// sum = sum + a
// a = a + d
// }
// return sum
return (n/2)*(2*a+((n-1)*d));
}

func main() {
Expand Down
15 changes: 8 additions & 7 deletions Arithmetic_Progression/Arithmetic_Progression.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,14 @@ The nth term of an A.P. is defined as (a + (n - 1)*d)

class Arithmetic_Progression {
static int SumOfAP(int a, int d, int n) {
int sum = 0;
for (int i = 0; i < n; i++)
{
sum = sum + a;
a = a + d;
}
return sum;
// int sum = 0;
// for (int i = 0; i < n; i++)
// {
// sum = sum + a;
// a = a + d;
// }
// return sum;
return (n/2)*(2*a+((n-1)*d));
}

public static void main(String args[]) {
Expand Down