HackerEarth's basic programming solutions( Life the Universe and Everything, Ladderophilia, Char Sum ) :

Problem 30: Life, the Universe, and Everything

Solution: (in c++)


( please guys before moving to the solution try it yourself at least 3-4 times , if you really wanna become a good coder)

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n;
    while(n!=42)
    {
        cin>>n;
        if(n!=42)
        cout<<n<<endl;
    }
    
}

This code is simple. There's no need for any explanation.
 

 

 

 

Problem 31:  Ladderophilia

Solution: (in c++)


( please guys before moving to the solution try it yourself at least 3-4 times , if you really wanna become a good coder)

#include<bits/stdc++.h>
using namespace std;
int main()
{
int n;
cin>>n;
cout<<"* *"<<endl;
for(int i=0;i<n;i++)
{
   cout<<"* *"<<endl;
   cout<<"*****"<<endl;
   cout<<"* *"<<endl;
}
   cout<<"* *"<<endl;
}

This code is simple. There's no need for any explanation.
 

 

 

 

Problem 32: Char Sum

Solution: (in c++)


( please guys before moving to the solution try it yourself at least 3-4 times , if you really wanna become a good coder)

#include<bits/stdc++.h>
using namespace std;
int main()
{
string s;
     int sum=0;
     cin>>s;
     for(int i=0;i<s.length();i++)
     {
         sum=sum+(s[i]-96); //see note below
     }
     cout<<sum<<endl;   
}

Take a look at the ASCII table. For 'a' the ASCII value is 97. Therefore , when we subtract 96 from it we will get 1. Similarly we can get the weights of all alphabets as required in the question by subtracting 96 from the ASCII value of every lower case alphabet. 
 

 

 

Guys , if you have any queries related to a question or need more explanation, comment down below! 

Comments

Popular posts from this blog

Coursera's Algorithmic toolbox assignment solutions( Sum of Two Digits, Maximum Pairwise Product ) :

HackerEarth's basic programming solutions( Seating Arrangement, Zoos, Anagrams ) :

HackerEarth's basic programming solutions( Minimize Cost, Magical Word, Best Index ) :