HackerEarth's basic programming solutions( Count Divisors, Factorial!, Toggle String, Roy and Profile Picture ) :
Problem 7: Count Divisors
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 l,r,k,count=0;
cin>>l>>r>>k;
for(int i=l;i<=r;i++)
{
if(i%k==0)
{
count++;
}
}
cout<<count<<endl;
}
This code is simple. There's no need for any explanation.
Problem 8: Factorial!
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()
{
long long int n,i,f=1;
cin>>n;
for(i=1;i<=n;i++)
{
f=f*i;
}
cout<<f<<endl;
}
This code is simple. There's no need for any explanation.
Problem 9: Toggle String
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;
cin>>s;
for(int i=0;i<s.length();i++)
{
if(s[i]>='A' && s[i]<='Z')
{
s[i]+=32; //see note below
}
else
{
s[i]-=32;
}
}
cout<<s;
}
When you give a look at ASCII table , you can see..by adding 32 to any uppercase alphabet you get the same lowercase alphabet. If the alphabet is in lowercase then get its uppercase by subtracting 32.
Problem 10: Roy and Profile Picture
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 l,w,h,t; //t=no. of test cases
cin>>l>>t;
while(t--)
{
cin>>w>>h;
if((w<l) || (h<l))
cout<<"UPLOAD ANOTHER"<<endl;
else if(w==h)
{
cout<<"ACCEPTED"<<endl;
}
else{
cout<<"CROP IT"<<endl;
}
}
}
This code is simple. There's no need for any explanation.
Guys , if you have any queries related to a question or need more explanation, comment down below!
Comments
Post a Comment