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.

Saturday, January 21, 2012

String to character array in java


The below given program is to convert the string stored in a String variable to a character array.

class StringChar
{
public static void main(String arg[])
{
String str=new String("knowledge");
System.out.println("String= " + str);
char c[]=new char[20];
c=str.toCharArray();
for(int i=0;i<c.length;i++)
System.out.println("c[" + i +"] = " +c[i]);
}
}

The program insert the string("knowledge") stored in variable str to a character array c[20].
Save the program as StringChar.java

Java program to find the Ascii value of a character


class GetAscii
{
public static void main(String arg[])
{
char c='p';
int i=c;
System.out.println("Ascii value of character " + c + " is " + i);
}
}

To get the ascii value of a character, store the character in an integer variable. in the above program. Variable c is assigned with the value 'p'. When it is assigned to the integer variable say i, the ascii value of the variable get stored in i.
Save the program as GetAscii.java

Friday, January 20, 2012

Introduction to JAVA

  •  Java is purely an object oriented programming language.
  • It was developed by James Gosling at Sun microsystem. Sun microsystem now belongs to Oracle. 
  • It was first named as Oak in 1991 and renamed in 1994 as Java.
Java Features
  1. simple
  2. robust
  3. secure
  4. platform independent
  5. compiled and interpreted

How to save a java program

You may be using some kind of text editors(eg. notepad, gedit etc.) for writing the program and you have to save your program with the extension .java

Whenever you save the program make sure that the program name must be the name of the class in which the main function is written.

eg:

Class Demo
        {
         public static void main(String arg[])
                  {
                   System.out.println("This is a sample java program");
                   }
         }


The program should be saved as Demo.java because main function resides in that class.

How to compile and run a java program

Open a terminal window in ubuntu or command prompt in windows, then type the commands below.


To compile the file
javac FileName.java

To run the file
java FileName

Please refer how to save a java program.

How to convert BigInteger to int in java



import java.math.BigInteger;
class ConvertBig
    {
    public static void main(String arg[])
        {
        BigInteger big= new BigInteger("1234");
        System.out.println("Value of BigInteger big= " + big);    
        int i = big.intValue();
        System.out.println("Value of Integer i= " + i);
        }
    }



This program shows how to convert a BigInteger value to an integer value in java. the program convertsthe value in 'big' which is aBigInteger variable to an integer variable 'i'.
Save program as ConvertBig.java

Java program to select a random number within a range


import java.util.Random;
public class Exercise
      {
       public static void main(String arg[])
              {
              Random rndNumbers = new Random();
              int min=10, max=50;
               int randomNum = rndNumbers.nextInt(max - min + 1) + min;
               System.out.println("Number: " + randomNum);
              }
      }


The above given program select a number randomly within the range 10 (min=10) and 20 (max=20).
Save the program as Exercise.java