Friday, January 1, 2021

Structs | HackerRank | C++ | Solution | Color The Code



Problem : https://www.hackerrank.com/challenges/c-tutorial-struct/problem

Solution :

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;

/*
    add code for struct here.
*/

struct Student{
int age;
string first_name;
string last_name;
int standard;    
};

int main() {
    Student st;
    
    cin >> st.age >> st.first_name >> st.last_name >> st.standard;
    cout << st.age << " " << st.first_name << " " << st.last_name << " " << st.standard;
    
    return 0;
}

Strings | HackerRank | C++ | Solution



Problem : https://www.hackerrank.com/challenges/c-tutorial-strings/problem

Solution :

Conditional Statements | HackerRank | C++ | Solution | color the code



Problem : https://www.hackerrank.com/challenges/c-tutorial-conditional-if-else/problem

Solution : 

#include <bits/stdc++.h>


using namespace std;



int main()
{
    int n;
    cin >> n;
    cin.ignore(numeric_limits<streamsize>::max(), '\n');

    // Write Your Code Here
    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{
        cout<<"Greater than 9";
    }
    
    return 0;
}

Thursday, December 31, 2020

Basic Data Types | HackerRank | Solution | C++



Problem : https://www.hackerrank.com/challenges/c-tutorial-basic-data-types/problem

Solution : 

#include <iostream>

#include <cstdio>
using namespace std;

int main() {
    // Complete the code.
    int a;
    float b;
    char c;
    double d;
    long l;
    scanf("%d %ld %c %f %lf",&a ,&l,&c,&b,&d);
    printf("%d\n%ld\n%c\n%0.3f\n%.9lf",a,l,c,b,d);
    return 0;
}

Input and Output | Hackrrank | C++ | Solution



Problem : https://www.hackerrank.com/challenges/cpp-input-and-output/problem

Solution : #include <cmath>

#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;


int main() {
    /* Enter your code here. Read input from STDIN. Print output to STDOUT */   
    int a , b , c;
    cin>>a>>b>>c;
    int sum = 0;
    sum = a + b + c;
    cout<<sum;
    return 0;
}

Say "Hello, World!" With C++ | Hackerrank | C++ | Solution



Problem : https://www.hackerrank.com/challenges/cpp-hello-world/problem

Solution : 


1 - #include <iostream>

#include <cstdio>
using namespace std;

int main() {
    printf("Hello, World!");
    return 0;
}




2 - #include <iostream>
#include <cstdio>
using namespace std;

int main() {
    char str[] = "Hello, World!";
    printf("%s",str);
    return 0;
}


3 - #include <iostream>
#include <cstdio>
using namespace std;

int main() {
    cout<<"Hello, World!";
    return 0;
}


Wednesday, December 30, 2020

Java If-Else | HackerRank | Java | Solution | Color The Code



Problem : https://www.hackerrank.com/challenges/java-if-else/problem

Solution : import java.io.*;

import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.regex.*;

public class Solution {



    private static final Scanner scanner = new Scanner(System.in);

    public static void main(String[] args) {
        int N = scanner.nextInt();
        scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
        if((N&1) == 1){
            System.out.println("Weird");
        }
        else if(N>=2 && N<=5){
            System.out.println("Not Weird");
        }
        else if(N>=6 && N <=20){
            System.out.println("Weird");
        }
        else if( N > 20){
            System.out.println("Not Weird");
        }
        scanner.close();
        
      
    }
}

Java Stdin and Stdout I HackerRank | JAVA | Solution | Easy | Color The Code



Problem : https://www.hackerrank.com/challenges/java-stdin-and-stdout-1/problem

Solution : import java.util.*;


public class Solution {

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int a = scan.nextInt();
        // Complete this line
        int b = scan.nextInt();
        // Complete this line
        int c = scan.nextInt();
        
        System.out.println(a);
        // Complete this line
        System.out.println(b);
        // Complete this line
        System.out.println(c);
        
        scan.close();
    }
}

Welcome to Java! | HackerRank | Java | Easy | Solution



Problem : https://www.hackerrank.com/challenges/welcome-to-java/problem

Solution : public class Solution {


    public static void main(String[] args) {
        /* Enter your code here. Print output to STDOUT. Your class should be named Solution. */
        System.out.println("Hello, World.");
        System.out.println("Hello, Java.");
    }
}


Tuesday, December 29, 2020

StringStream | HackerRank | Solution | C++ | Easy



Problem : https://www.hackerrank.com/challenges/c-tutorial-stringstream/problem

Solution :  

#include <sstream>

#include <vector>
#include <iostream>
using namespace std;

vector<int> parseInts(string str) {
    // Complete this function
    vector<int>v;
    
    stringstream s(str);
    int i;
    char c;
    
    while(s >> i){
        v.push_back(i);
        s>>c;
    }
    
    return v;
}

int main() {
    string str;
    cin >> str;
    vector<int> integers = parseInts(str);
    for(int i = 0; i < integers.size(); i++) {
        cout << integers[i] << "\n";
    }
    
    return 0;
}

Variable Sized Arrays | Hackerrank | Solution | C++

 

                                                    


Question : https://www.hackerrank.com/challenges/variable-sized-arrays/problem

Solution : #include <cmath>

#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;


int main() {
    /* Enter your code here. Read input from STDIN. Print output to STDOUT */ 
    int size , q;
    cin>>size >>q;
      
    int ** A = new int*[size];
    for(int i =0;i<size;i++){
        int s;
        cin>>s;
        A[i] = new int[s];
        for(int j = 0;j<s;j++){
            cin>>A[i][j];
        }
    }
    
    for(int i =0;i<q;i++){
        int r , c;
        cin>>r>>c;
        cout<<A[r][c]<<endl;
    }
    
/* Free your memory using this code
for(int i = 0; i < size; ++i)
{
    delete[] A[i];
}
delete[] A;

*/
    return 0;
}

Monday, December 28, 2020

Arrays Introduction | Hackerrank | C++ | Easy | Solution

Problem : https://www.hackerrank.com/challenges/arrays-introduction/problem

 Solution : 

#include <cmath>

#include <cstdio>

#include <vector>

#include <iostream>

#include <algorithm>

using namespace std;


void reverse(int A[],int size){

    for(int i = size -1 ;i>=0;i--){

        cout<<A[i]<<" ";

    }

}


int main() {

    /* Enter your code here. Read input from STDIN. Print output to STDOUT */ 

    int size;

    cin>>size;

    int A[size];

    for(int i =0;i<size;i++){

        cin>>A[i];

    }

    

    reverse(A,size);

    return 0;

 

}




Pointers : Hackerrank | Easy | Solution | C++


Problem : https://www.hackerrank.com/challenges/c-tutorial-pointer/problem

Solution : 

 #include <stdio.h>

#include<math.h>

void update(int *pa,int *pb) {

    // Complete this function 

    int temp = *pa;

    *pa = *pa + *pb;

    *pb = abs(temp - *pb);

}


int main() {

    int a, b;

    int *pa = &a, *pb = &b;

    

    scanf("%d %d", &a, &b);

    update(pa, pb);

    printf("%d\n%d", a, b);


    return 0;

}