Baby Steps into Programming

Baby Steps into Programming - 3

published on : 16/05/2015

Hi everyone. On this edition of Baby Steps into programming, we'll look beyond what we've done so far. If you're a complete novice in coding, I recommend that you go through Insights into programming, 'cause we are going to program using everything that we learnt there. We'll discuss three programs today. 1) A program to add two integers
                                    2) A program to divide two integers
                                    3) A program to check whether a number is Odd or Even.

And of,course, as always we'll implement these codes in three languages, namely C, C++, Java. But before we do that, today here we'll learn how to create a Pseudo Code. Now a question comes, what is a Pseudo Code. Well, a pseudo code is somewhat of a road map on which you'll build your program. In a pseudo code, we just try to list out steps that we need to perform via our code in order to get a desired result. Pseudo codes are written in ordinary language, not programming languages. Program languages are only concerned with implementation.

So let's try out with a pseudo code to add two numbers:

Step 1:    Declare two integer variables, say A and B
Step 2:    Take input for those numbers
Step 3:    Declare a variable SUM and set SUM = A + B
Step 4:    Print SUM
Step 5:    Exit

Now, let's move on to our second problem. The pseudo code to divide two numbers:

Step 1:    Declare two floating point variables, say A and B
[Why floating point? Division can return fractional values and integers can't hold them, so...]
Step 2:    Take input for those numbers
Step 3:    Declare a variable RES and set RES = A + B
Step 4:    Print RES
Step 5:    Exit

Now, the third an final problem. The pseudo code to check whether a number is odd or even:

Step 1:    Declare an integer variable, say A
Step 2:    Take input for that variable
Step 3:    Check If A mod B = 0, then Print : Even number
                Else Print : Odd Number
[Why? Try dividing an odd number by 2, you'll get a remainder of 1, and if it's an even number, it will be completely divided, there'll be no remainder left. Besides, remember how a set of Even numbers is defined. Even = {n : n = 2m, m Natural Numbers} and 
Odd = {n : n = 2m - 1, m Natural Numbers}. Remember? Also mod here means % or the Modulus operator] 
Step 4:    Exit

Now, as we have finished with the Pseudo Code, let's try implementing the first problem in C, C++, and Java

1) Program  to add two integers

C code :

#include <stdio.h>

int main(int argc, char **argv)
{
    int a, b, sum;
    printf("\nEnter the numbers to be added :\t");
    scanf("%d %d",&a,&b);
/*scanf( ) is the input function for C, comes with the header file stdio.h. It takes arguments with their specified type in a string i.e placed in quotation, and the corresponding variables to take inputs for. %d is for integer. It's aceepts arguments as scanf("%type", &variable_name).*/ 
    sum = a + b;
    printf("\nThe sum is : %d",sum);
/*You can add variables in output too, just place the tag in the string and, supply the variable name, without the &. We'll learn more about the significance of & in inputs in Insights into programming*/
    return 0;

}

C++ code :

#include <iostream>

using namespace std;

int main(int argc, char **argv)
{
    int a, b;
    cout<<"\nEnter the numbers to be added :\t";
    cin>>a>>b;
/*cin is the input stream for C++. You just place, all your input variables behind cin, separated by >>., called the extraction operator*/
    int sum = a + b;
    cout<<"\nThe sum is : "<<sum;
/*You can add variables in output by simply adding it after the string with << or used directly with cout when you have no text to print as cout<<variable*/
    return 0;

}

Java code :

package myjava;


import java.util.Scanner;

public class Myjava
{
    public static void main(String[] args)
    {
        Scanner in = new Scanner(System.in);
/*Scanner is the input tool for Java. It is required to be declared as above*/
        int a, b;
        System.out.print("\nEnter the two numbers to be added : \t");
        a = in.nextInt();
/*a = in.nextInt( ) will mean that any number you enter with your keyboard will be saved in a*/
        b = in.nextInt();
        int sum = a + b;
        System.out.print("\nThe result is : "+sum);
/*Variable can be added in output after strings, that is text in quatation using " "+variablename.
To place more string after variable name, you can go like System.out.print("text"+variable+"text")*
    }
}

2)Program to divide two numbers

C code :

#include <stdio.h>

int main(int argc, char **argv)
{
    float a, b, res;
    printf("\nEnter the numbers to be divided :\t");
    scanf("%f %f",&a,&b);
/* %f is the format tag for floating point variables*/
    res = a / b;
    printf("\nThe sum is : %f",res);
    return 0;

}

C++ code :

#include <iostream>

using namespace std;

int main(int argc, char **argv)
{
    float a, b;
    cout<<"\nEnter the numbers to be divided :\t";
    cin>>a>>b;
    res = a / b;
    cout<<"\nThe sum is : "<<res;
    return 0;

}

Java code :

package myjava;

import java.util.Scanner;

public class Myjava
{
    public static void main(String[] args)
    {
        Scanner in = new Scanner(System.in);
        float a, b;
        System.out.print("\nEnter the two numbers to be divided");
        a = in.nextFloat();
        b = in.nextFloat();
        float res = a/b;
        System.out.print("\nThe result is : "+res);
    }

}

3) Program to check whether a number is Odd or Even

C code :

#include <stdio.h>

int main(int argc, char **argv)
{
    int num;
    printf("\nEnter the number :\t");
    scanf("%d",&num);
    if (num % 2 == 0)
        printf("\nEven Number");
    else
        printf("\nOdd Number");
/*If Else statements are called conditional statements, a subset of methods known as control statements. An if statements tests the condition supplied to it in parenthesis ( ), and if true, executes the statements that follow it. Otherwise, the statements with Else are executed. Here, if the remainder of the division of num and 2 comes out to be zero, Even Number is printed, otherwise Odd Number is printed. We'll learnt about If Else statements in detail in the next Edition of Insights into Programming*/
    return 0;
}

C++ code :

#include <iostream>

using namespace std;

int main(int argc, char **argv)
{
    int num;
    cout<<"\nEnter the number :\t";
    cin>>num;
    if (num % 2 == 0)
        cout<<"\nEven Number";
    else
        cout<<"\nOdd Number";
    return 0;
}

Java code :

package myjava;

import java.util.Scanner;

public class Myjava
{
    public static void main(String[] args)
    {
        Scanner in = new Scanner(System.in);
        int num;
        System.out.print("\nEnter the number : \t");
        num = in.nextInt();
        if(num%2 == 0)
            System.out.print("\nEven Number");
        else
            System.out.print("\nOdd Number");
    }
}

That was all in this Third Edition in Baby Steps into Programming. Now, we'll go ahead with Baby steps into Programming - 4, only after we finish Insights into Programming -2, where we'll learn about escape sequences, formatting output and Control Statements. Try executing these codes and experiment with your own codes and ry new things out. We'll see you next time.

Note: Baby Steps into Programming - 1 and 2 have been archived, but we have decide to have their links in this very page. We'll keep updating links to Archived articles as we go on, so that you can find them right from this very page:

Here goes :
1) Baby Steps into Programming - 1
2) Baby Steps into Programming - 2

No comments:

Post a Comment