Accenture Coding Question | Adam’s Charity | CodeWindow

Adam’s Charity

Adam decides to be generous and do some charity. starting today, from day 1 until day n, he gives i^2 coins to charity on day ‘i’ (1<=i<=n).
return the total coins he would give to charity.

Input specification:
Input 1: number of days of Charity.

Output Specification:
Return the total number of coins till charity days.

Example 1:
Input 1: 2

Output: 5
Explanation:
There are 2 days.

Example 2:
Input 1: 3

Output: 14
Explanation:
There are 3 days.

Solution in Python 3:

# https://codewindow.in

n=int(input())
sum=0
for i in range(1,n+1):
    sum=sum+(i**2)
print(sum)

#join Telegram @codewindow

Solution in C++ :

//https://codewindow.in
//join our telegram channel @codewindow

#include <iostream>
#include <math.h>

using namespace std;

int main()
{
    int n;
    cin >> n;
    int sum = 0;
    for(int i=1;i<=n;i++)
        sum += pow(i,2);
    cout << sum;
    return 0;
}

Solution in C :

//https://codewindow.in
//join our telegram channel @codewindow

#include <stdio.h>
#include <math.h>

int main()
{
    int n;
    scanf("%d",&n);
    int sum = 0;
    for(int i=1;i<=n;i++)
        sum += pow(i,2);
    printf("%d",sum);
    return 0;
}

Solution in JAVA :

//https://codewindow.in
//join our telegram channel @codewindow

import java.util.*;

class CodeWindow {
    public static void main(String[] args)
    {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int sum = 0;
        for(int i=1;i<=n;i++)
            sum += Math.pow(i,2);
        System.out.println(sum);
    }
}

Output:

3
14
Recent Posts

Leave a Comment

Your email address will not be published. Required fields are marked *