Monday, January 30, 2012

if-else statement



if-else is a technique used in programming. It is having a common syntax in most of the programming languages like C, C++, java etc. Its logic is quite simple.

Take a look on the flowchart to understand the logic.



The syntax of if-else statement is

if(condn)
{
stmt1;
}
else
{
stmt2;
}

stmt1 is executed if the condition given in the f statement is satisfied otherwise stmt2 is executed.



Example Program

Program to check whether the number given is prime or not.

#include<iostream.h>
void main()
{
int no;
cout<<"Enter the number :";
cin>>no;
if(no%2==0)
{
cout<<"\n Number is even";
}
else
{
cout<<"\n Number is odd";
}
}

Wednesday, January 25, 2012

A simple C program to print a line

 This post is for a beginner programmar. It is a simple C program to display a line.

#include<stdio.h>
void main()
    {
    printf("A simple C program\n");
    }

Output:
A simple C program

A program starts executing from the main() function. Here there only 1 line for execution in the main function ie. printf("A simple C program \n");
This statement is used to print the values to the standard output device(monitor).
'\n' is an escape sequence for newline. It set the cursor to the next line.
stdio.h is a header file. It has the definition of the function printf(). That is why we are including it.

If you have understood the program then try to write a program to display your name and address in the below given format
Name
Address line 1
Address line 2
Pincode

Tuesday, January 24, 2012

How to run a unix command from a C program

The C function used to run the command is
system();

for eg:
system("ls");
execute ls command whereas

system("pwd");
execute pwd command.

A sample C program

#include<stdio.h>
#include<stdlib.h>
main()
    {
    char c[10]="pwd";
    system(c);
    }

It produces the output as


'pwd' is a unix command for showing the present working directory.

How to generate a random prime number in java

Consider the following program

import java.math.BigInteger;
import java.security.SecureRandom;
import java.util.Random;
public class RandomPrime
    {
    public static void main(String arg[])
        {
            Random ran = new SecureRandom();
            System.out.println(BigInteger.probablePrime(10, ran));
            }
    }

This program will generate a prime number randomly using the built-in function.
Save the program as RandomPrime.java

How to install mysql in ubuntu

open a terminal window and type the following command

sudo apt-get install mysql-server

It will install mysql with no password

How to install java in ubuntu

open a terminal window and type the following command,

sudo apt-get install openjdk-6-jdk

then it will prompt for the password. You have to give the password and press enter. Then it will be done within a few minutes.

Monday, January 23, 2012

for, while do-while loops

Looping are most common in programming languages. It makes programming easier if we understand it correctly. Loops are used to repeat the execution of a set of statement that come inside the set of curly braces { } of that particular loop. There are 3 different types of looping statements- for, while and do-while. Once u understand any one of them, then the rest of them become understandable.

Consider the C++ code below

      1     2    4
for(i=0; i<5; i++)
     {
      cout<<"\n"<<i;        3
      }
(rest of the program)     5

At first the value of i will be 0. It happens by the statement i=0. This statement get executed only once i.e. when it first enters into the loop. Then it checks i<5. If it is yes then the statements inside the loop get executed otherwise the statement after the loop(rest of the program) gets executed. If the statement inside the loop is executed, then the statement i++ executed. It increments the value of i and i becomes 1 this time. Then it checks i<5 and the above mentioned steps continues till the value of i get incremented to 5. It checks i<5. That time i is not less that 5 as the value of i is 5. Then the loop exits.

The sequence of execution will be.
1234343434345

The output will be.
0
1
2
3
4

So if we need to execute a set of statement for 5 times then we should put inside the loop in part 3.

It is same with the case of while loop also, but a little bit difference in the syntax.

i=0;
while(i<5)
   {
   cout<<“\n”<<i;
   i++;
   }

The do-while loop is a bit different from the other 2 loops. It is exit controlled loop but other are entry controlled loops i.e. they perform checking when it enters the loops whereas do-while performs checking after executing the statements. It checks whether to continue the loop.
So that do-while loop performs the execution of the loop atleast once.

Code eg.

i=1;
do
   {
   cout<<i;
   i++;
   }while(i<0);

The output will be.
1

The statement inside the loop get executed once even then the value if i is not less than 0.