Sunday, June 1, 2014

C++ Simple pointer program to print address and value

#include<iostream>
#include<conio.h>
using namespace std;
int main ()
{
     int a= 5;
 /* actual variable declaration */
 int *ptr;
 /* pointer variable declaration */
  ptr = &a;
   /* store address of a in pointer variable*/
   cout<<"Address of variable a="<< &a;
    cout<<"\nAddress stored in variable ptr="<<ptr;
     cout<<"\nValue of *ptr variable="<<*ptr;
     getch();
     return 0;
      }


C++ Program to print largest number by if else

#include<iostream>
#include<conio.h>
using namespace std;
int main()
{

int a,b;//variable declaration
cout<<"Enter two numbers:";//prompt
cin>>a>>b;
if(a==b)
cout<<a<<"\n Both numbers are equal";
if(a>b)
cout<<a<<"\n is larger";
else
cout<<b<<"\n is larger";
getch();
}


C++ Program to find largest number using function

#include <iostream>
#include <conio.h>
using namespace std;
int max(int , int); // function declaration
int main ()
{
int x; // local variable declaration:
int y;
int ans;
cout<<"Enter the first value:";
cin>>x;
cout<<"Enter the second value:";
cin>>y;
ans = max(x, y); // calling a function to get max value.
cout << "Max value is : " << ans << endl;
getchar();
return 0;
}
int max(int num1, int num2) // function definition
{
int result; // local variable declaration
if (num1 > num2)
result = num1;
else
result = num2;
getchar();
return result;
}



Saturday, May 31, 2014

C++ Program which displays the radius ,area and color of the circle using constructor

#include <iostream>   
#include<conio.h>
#include <string>      // using string
using namespace std;

class Circle {
private:
double radius;      // Data member (Variable)
string color;       // Data member (Variable)

public:
   // Constructor with default values for data members
Circle(double r , string c ) {
radius = r;
color = c;
   }

double getRadius() {  // Member function (Getter)
return radius;
   }

string getColor() {   // Member function (Getter)
return color;
   }

double getArea() {    // Member function
return radius*radius*3.1416;
   }
};   // need to end the class declaration with a semi-colon
 // Test driver function
int main() {
   // Construct a Circle instance
   Circle c1(1.2, "blue");
cout<< "Radius=" << c1.getRadius() << " Area=" << c1.getArea()
<< " Color=" <<c1.getColor() <<endl;

   // Construct another Circle instance
   Circle c2(3.4,"Red"); // default color
cout<< "Radius=" << c2.getRadius() << " Area=" << c2.getArea()
<< " Color=" <<c2.getColor() <<endl;

   // Construct a Circle instance using default no-arg constructor
   Circle c3(5,"Yellow");      // default radius and color
cout<< "Radius=" << c3.getRadius() << " Area=" << c3.getArea()
<< " Color=" <<c3.getColor() <<endl;
getch();
return 0;
}










First Java Program

public class HelloWorld {
   public static void main(String[] args) {
      System.out.println("Hello, World");
   }
}

To compile and run java program open command prompt. Go to the location where the java file is saved. To complie type javac and name of java program. To run java program type java and name of file. In below picture "FirstJava" file is compiled and run by cmd prompt.

C++ Program to print week day according to number by getting number from user

#include<conio.h>
#include<iostream.h>
main()
{
      int num;
      clrscr();
      cout<<"Enter any number between 1 to 7: ";                            
      cin>>num;
      switch(num)
      {
      case 1:
                   cout<<"Monday";
                   break;
      case 2:
                   cout<<"Tuesday";
                   break;
      case 3:
                   cout<<"Wednesday";
                   break;
      case 4:
                   cout<<"Thursday";
                   break;
      case 5:
                   cout<<"Friday";
                   break;
      case 6:
                   cout<<"Saturday";
                   break;
      case 7:
                   cout<<"Sunday";
                   break;
      default:
                   cout<<"INVALID NUMBER!!! Please enter number between 1 to 7";
      }
      getch();
      }






Friday, May 30, 2014

Basic Sytanx of C

Some basic commands you should know while writing program in C .
Let write a C program which prints a line of text.

#include<stdio.h>
int main()
{
printf("This is my first C program");
return 0;
}

stdio.h

While writing program in C you should start with #include<stdio.h>.
This is header file, "stdio" means standard input output header. 

Printf

In C language "printf" command is used to display any message on screen.

scanf

In C language scanf is used to accept the user input from the keyboard. The command of scanf is :
scanf("%d",&base).
Let us take an example. We write a program that will calculate area of triangle and display the answer.
#include<stdio.h>
int main()
{
float area,base,height;
printf("n\Program to calculate the area of triangle");
printf("n\nPlease enter the value of base :")
scanf("%f",&base);
printf("Please enter the value of Height:")
scanf("%f",&height);
area=0.5*base*height;
printf("\n\nHeight=%f",height);
printf("\nBase=%f",base);
printf("\n0.5*Height*Base=Area");
printf("\n0.5*%f*%f=%f\n",height,base,area)'
return 0;
}

"%d" is used for integer data type.
"%f" is used for float data type.
"%c" is used for character data type.
"%s" is used for string data type.

Some other commands to remember

\n

\n is new line character. So whenever you want a newline you have to add a \n.

\t

\(horizontal tab) Moves the active position to the next horizontal tabulation position on the current line.

\a

is alert/bell