Search This Blog

Showing posts with label Programing. Show all posts
Showing posts with label Programing. Show all posts

Thursday, 1 April 2021

Day 3: Intro to Conditional Statements

Hacker rank 30days of code - Conditional statement Program:

Objective
In this challenge, we learn about conditional statements. Check out the Tutorial tab for learning materials and an instructional video.

Task
Given an integer, , perform the following conditional actions:

  • If  is odd, print Weird
  • If  is even and in the inclusive range of  to , print Not Weird
  • If  is even and in the inclusive range of  to , print Weird
  • If  is even and greater than , print Not Weird

Complete the stub code provided in your editor to print whether or not  is weird.

Input Format

A single line containing a positive integer, .

Constraints

Output Format

Print Weird if the number is weird; otherwise, print Not Weird.


 #include <bits/stdc++.h>
using namespace std;
int main()
{
    int N;
    cin >> N;
    if(N%2!=0){
        cout<<"Weird";
    }else if(N%2==0 && N>1 && N<6){
        cout<<"Not Weird";
    }else if(N%2==0 && 5<N && N<21){
        cout<<"Weird";
    }else if(N%2==0 && N>20 ){
        cout<<"Not Weird";
    }
    return 0;
}

Wednesday, 31 March 2021

c-tutorial-conditional-if-else/problem

 if else hackerrank c++ problem

Given a positive integer , do the following:

  • If , print the lowercase English word corresponding to the number (e.g., one for , two for , etc.).
  • If , print Greater than 9.

Input Format

A single integer, .

Constraints

Output Format

If , then print the lowercase English word corresponding to the number (e.g., one for , two for , etc.); otherwise, print Greater than 9.

Program:

#include<iostream>
#include<string>
#include<bits/stdc++.h>
using namespace std;
int main(){
    int n;
    string a[10]={"Greater than 9", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};
    cin>>n;
    if(n>9){
        cout<<a[0]<<endl;
    }else if(0<n<=9){
        cout<<a[n]<<endl;
    }
    return 0;
}

c++ data type hackerrank basic data type 2nd problem

Since float has a precision of up to only 7 digits, it shows garbage values after its precision is exceeded.

Our double variable shows the correct number because it has a precision of 15 digits, while the number itself consists of 13 digits


Solution 1:

add header  library #include <bits/stdc++.h> and use fixed setprecision(n) in the code

cout<<fixed<<setprecision(3)<<d<<endl;
cout<<fixed<<setprecision(9)<<e<<endl;

Solution 2:

use printf:

printf("%.3f\n", f);
printf("%.9lf", z);


#include <iostream>
#include <cstdio>
#include <bits/stdc++.h>
using namespace std;

int main() {
    int a;
    long b;
    char c;
    float d;
    long double e;
    cin>>a>>b>>c>>d>>e;
    cout<<a<<endl;
    cout<<b<<endl;
    cout<<c<<endl;
    cout<<fixed<<setprecision(3)<<d<<endl;
    cout<<fixed<<setprecision(9)<<e<<endl;
    
    return 0;
}


or

printf("%.3f\n", f);
printf("%.9lf", z);