Comparing Numbers

Problem Statement

Given two integers, X and Y, identify whether X<Y or X>Y or X=Y.

Comparisons in a shell script may either be accomplished using regular operators (such as < or >) or using (-lt, -gt, -eq, i.e. less than, greater than, equal to) for POSIX shells. This discussion on stack overflow contains useful information on this topic.

Input Format
Two lines containing one integer each (X and Y, respectively).

Output Format
Exactly one of the following lines:
X is less than Y
X is greater than Y
X is equal to Y

Sample Input 1

5  
2  

Sample Output 1

X is greater than Y  

Sample Input 2

2
2  

Sample Output 2

X is equal to Y   

Sample Input 3

2
3  

Sample Output 3

X is less than Y  

Reference Resources
A relevant and interesting discussion on Stack-Exchange for those getting started with handling numbers and arithmetic operations in Bash.

These discussions on stack overflow and geeksforgeeks contains useful information on this topic.

Solution

  
read first
read second
if [ $first -eq $second ]
then
	echo "X is equal to Y"
elif [ $first -gt $second ]
then
	echo "X is greater than Y"
else
	echo "X is less than Y"
fi

The World of Numbers

Problem Statement

Given two integers, X and Y, find their sum, difference, product, and quotient.

Input Format
Two lines containing one integer each (X and Y, respectively).

Input Constraints
100X,Y100
Y0

Output Format
Four lines containing the sum (X+Y), difference (XY), product (X×Y), and quotient (XY), respectively.
(While computing the quotient, print only the integer part.)

Sample Input

5  
2

Sample Output

7
3
10
2

Reference Resources
A relevant and interesting discussion on Stack-Exchange for those getting started with handling numbers and arithmetic operations in Bash.

Solution

  
read a
read b
echo $(($a+$b))
echo $(($a-$b))
echo $(($a*$b))
echo $(($a/$b))


Looping with Numbers

Problem Statement

for loops in Bash can be used in several ways: – iterating between two integers, a and b – iterating between two integers, a and b, and incrementing by c each time – iterating through the elements of an array, etc.

Use _for: loops to display the natural numbers from 1 to 50.

Input
There is no input

Output

1
2
3
4
5
.
.
.
.
.
50  

Recommended Resources

A quick but useful tutorial for Bash newcomers is here.
Handling input is documented and explained quite well on this page.
Different ways in which for loops may be used are explained with examples here.

Solution

  
for x in {1..50}
do
    echo $x
done

A Personalized Echo

Problem Statement

Write a Bash script which accepts name as input and displays a greeting: “Welcome (name)”

Input Format
One line, containing a name.

Output Format
One line: “Welcome (name)” (quotation marks excluded).
The evaluation will be case-sensitive.

Sample Input 0

Dan  

Sample Output 0

Welcome Dan  

Sample Input 1

Prashant

Sample Output 1

Welcome Prashant

A quick introduction to ‘Echo’

This is the equivalent of common output commands in most programming language (print or puts statements).

For Example:

echo "Greetings"

This outputs just one word “Greetings” (without the quotation marks).

echo "Greetings `$USER, your current working directory is $`PWD"

This picks up the values of the environment variables USER and PWD and displays something like:

Greetings prashantb1984, your current working directory is /home/prashantb1984  

The above message, of course, will vary from system to system, depending on the setting of environment variables.

Accepting Inputs

This can be accomplished using the read statement.

read number
echo "The number you have entered is $number"  

If the user provides an input 4, the output is:

The number you have entered is 4  

Consider this snippet:

read name
echo "The name you have entered is $name"  

If the user provides an input Max, the output is:
The name you have entered is Max

Recommended Resources

A quick but useful tutorial for Bash newcomers is here.
Handling input is documented and explained quite well on this page.

Solution

  
read name
echo "Welcome $name"

Looping and Skipping

Problem Statement

for loops in Bash can be used in several ways:
– iterating between two integers, a and b
– iterating between two integers, a and b, and incrementing by c each time
– iterating through the elements of an array, etc.

Your task is to use for loops to display only odd natural numbers from 1 to 99.

Input
There is no input.

Output

1
3
5
.
.
.
.
.
99  

Recommended Resources

A quick but useful tutorial for bash newcomers is here.
Handling input is documented and explained quite well on this page.
Different ways in which for loops may be used are explained with examples here.

Solution

  
for (( c=1; c<=100; c+=2 ))
do
   echo "$c"
done

Let’s Echo

Problem Statement

Write a bash script which does just one thing: saying “HELLO”.

Input Format
There is no input file required for this problem.

Output Format
HELLO

Sample Output
HELLO

A quick introduction to ‘Echo’
This is the equivalent of common output commands in most programming language (print or puts statements).

For Example:

echo "Greetings"

This outputs just one word “Greetings” (without the quotation marks).

echo "Greetings $USER, your current working directory is $PWD"

This picks up the values of the environment variables $USER and $PWD and displays something like:

Greetings prashantb1984, your current working directory is /home/prashantb1984  

The above message, of course, will vary from system to system, depending on the setting of environment variables.

Recommended Resource
A quick but useful tutorial for bash newcomers is here.

Solution

  
echo "HELLO"

Solve me first FP

Problem Statement

This is an introductory challenge. The purpose of this challenge is to give you a working I/O template in your preferred language. It includes scanning 2 integers from STDIN, calling a function, returning a value, and printing it to STDOUT.

Your task is to scan two numbers from STDIN, and print the sum A+B on STDOUT.

Note: The code has been saved in a template, which you can submit if you want.

Input Format
Given A and B on two different lines.

Output Format
An integer that denotes Sum (A + B)

Constraints
1 ≤ A, B ≤ 1000

Sample Input

2
3

Sample Output

5

Solution : In Haskell

  
solveMeFirst a b = a + b

main = do
    val1 <- readLn
    val2 <- readLn
    let sum = solveMeFirst val1 val2
    print sum


Basic Data Types

Problem Statement

C++ has the following data types along with their format specifier:

  • Int (“%d”): 32 Bit integer
  • Long (%ld): 32 bit integer (same as Int for modern systems)
  • Long Long (“%lld”): 64 bit integer
  • Char (“%c”): Character type
  • Float (“%f”): 32 bit real value
  • Double (“%lf”): 64 bit real value

Reading
In order to read a data type, you need the following syntax:

scanf("`format_specifier`", &val)

E.g., in order to read a character and then a double

char ch;
double d;
scanf("%c %lf", &ch, &d);

P.S.: For the moment, we can ignore the spacing between format specifiers.


Printing
In order to print a data type, you need the following syntax:

printf("`format_specifier`", val)

E.g., in order to print a character and then a double

char ch = 'd';
double d = 234.432;
printf("%c %lf", ch, d);

P.S.: For the moment, we can ignore the spacing between format specifiers.

Input Format

Input will consists of an int, long, long long, char, float and double, each separated by a space.

Output Format

Print the elements in the same order, but each in a new line.

Sample Input

3 444 12345678912345 a 334.23 14049.30493

Sample Output

3
444
12345678912345
a
334.23
14049.30493

Solution

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

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


Find Digits

Problem Statement

You are given an integer N. Find the digits in this number that exactly divide N (division that leaves 0 as remainder) and display their count. For N=24, there are 2 digits (2 & 4). Both of these digits exactly divide 24. So our answer is 2.

Note

  • If the same number is repeated twice at different positions, it should be counted twice, e.g., For N=122, 2 divides 122 exactly and occurs at ones’ and tens’ position. So for this case, our answer is 3.
  • Division by 0 is undefined.

Input Format

The first line contains T (the number of test cases), followed by T lines (each containing an integer N).

Constraints
1T15
0<N<1010

Output Format

For each test case, display the count of digits in N that exactly divide N in a separate line.

Sample Input

2
12
1012

Sample Output

2
3

Explanation

2 digits in the number 12 divide it exactly. The first digit, 1, divides it exactly in twelve parts, and the second digit, 2 divides 12 equally in six parts.

1 divides 1012 exactly (and it occurs twice), and 2 also divides it exactly. Division by 0 is undefined, therefore it will not be counted.

This challenge was part of Pragyan 12.

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 */ 
    vector vec;
    int t,n,a,count;
    cin>>t;
    while(t--)
        {
        count = 0;
        cin>>n;
        a=n;
        while(n!=0)
            {
            int temp = n%10;
            vec.push_back(temp);
            n/=10;
            }
        reverse(vec.begin(),vec.end());
        for(std::vector::iterator it = vec.begin(); it != vec.end(); ++it)
            {
                if(*it==0);
                else if((a%*it)==0)
                   count++;
                   
            }
        cout<<count<<endl;
        vec.clear();
    }
    return 0;
}