Showing posts with label Hackerrank Project Euler solutions. Show all posts
Showing posts with label Hackerrank Project Euler solutions. Show all posts

Project Euler #13: Large sum | Hackerrank | Project Euler

  Large sum


Work out the first ten digits of the sum of N 50 - digit numbers.

Input Format

First line contains N, next N lines contain a 50 digit number each.

Constraints

1<=N<=10^3

Output Format

Print only first 10 digit of the final sum

Sample Input

5
37107287533902102798797998220837590246510135740250
46376937677490009712648124896970078050417018260538
74324986199524741059474233309513058123726617309629
91942213363574161572522430563301811072406154908250
23067588207539346171171980310421047513778063246676


Sample Output

2728190129

Explanation

Summing the numbers we get 2728190129820036131461476701043585006837989465343, first 10 digits are 2728190129.

Solution in python:


n = int(input())
tot=0
for i in range(n):
    a=int(input())
    tot=int(tot)+a;
while (tot >= 10000000000):
    tot = tot // 10
print(tot)



You can also run it on an online IDE :



Your feedback are always welcome! If you have any doubt you 
can contact me or leave a comment!  
Happy Coding!! Cheers!!!


Project Euler #17: Number to Words | Hackerrank | Project Euler

 Number to Words


The numbers 1 to 5 written out in words are One, Two, Three, Four, Five 

First character of each word will be capital letter for example:
 104382426112 is 

One Hundred Four Billion Three Hundred Eighty Two Million Four Hundred Twenty Six Thousand One Hundred and Twelve

Given a number, you have to write it in words.

Input Format

The first line contains an integer T, i.e., number of test cases.
Next T lines will contain an integer N.

Constraints

1<=T<=10
0<=N<=10^2

Output Format

Print the values corresponding to each test case.

Sample Input

2
10
17

Sample Output

Ten 
Seventeen

Solution : Source (Hackerrank)


#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
char digits[20][10] = {"","One","Two","Three","Four",
"Five","Six","Seven","Eight","Nine","Ten","Eleven",
"Twelve","Thirteen","Fourteen",
"Fifteen","Sixteen","Seventeen","Eighteen","Nineteen"};
char tens[11][10] =  {"","","Twenty","Thirty","Forty",
"Fifty","Sixty","Seventy","Eighty","Ninety"};

void word(int num){
    if(num/100)
        printf("%s Hundred ", digits[num/100]);
    if((num%100) < 20 && (num%100) > 0)
        printf("%s ",digits[num%100]);
    else if((num/10)%10){
        printf("%s ", tens[(num/10)%10]);
        if(num%10)
            printf("%s ", digits[num%10]);
    }
}

int main() {
    int t;
    scanf("%d", &t);
    while(t--){
        long long int num;
        scanf("%lld", &num);
        int tn = num / 1000000000000;
        int bn = (num / 1000000000) % 1000;
        int mn = (num / 1000000) % 1000;
        int th = (num / 1000) % 1000;
        int hd = num % 1000;
        //printf("%d %d %d %d %d\n", tn, bn, mn, th, hd);
        if((tn + bn + mn + th + hd) == 0)
            printf("Zero");
        if(tn){
            word(tn);
            printf("Trillion ");
        }
        if(bn){
            word(bn);
            printf("Billion ");
        }
        if(mn){
            word(mn);
            printf("Million ");
        }
        if(th){
            word(th);
            printf("Thousand ");
        }
        if(hd){
            word(hd);
        }
       printf("\n"); 
    }
    return 0;
}


You can also run it on an online IDE :


Your feedback are always welcome! If you have any doubt you 
can contact me or leave a comment!  
Happy Coding!! Cheers!!!




Super Market Problem | TCS Code Vita 2023 - Zone 1 | Super Market TCS Code Vita 2023 Solution | Code Vita 2023 | Code Vita 2023 season 11 solution

 Problem Description: In a Super market we will find many variations of the same product. In the same way we can find many types of rice bag...