Skip to main content

Posts

Showing posts from September 20, 2018

Conditional Statements C++ - hackerrank

https://www.hackerrank.com/challenges/c-tutorial-conditional-if-else/problem #include <bits/stdc++.h> using namespace std; int main() {     int n;     cin >> n;         //cin.ignore(numeric_limits<streamsize>::max(), '\n');      if(n == 1)       cout << "one";    else if(n == 2){       cout << "two";    }    else if(n== 3){       cout << "three";    }    else if(n == 4){       cout << "four";    }    else if(n == 5){       cout << "five";    }    else if(n == 6){       cout << "six";    }    else if(n ==7){       cout << "seven";    }    else if(n == 8){       cout << "eight";    }    else if(n == 9){       cout << "nine";    }    else if(n>9){       cout << "Greater than 9";    }     // Write Your Code Here     return 0; }

Basic Data Types C++ - hackerrank

Basic Data Types  https://www.hackerrank.com/challenges/c-tutorial-basic-data-types/problem #include <iostream> #include <cstdio> using namespace std; int main() { int i; long l; char c; float f; double d; scanf("%d %ld %c %f %lf", &i, &l, &c, &f, &d); printf("%d\n%ld\n%c\n%0.3f\n%0.9lf\n", i, l, c, f, d); return 0; }

Codewars — Sum of the first nth term of Series

Your task is to write a function which returns the sum of following series upto nth term(parameter). Series: 1 + 1/4 + 1/7 + 1/10 + 1/13 + 1/16 +... Rules: You need to round the answer to 2 decimal places and return it as String. If the given value is 0 then it should return 0.00 You will only be given Natural Numbers as arguments. Examples: SeriesSum(1) => 1 = "1.00" SeriesSum(2) => 1 + 1/4 = "1.25" SeriesSum(5) => 1 + 1/4 + 1/7 + 1/10 + 1/13 = "1.57"     My Solution     #include <iostream> #include <iomanip> using namespace std; class ta { float ar[50]; int j,f,k,q; float sum=0.00; public: void taa(int k) { f = k; } void paa() { for(j=0; j<f; j++) { ar[j] = 1.00/(1 + j * 3); } } void baa() { for(q=0; q<f; q++) { sum = sum+ar[q]; } cout <<setprecision(3)<< sum;