#include < iostream .h >
#include < conio .h >
void main()
{
int a, b;
clrscr();
cout< <"Enter 2 numbers\n";
cin>>a>>b;
int big;
big = a > b ? a : b;
/*
if( a > b )
big = a;
else
big = b;
*/
cout< <"\nBig is: "<<big;
getch();
}
#include < iostream .h >
#include < conio .h >
void main()
{ int a, b; clrscr(); cout< <"Enter 2 numbers\n"; cin>>a>>b; int big; big = a > b ? a : b;
/* if( a > b ) big = a; else big = b;
*/ cout< <"\nBig is: "<<big; getch();
}
In this program we take 2 integer values from the user and using ternary operator decide the biggest of 2 numbers.
big = a > b ? a : b;
big = a > b ? a : b;
if ( a > b ) is true, then value of a will be stored in variable big; else value of b will be stored in big.
Video Tutorial: Biggest of 2 Integer Numbers Using Ternary Operator
Video tutorial to find the biggest of the two integer numbers in C++, using if-else control statements.
Full Source Code
#include < iostream .h >
#include < conio .h ">
void main()
{
int a, b;
clrscr();
cout< <"Enter 2 numbers\n";
cin>>a>>b;
int big;
if( a > b )
big = a;
else
big = b;
cout< < endl << "among " << a << "="" and="" " << b<<"="" biggest= "" is="" " << big;
In this program we get two integer variables from the user. If value of variable a is bigger, then we store it inside the variable big; else we store the value of variable b inside variable big. Finally display the value of variable big which contains the biggest number amongst the two values entered by the user.
This video tutorial illustrates a simple program of adding two integer numbers.
Full Source Code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include<iostream .h>
#include<conio .h>
void main()
{
int a, b;
clrscr();
cout< <"Enter two no\n";
cin>>a>>b;
int sum = a + b;
cout< <endl<<"Sum is: "<<sum;
getch();
}
#include<iostream .h>
#include<conio .h>
void main()
{ int a, b; clrscr(); cout< <"Enter two no\n"; cin>>a>>b; int sum = a + b; cout< <endl<<"Sum is: "<<sum; getch();
}
We can declare variables anywhere in a cpp program.
Full Source code: Colorful Text Output: C program C
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include< stdio.h> /* Leave No space b/w < and stdio.h */
void main()
{
int i;
clrscr();
for( i=0; i<=15; i++ )
{
textcolor(i);
cprintf("Microsoft, Google, Yahoo, Oracle, eBay, PayPal \r\n");
}
getch();
}
#include< stdio.h> /* Leave No space b/w < and stdio.h */void main()
{
int i;
clrscr();
for( i=0; i<=15; i++ )
{
textcolor(i);
cprintf("Microsoft, Google, Yahoo, Oracle, eBay, PayPal \r\n");
}
getch();
}
textcolor() function takes color code as it’s parameter, and this color is taken by cprintf() function and the parameter(string) passed to cprintf is printed in the color given by textcolor() function.
Color Name ———-
Color code ———-
BLACK BLUE GREEN CYAN RED MAGENTA BROWN LIGHTGRAY DARKGRAY LIGHTBLUE LIGHTGREEN LIGHTCYAN LIGHTRED LIGHTMAGENTA YELLOW WHITE