Sunday, June 1, 2014
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;
}
#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;
}
#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 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();
}
#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 .
"%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.
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
\t (horizontal tab) Moves the active position to the next horizontal tabulation position on the current line.\a
is alert/bell
How TO Install JDK on Windows
- Download the latest version of the *Java JDK from Sun Microsystems.
-
Double-click on the install file and it should open an installer (Fig. 1).
-
3Click next, then read and accept the license.
-
On the next screen you will encounter some options. Just leave these alone and click next unless you know what you are doing. (Because this is being read it is assumed that you do not.)(Fig. 2)
-
The next page you encounter should install (and in some cases download) the Java Development Kit. (Fig. 3)
-
After the installer is finished, open run by clicking Start > Run... or by typing Windows Key + R.
-
In the text box, type "cmd" and click "OK".
-
A simple window should be opened with a black background and a text prompt. This is called the "Command Prompt." My command prompt background is red, but it could really be any color. (Fig. 4)
-
After focusing the window, type "javac" and press enter. If the prompt returns something along the lines of: "'javac' is not recognized as an internal or external command, operable program or batch file" then continue with the next step. If it shows many more options and lines, skip to step 15.
-
Open the properties of "My Computer" by either right-clicking the icon on the desktop or right-clicking Start > My Computer. When the pop up menu opens, scroll to the bottom and select "Properties".
-
This should open a window named "System Properties". Click on the "Advanced" tab and then click "Environment Variables". (Fig. 5)
-
Next, another window opens with a lot of confusing sentences and letters. Double-click on the "Path" variable on either of the option boxes. It is recommended to edit the variable in the box "User variables for (your username)".
-
Once the variable is opened, a text box in yet another window appears. Careful not to delete anything in this box. At the end of the text box, add a semi-colon if there is not one already, and add "C:\Program Files\Java\jdk1.6.0\bin" to the text box. This is assuming you did not change the file path of the installation.
-
Click "Apply" and "OK" to all the windows you have just opened. Open the command prompt again, while following steps 6-9 to see if that "javac" command works.
-
Congratulations! You have succesfully installed JDK on your system.
Subscribe to:
Comments (Atom)