Binary Search Program in C…!!!

As one of the basic searching technique we learned about Linear Search.Today we shall learn about Binary Search technique.In linear searching method we compared each element of the array with given key.The main drawback of the linear search is,”if the key present in the last position of the array then,number of steps to find the element is n”, where n is size of the array.For an array with size less than 10,does not matter.But for an array with size of 1000,the number of steps needed to search the element matter’s a lot.

The main essence of this method is…….just divide the entire array into parts comparing with middle value of the array.Let us consider an example…….Let the elements of the array be 1 2 3 4 5.The key to search in the array is 1.
Step 1: Sort the given array in ascending order(you can sort in any way)
Step 2: Compare the given key element with middle value of the array.
Step 3: If the element at middle is less than the key then consider the elements from starting to the middle of the array.
Step 4: If the element at middle is greater than the key then consider the elements from middle of the array to ending of the array
Step 5: Now repeat from step 2 to step 4 until you search the element.

The code for the above program is:

#include<stdio.h>
#include<conio.h>
int bsearch(int[],int,int);
int i;
void main()
 {
	int a[100],j,n,pos=-1,key;
	clrscr();
	printf("Enter array size");
	scanf("%d",&n);
	printf("Enter values into array");
	for(j=0;j<n;j++)
	 {
		scanf("%d",&a[j]);
	 }
	printf("Enter the value to be serached");
	scanf("%d",&key);
	pos=bsearch(a,key,n);
	if(pos==-1)
	 {
		printf("Unsucess full");
	 }
	else
	 {
		printf("sucess full");
		printf("the element is found at %d",pos);
	 }
	getch();
 }
int bsearch(int a[],int key,int n)
{
	int first=0,last=n-1,middle;
	while(last>=first)
	 {
	 	middle=(first+last)/2;
		if(key<a[middle])
			last=middle-1;
		else if(key>a[middle])
			first=middle+1;
		else
			return middle;
	}
    return -1;
}


OUTPUT:
bsearch
To download the program click here:download

Advertisements
Posted in C | Tagged , , , , , , , , , , | Leave a comment

Control Structures in C…!!!(Iteration or Looping)

In our earlier post we presented you about Decision Making Statements in C……..In this post we are going to learn about Looping statements or Iterative statements in C.The main advantage of loops is we can execute the same steps more than single item.For ex: if we want to print the hello world statement for 10 times we can’t write it 10 times,hence we use looping and make it print 10 times using a condition.There are three type of Iterative statements:
1.while.
2.do while.
3.for loop.
Let us learn about these statements in detail and those syntax:
1.while statement:while loop is also termed as entry controlled as it checks the condition in the starting.The syntax of the while statement is

while(condition 1)
 {
     statements to be executed;
  }

If the given condition is true,then only the statements present inside the while statement are executed.If the condition is not satisfied control goes to other statement.Consider an example of using while statement

int i=1;                //variable to control the while loop
while(i<=10)
 {
       printf("\nHello world");
       i++;            //Increment of the variable.
  }

The output of the above part is printing Hello world line for 10 times but by using a single statement.
2.do….while:It is termed as exit controlled because it checks the condition at the exit of the while….With do while we are going to execute the statements of the while for a minimum of one time…..and if condition is satisfied it re-executes those statements in loop.The syntax of the do…while is

do
 {
     statements to execute
 }while(condition);

Example illustrating the do…while is

int i=11;
 do
  {
      printf("\nHello world");
  }while(i<10);

The output of the above statement is a single line Hello world…since the condition has been failed it is not going to be executed again.
3.for statement:The main drawback of the while and do while statements is we need to initialize the variable outside the loop and increment the variable to control the loop.In for loop we can initialize the variable,check for the condition and increment the variable.The syntax of the for loop is:

for(variable initialize;condition of the variable;incrementing the variable)
 {
      statements to execute;;
  }

Let us make an example to demonstrate the use of the for loop

for(i=1;i<10;i++)
 {
       printf("\nHello world");
 }

The flow of the control while executing for loop is…….It firsts initialize the variable….checks for the condition…if the condition is true then the statements are executed….After the end of the statements the control goes to incrementing part of the variable…..after that it checks the condition and control passed to the statements in loop.

The output of the above program is Hello world line for 10 times.

With this we reached the end of control strucutres in C…….If you have any queries please post them in comments.

Posted in C | Tagged , , , , , , | Leave a comment

Linear Search Program in C

In this tutorial we shall try to make a program for Linear Search.Linear Search is the basic and easiest searching method and is for junior programmers.The steps involved in searching a number using Linear Search algorithm is:
Method

1.Get the data from the user and store it in an array.
2.Ask the user to enter the number which he interested to search.
3.Make a for loop such that,in it compare each element of the array with the key given by the user.
4.If the element is found…..Display the appropriate the message to the user.
5.If the element is not found…..display the appropriate the message to the user.

Now the code is as follows:

#include<stdio.h>
#include<conio.h>
void main()
{
  int data[50],size,i,key,flag=0;

  clrscr();
  printf("\nEnter the Array Size:");    //Get the size of the array going to be entered
  scanf("%d",&size);

  printf("\nEnter the Array Elements:"); //Enter those number of elements in to the array
  for(i=0;i<size;i++)
    scanf("%d",&data[i]);
 
  printf("\nEnter the Key:");            //Enter the key to search in array
  scanf("%d",&key);

  for(i=0;i<size;i++)                  //In this for loop we are going to compare each element with key 
  {
      if(data[i]==key)
	   flag++;
  }
  if(flag!=0)
       printf("\nkey found in the given data",i);
  else
      printf("\nKey not found in the given data");
  getch();
}

OUTPUT:
l_search
YOu can download the program:download
IF you have any doubts you can comment them.

Posted in C | Tagged , , , , , , | Leave a comment

Decision Making Statements in C

Have you ever thought of controlling the execution of the program…..!!!.Those which control the flow of the program are called as control Structure of a program.These control structures are common for all programming languages irrespective of their designing.

Control Structures are broadly divided into two types:
1.Decision Making statement or Conditional Branching.
2.Looping or Iterative statements.
In this post we shall learn about both of these.
Decision Making Statements
These decision making statements are divided into four types.
1.simple if
2.if…else
3.nested if else
4.else if ladder.
1.simple if:If we need to check a single condition and then execute the program depending on the result of the condition then we use a simple if statement.The syntax of the simple if statement is:

        
if(condition)
 {
    statements to execute
  }</b> 

eg:

if(a>b)
{
    printf("A value is greater than B);
}

2.if…else:In the above case if the condition fails we don’t have other option to execute.Hence we use the if…else statement to display an appropriate message when the condition fails.The syntax of the if…else statement is

if(condition)
{
   statements to execute
}
else
{
  statements to execute
}

for eg:

if(a>b)
 {
        printf("A value is greater than B);
 }
else
 {
    printf("B value is greater than A);
  }

3.Nested if else:After an initial condition if we need to check another condition under it then we use Nested if else.That is if statement is placed in another if statement.The syntax of nested if else is;

  if(condition 1)
    {
       if(condition 2)
         {
            statements to execute
          }
       else
         {
            statements to execute
          }
     }
   else
     {
        statements to execute
      }

For eg:While comparing three numbers

if(a>b)
 {
     if(a>c)
      {
         printf("A is greatest of all the three");
      }
     else
      {
          printf("B is greatest of all the three");
      }
 }
else
  {
      if(b>c)
        {
           printf("B is greatest of all the three");
         }
      else
         {
             printf("C is greatest of all the three");
         }
  }

4.else…if:If we need to check for multiple conditions then we use else..if statement.The syntax of the else…if statement is:

 if(condition 1)
  {
      statements to execute;
  }
 else if(condition 2)
  {
      statement to execute;
  }
else if(condition 3)
  {
    statements to execute;
  }
else 
 {
   statements to execute;
  }

With this we complete the explanation of decision making statements.The next post will be about Iterative statements…..

Posted in C | Tagged , , , , , | 2 Comments

C program to calculate area and perimeter of a Cirlce…!!!

In our earlier post we calculated the area and perimeter of a circle.In this post we are dealing with calculating the area and perimeter of a circle.

The main input for calculating area and perimeter of a circle is radius of the circle.We use the datatype float to store the radius of the circle.The formula to find the area of the circle is 3.14*r*r and the formula to find perimeter of the circle is 2*3.14*radius.After calculating the area and perimeter we need to display the results to the user.

#include<stdio.h>
#include<conio.h>
void main()
{
  float radius,area,perimeter;  //variable to store radius,area and perimeter
  clrscr();
  printf("\nEnter the radius of the circle:");
  scanf("%f",&radius);
  area = 3.14*radius*radius;
  perimeter = 2*3.14*radius;
  printf("\nArea of the given cirlce is:%f",area);
  printf("\n\nPerimeter of the given circle is:%f",perimeter);
  getch();
}

OUTPUT:
cirareaperi
You can download the program:download
If you have any doubts you can place them in comments.

Posted in C | Tagged , , , , | 4 Comments

C program to calculate area and perimeter of a Rectangle…!!!

In this tutorial we are going to write a program to calculate area and perimeter of a rectangle.Here we are going to use input and output statement for programming.

1.The main input of this program is length and breadth of the rectangle.we are going to take input from the user through scanf() statement.
2.As a processing step we need to calculate the area and perimeter of the rectangle.
3.After calculating the area and perimeter we are going to display the area and perimeter.

Hence from the above points we are going to write the program.

#include<stdio.h>
#include<conio.h>
void main()
{
  float length,breadth,area,perimeter;  //variables to store the values from the users
  clrscr();                                               //To clear the output screen
  printf("\nEnter the length of the rectangle:");
  scanf("%f",&length);
  printf("\nEnter the breadth of the rectangle:");
  scanf("%f",&breadth);
  area = length * breadth;                    //Calculating the area using the formula.
  perimeter = 2*(length + breadth);    //Calculating the perimeter using the formula.
  printf("\nArea of the rectangle with length %d and breadth %d is:%f",(int)length,(int)breadth,area);
  printf("\n\nPerimeter of the rectangle with length %d and breadth %d is:%f",(int)length,(int)breadth,perimeter);
  getch();
}

OUTPUT:
recareaperi
You can download the program:download
If you have doubts please mention them in comments

Posted in C | Tagged , | 3 Comments

Hello world Program and Basic Arithimetic Operations Program in C

In this tutorial we shall start learning Programming through C.As a basic program of every programming language we shall start with Hello world program and some basic arithmetic program.

For a Hello world we need to just display the “Hello world” to user.For this we shall use printf statement which we explained in our earlier post.As we are using an output statement we include stdio.h header statement.
The program of Hello World is:

#include<stdio.h>
void main()
{
  printf("HELLO WORLD");
}

OUTPUT:

HELLO WORLD

This is how we need to use the input/output statement in body of a program.Now we shall write a program to perform all the basic arithmetic operation on inputs given by user using scanf() statement.The flow of the program is:
1.We need to get two values from the user.
2.Perform all basic arithmetic operation such as: addition,subtraction,multiplication,division.
3.Display those output values to the user with respective text display.
The main important point in usage of division is we get the remainder as an output of division performed.
Now we shall write the code basing on above guidelines:

#include<stdio.h>
void main()
{
  int x,y;   //Declaration of integer type variables
  int add,sub,mul,div;
  printf("\n  Enter first integer value as an input:");
  scanf("%d",&x);  //Getting the value from user and storing it in x.
  pirntf("\n  Enter second integer value as an input:");
  scanf("%d",&y);  //Getting the value from user and storing it in y.
  add = x+y;
  sub = x-y;
  mul = x*y;
  div = x/y;
  printf("\n  Addition of given two numbers is:%d",add);
  printf("\n  Subtraction of given two numbers is:%d",sub);
  printf("\n  Multiplication of given two numbers is:%d",mul);
  printf("\n  Division of given two numbers is:%d",div);
}

OUTPUT:
arithimetic
You can download the arithmetic program:download

Posted in C | Tagged , , , , , , , , , | 1 Comment

Creating your first working page with Java!

ProgramThat;

Following on from the first page of our app. We are now going to make some of the features work!

1. Go to MyActivity in the src folder.

2. The first thing you need to look at is that it extends activity. 

You are saying to java you want to use some of the predefined methods in this class one of which is onCreate. 

In every Java file you are most likely going to have an onCreate method, therefore they always have to extend Activity.

Only Java classes should be extended. 

3. You should also implement an onClickListener. In doing so you are saying to Java that there is going to some form of button on this page.

To do so it should look like this:

implements View.OnClickListener Image

Dont worry about if it comes up red this is because you haven’t added in the method yet.

4. Just bellow the…

View original post 315 more words

Posted in Android Programming | Leave a comment

Constructors in C++

What is a Constructor…???It is a method which have it’s name same as it’s respective class name.In this tutorial we shall learn about constructors in detail.

The main properties of constructors are:
1.Name of the constructor is same as class name.
2.They don’t have any return type….even void too.
3.Constructor are invoked implicitly whenever an object is created.

They are two types of constructors in C++.
1.default constructor.
2.parameterized constructor.

A constructor which do not have any parameters is called as default constructor.
A constructor which takes one or more parameters is called as parameterized constructor.
Let us write a simple code to demonstrate the use of constructors in programming.

#include<iostream.h>
#include<conio.h>
class cont_demo
{
  public:
  cont_demo();      //default constructor
  cont_demo(int ); //parameterized constructor
}
cont_demo :: cont_demo()
{
  cout<<"\n\n\t\t   *****DEFAULT CONSTRUCTOR IS INVOKED*****";
}
cont_demo :: cont_demo(int i)
{
  cout<<"\n\n\t\t*****PARAMETERIZED CONSTRUCTOR IS INVOKED*****";
}
void main()
{
   clrscr();
   cont_demo ob;
   cont_demo ob1(1);
   getch();
}

But the above program is also an example of method overloading…i.e using the same name for two or more methods with a difference in number of parameters.
OUTPUT:
const_demo
You can download the program:download

Posted in C++ | Tagged , , , , , , , | Leave a comment

Binary Search Program in C++

In our earlier post we learned about Linear search.Today we shall learn about other type of searching technique…i.e is binary searching.This method is different form the earlier one.We shall go in detail about this.

If the data given by the user is stored in the array then the array will have three variable to named as first,last and middle.Each of the three variables are used in searching of the given array.

Method:1.Get the data from the user and store it in array.
2. Initialize the variables first to zero,last to the length of the array and middle to (first+last)/2.
3.Get the key value from the user to search in the array.
4.Now make a while loop with condition last>=first.
5.In the loop if element present in the middle of the given array is greater than key element then assign first to middle+1.
6.If the element in the middle of the given array is less than the key then assign last to middle-1.
7.If the element present in the middle of the array is equal to key then searching is completed.
8.End the loop.

Let us write the program following the above steps.

#include<iostream.h>
#include<conio.h>
class bsearch
{
  public:
  int data[10],n,key,first,last,middle;
  void getdata();
  void display();
};
void bsearch :: getdata()
{
  cout<<"\nEnter the length of the array:";
  cin>>n;
  for(int i=0;i<n;i++)
   {
     cout<<"\nEnter the element at position"<< (i+1)<<" of the array:";
     cin>>data[i];
   }
  cout<<"\nEnter the key to find the element in the array:";
  cin>>key;
}
void bsearch :: display()
{
  first=0;
  last=n-1;
  middle=(first+last)/2;
  while(last>=first)
  {
    middle=(first+last)/2;
    if(key>data[middle])
      first=middle+1;
    else if(key<data[middle])
      last=middle-1;
    else
    {
      cout<<"\nKey "<<key<<" found in the given array";
      break;
    }
  }
}
void main()
{
 clrscr();
 bsearch ob;
 ob.getdata();
 ob.display();
 getch();
}

OUTPUT:
bsearch
You can download the program:download
If you have any doubts please comment them.

Posted in C++ | Tagged , , , , , , | 1 Comment

Linear Search Program in C++

In this tutorial we shall learn about searching elements present in the array.Generally two methods are being followed for searching.Those are:

1.Linear searching
2.Binary searching

.In this tutorial we shall learn about Linear searching.
Method:
1.Get the data from the user in an array.
2.Ask the user to enter an element to search in the array.
3.Compare the given key element to each and every element in array
4.If element is present in the given array, then display the position of the element.

Let us write the code for it.

#include<iostream.h>
#include<conio.h>
class lsearch
{
  public:
  int data[10],n,key;
  void getdata();
  void display();
};
void lsearch :: getdata()
{
  cout<<"\nEnter the length of the array:";
  cin>>n;
  for(int i=0;i<n;i++)
  {
   cout<<"\nEnter the element in the "<<(i+1)<<" position of the array:";
   cin>>data[i];
  }
  cout<<"\nEnter the key to find the element in the array:";
  cin>>key;
}
void lsearch :: display()
{
  int flag=0;
  for(int i=0;i<n;i++)
   {
     if(key == data[i])
     {
       cout<<"\n\nThe element "<<key<<" is present in the position "<<(i+1)<<" of the array";
       flag++;
     }
   }
   if(flag==0)
    cout<<"\nGiven key "<<key<<" is not present in the array";
}
void main()
{
  clrscr();
  lsearch ob;
  ob.getdata();
  ob.display();
  getch();
}

OUTPUT:
LSEARCH
You can download the program:download
If you have any doubts you can make a comment

Posted in C++ | Tagged , , , , , , , , , | 2 Comments

Input and Output Statements in C

In our earlier post we introduced you to C language.Today we shall learn about input and output statements in C.These statements are used for intake of data from the user and display the output after processing to the user.

First we shall learn about the output statements present in C language.The main and important output statement present in C is “printf”.This is a keyword and we include the stdio.h header file in our program.The syntax of the printf statement is as follows:

printf(“———“);

In the place of dashed line you can place your message for the user.There are other types of input statements in C as substitute for “printf” statement.The other output statements are:
1. puts().
2. putchar().
1.When we need to display strings then we use puts() function to display.
2.When we need to display character by character then we use putchar() method to display.

Let us write a program to demonstrate the use of printf().

#include<stdio.h>
void main()
{
  printf("Welcome to the world of Programmming");
}

OUTPUT:
Welcome to the world of Programming
So….we have learnt how to display our message to the user.Let us go to the next part of our today’s tutorial….i.e about input statements.
The main and most important input statement used in C programming is “scanf()” statement.The syntatx of the “scanf()” statement is:

scanf(“**”,&var_name);

In the above syntax…… in the place of ** we need to enter a suitable format specifier.Format specifier is different for each of the datatype present in C.
1.int —- %d
2.char —- %c
3.string —- %s
4.Float —- %f.
These are format specifier and we need to remember them till the extinct of C language. Let us consider that a user is giving a number is input and we are going to store it a variable(named as num).Then the scanf() statement is as follows:
scanf(“%d”,&num);
This is how we write a scanf() statement.The other types of input statements are:
1.gets()
2.getchar().

These functions work similar to puts() and putchar().

Write a C Program such that we take a number as an input from the user and display the same number to him.

#include<stdio.h>
void main()
{
  int num;  //declaration of a variable
  printf("Please enter any number:") // using printf() to display the message
  scanf("%d",&num); // using scanf() statement and format specifiers
  printf("Given number by the user is:%d",num);

In the last printf() statement we again used format specifier to display the number present in the variable num……..From this we can say that we can use format specifiers in both printf() and scanf() statements.

OUTPUT:
Please enter any number:6
Given number by the user is:6
So,this is the end of today’s tutorial and next tutorial we shall learn about data types.

Posted in C | Tagged , , , , , | Leave a comment

Introduction to C

C is a programming language which is developed by Dennis Ritchie between the years 1963 and 1973.He developed this language in AT&T Bell Labs.Actually C got it’s name as a sequence to it’s parents versions.First Dennis Ritchie named it as A and B.After some updates he named it as C and is popular by that name.

STRUCTURE OF C PROGRAM

Today we shall know about the files present in the C and about installing the C compiler for it. The files where meaning of all the keywords present in C are defined in Header files. There are large number of header files defined for C.We need to include them in our program according to usage of our program.Some of header files are used commonly in every program are given below:
1.stdio.h This header file contains the definition of all input/output keywords
2.conio.h This header file contains the definition of all console input and output.
3.math.h This header file contains the definition of all math function defined in C and used in C.
4.string.hThis header file contains the definition of ass string functions defined in C and used in C.
To use these header in our program we need to use the keyword #include.

#include<stdio.h>
#include<conio.h>
{
    //Body of the program.
}

In this way we need to use them in our program.Now we shall go detail about body of program.The body of a program starts with defining a main method.A C program must contain the main function.For this we use the keyword main.Here we need to specify the return type of the main method.A return type can be of any one…!!!but from the below list only
1.int
2.float
3.char
4.void
There are lot of sub divisions among int,float,char…we shall learn them in next tutorial.Therefore a return can be anything.Let us consider return type as void.With this we have started writing a program.Now we need to start writing body of the program.Body of a program starts with opening of curly braces({) and body of the program ends with closing of curly braces(}).This is called as the structure of C program.

#include<stdio.h>
#include<conio.h>
void main()
{
   // body of the program
}

In our next tutorial we shall learn about basic statements present in the C language……..Until then sailing off

Posted in C | Tagged , , , , , | 1 Comment

Programming with Packages…!!!

In our earlier post we introduced you to package in Java.Today we shall make a program on this concept.The statement of the program is as follows:
“Write a program to keep student information in one package and student marks information in a class and try to access the data in the student information package and finally make a report with the complete information of the student.”
From the above statement we can understand that we need to create a package which all the information of the students.Then we need to use the data in student information package in a class which displays the final report about the students.
Before creating a package we need to create a folder of any desired name.Then we need to place the code of the package class in the folder and compile the package without execution(Because it does not contain main method).Now we shall write the package which contains the roll numbers of the students as the data in it.I’m going to store about five roll numbers of the students.

package mypackage;
public class studentinfo
{
  int[] roll_no = new int[6];
 public studentinfo()
  {
    for(int i=1;i<=5;i++)
      roll_no[i]=i;
   }
 public void display(int j)
  {
    System.out.print("\nStudent roll-no is:" + roll_no[j]);
   }
}

Here the method studentinfo() is a constructor we it calls implicitly when an object for class studentinfo is created.
Now we shall write program to use package.

import mypackage.*;
class studentmarks extends studentinfo
{
 public static void main(String arg[])
  {
    int[] marks = new int[6];
    int k=1;
    for(int i=91;i<=95;i++)
     {
       marks[k]=i;
       k++;
     }
    studentinfo ob = new studentinfo();
    System.out.print("               *****Student details are******");
    for(int j=1;j<=5;j++)
     {
       ob.display(j);
       System.out.print("\nStudent marks is:" + marks[j]+"\n\n");
     }
  }
}

Output:
packages...problem
You can download package and the program.
1.Package.
2.Program.

Posted in Java | Tagged , , , , , , | Leave a comment

Packages in Java with Examples…!!!

Packages….!!!Packages are those class which contains methods without the main method in them.Packages are mainly used to reuse the classes which are already created/used in other program.We can define many number of classes in the same package.Packages are mainly divided into two types.They are
1.Built in packages
2.User defined Packages.
The main use of Packages is:

  1. Classses contain in packages of other programme can easily be used.
  2. In packages,classes can be unique,compare with classes in other packages.
  3. Packages provide the way to hide classes thus preventing other program(or) packages from accessing classes that are meant for internal use only.

Built in Packages:Built Packages are provided for the programme in-order to reduce the burden.Some of the frequently used built in packages are:

  1. lang
  2. util
  3. io
  4. awt
  5. net
  6. applet

These built in packages are placed in the package “java”.In order to use these packages/classes present in the package we need to import them in the current program.The syntax for importing packages is:
import java.util.*
The above statement tells the compiler that you need to import all the classes present in the “util” package,which in turn present in the “java” package.The asterisk symbol tells the compiler to import all the classes in present in the util package.If you want to import specific class then we need to mention the name of the class instead of the asterisk.For eg: import java.util.Scanner
Built in packages:
1.java.lang:language supports classes.These are classes that java compiler itself uses and therefore automatically imported.They include classes for primitive types,stirngs,maths,threads and exceptions.
2.java.util:language utility classes such as vecotrs,hash tables,random numbers,date etc.
3.java.io:input/output support classes.They provide the facility for input/output of data.
4.java.awt:set of classes for implementing graphical user interface.They include classes for windows,buttons,lists
5.java.net:Classes for networking.They include classes for communicating with local computers as well as with internal servers.
Today we shall create a user defined package.For this we need to create a folder with your desired name.

package name_of_folder_given
public class A
 {
   public void display()
    {
      System.ou.println("Hello world");
     }
 }

Now we have created a package.We shall now reuse the above defined class in other program.

import name_of_the_folder_given.*;
class packagedemo
 {
   public static void main(String arg[])
    {
      A ob = new A();
      ob.display();
    }
 }

There fore we have reused the a class defined in other program.Before executing the second program we need to compile the package i.e. javac a.java
Output:
package_demo
you can download the code:
1.download package
2.download program

Posted in Java | Tagged , , , , , , , , , | 1 Comment

Java program to get the IP address

In today’s post we are going to learn how to get the ip address.This is very simple and easy program among all basic programs of java.In this program we are importing the classes “import java.net.InetAddress”,”import java.net.UnknownHostException”.Here we are declaring a variable for InetAddress class. This variable is used to store the ip address. If any Error is occurred then we need to handle those errors.For this we are including the try block and catch block to handle these errors.
Let us write the code for this:

import java.net.InetAddress;
import java.net.UnknownHostException;
public class ip_address
{
  public static void main(String[] args) {
  InetAddress ip;
  try 
   {
     ip = InetAddress.getLocalHost();
     System.out.println("Current IP address : " + ip.getHostAddress());
    } 
  catch(UnknownHostException e)
   {
      System.out.println(e);
    }
 }
}

Output:
ip_address
You can download the source code:download

Posted in Java | Tagged , , , , | Leave a comment

Creating your first app page!

ProgramThat;

I’m going to create the first page using the palette which is on the left hand side of the IDE. 

Image

Before you start it’s best to familiarise yourself with the properties which are in the palette. Find out which layout is best for the app you are creating, as each has their benefits and disadvantages. 

I will take you through the most commonly used ones and how to use them. 

1. First chose what layout you are going to use. In this case i’m going to use a “relative layout”. This will enable me to place textviews and textedits etc freely around the screen. 

2. Placing TextViews. Textviews are using for text which cannot be edited. They can be used to display titles or anything, however the text placed within them should be stored in the strings file. 

You should also update the id of the widget you have inserted…

View original post 92 more words

Posted in Android Programming | Leave a comment

C++ program to print number in pyramid shape

This is once of the basic program to start programming in C++.Here your main goal is to print the number’s in pyramid shape.The size of the shape depends on the input of the user.The output is similar to this picture:

For this we need to write the control statements such as for loop.Here in the program we use the method print_pyramid to print the output.Now we shall write a program to this problem:

#include<iostream.h>
#include<conio.h>
class pyramid
{
  public:
  int n;
  void print_pyramid();
};
void pyramid :: print_pyramid()
{
  for(int i=1;i<=n;i++)
   {
     for(int j=1;j<=i;j++)
     {
       cout<<j<<"  ";
     }
     cout<<"\n";
   }
}
void main()
{
   clrscr();
   cout<<"Enter number  of lines to be printed in pyramid shape:";
   pyramid ob;
   cin>>ob.n;
   ob.print_pyramid();
   getch();
}

Output:
pyramid
you can download the code:download

Posted in C++ | Tagged , , | Leave a comment

Prime number program in C++

Today we shall learn how to write a program to find a number is Prime number or not.Before we start writing a program we should learn the basic definition.A number is said to be prime number if”It is divisible by one and itself only”.Therefore while deciding a prime number we should about the factors of the given number.

To find the factors of the given number we should divide the number.If the remainder is 0 then number is divisible and it is said to be the factor of the given number.Here we shall use the modulous operator(%) to find the remainder.Let us write the code:

#include<iostream.h>
#include<conio.h>
class prime_number
{
  public:
  int n;
  void calculating();
};
void prime_number :: calculating()
{

    int flag=0,count=0;
    for(int i=2;i<n;i++)
     {
       count=0;
       if(n%i==0)
       {
	 count++;
	 flag++;
       }
       if(count !=0)
	cout<<"\nGiven number is divisible by"<<i;
     }
    if(flag ==0)
     cout<<"\n******Given number "<<n<<" is a prime number*******";
    else
    cout<<"\n********There fore given number "<<n<<" is not a prime number*********";
}
void main()
{
  clrscr();
  prime_number ob;
  cout<<"Enter the number to check for prime number:";
  cin>>ob.n;
  ob.calculating();
  getch();
}

Output:
prime_number
You can download the code:download

Posted in C++ | Tagged , , , , , , , , , , , , , , , , , | Leave a comment

Understanding android files and interface

ProgramThat;

Once you have successfully set up IntelliJ, it should then load your project with default files which it sets up. I’m going to explain what they are used for as they are essential to understand before you begin.

 

1. The first file you see if the res (resource). This includes three other sub files which I am going to explain individually.

 

1.1. The drawable-hdpi. This is where all of the images which you are going to place in your project are going to go. It is only necessary to place them in one of the files, I have chosen the highest resolution one. Android does not accept capital letters for the image names, so try to avoid doing that.

 

1.2. The layout folder. This is where all of the pages (activities) for your app are going to go into. You can have as many activities as you like…

View original post 178 more words

Posted in Android Programming | Leave a comment