Reverse Given Number And Check For Palindrome: C++

Cpp program to read a number, reverse the given number and check whether it is palindrome or not.

Full Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include< iostream .h>
#include< conio .h>
 
void main()
{
  int num, rem, sum = 0, temp;
  clrscr();
 
  cout< <"Enter a number\n";
  cin>>num;
 
  temp = num;
 
  while( num )
  {
    rem = num % 10;
    num = num / 10;
    sum = sum * 10 + rem;
  }
 
cout< <"\nReversed Number: "<<sum<<endl;
 
  if( temp == sum )
   cout<<temp<<" is a palindrome";
  else
   cout<<temp<<" in NOT a palindrome";
 
  getch();
}

In this program we assign variable sum = 0 to avoid garbage values in sum before the calculation, which would result in wrong output.
We store the user entered value in temp, so that we can compare it with the final result, to determine whether the given number is palindrome or not.

1
2
3
4
5
6
 while( num )
  {
    rem = num % 10;
    num = num / 10;
    sum = sum * 10 + rem;
  }

Here the loop executes until the value of num is zero.

If user enters 301, we apply the modulus to get the individual values.
Ex:
301 % 10 = 1
30 % 10 = 0
3 % 10 = 3

We get 301, 30 and 3 by dividing the original value by 10.
Ex:
301 user entered value.
301 / 10 = 30
30 / 10 = 3

Now the sum.

sum = sum * 10 + rem;

1 = 0 * 10 + 1
10 = 1 * 10 + 0
103 = 10 * 10 + 3

So the reverse of 301 is 103, which is not a palindrome.

Video Tutorial: Reverse Given Number And Check For Palindrome: C++


[youtube https://www.youtube.com/watch?v=j8Vv4wgdwoA]

YouTube Link: https://www.youtube.com/watch?v=j8Vv4wgdwoA [Watch the Video In Full Screen.]



Output:
Enter a number
301
Reversed Number: 103
301 in NOT a palindrome

Array Basics in C++ : Find Sum

Video tutorial to show the basic use of arrays: Initialization, Declaration, Getting values from the users, finding sum of all the array elements etc.

Array is a collection of homogeneous data items.

Full Source Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include<iostream .h>
#include<conio .h>
 
void main()
{
  //  int a[5] = { 1, 3, 2, 4, 5 }; declaration with initialization
 
  int a[5], sum = 0;
  clrscr();
 
  cout< <"Enter 5 numbers\n";
  for(int i=0; i&lt;5; i++)
   cin>>a[i];
 
  cout< <"Input array is..\n";
  for(i=0; i&lt;5; i++)
  {
   cout<<a[i]<<endl;
   sum = sum + a[i];  // sum += a[i];
  }
  cout<<"Sum of array elements: "<<sum;
 
  getch();
 
}

In this program we take input from the user and display the user entered numbers and find the sum of all the array elements and display it as well.

Elements are stored from the index number 0. i.e., if the array size is 5, the values will be stored in a[0], a[1], a[2], a[3], a[4];

Ex:
int a[5] = { 1, 3, 2, 4, 5 }; declaration with initialization
float a[5] = { 1.1, 2.0, 3.3, 1.3, 5.6 };

Video Tutorial: Array Basics in C++ : Find Sum


[youtube https://www.youtube.com/watch?v=BfAa2koJv64]

YouTube Link: https://www.youtube.com/watch?v=BfAa2koJv64 [Watch the Video In Full Screen.]



Output:
Enter 5 numbers
1
2
3
4
5
Input array is..
1
2
3
4
5
Sum of array elements: 15

Find the Factorial of a Number: C++

Video tutorial to find the factorial of a number: if the user enters 3, then the factorial is 1 * 2 * 3 i.e., factorial = 6
This logic must be handled with a c++ program.

Full Source Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include<iostream .h>
#include<conio .h>
 
void main()
{
  int fact = 1, N;
  clrscr();
 
  cout< <endl<<"Enter a number\n";
  cin>>N;
 
  for( int i=1; i< =N; i++ )
   fact = fact * i;  // OR fact *= i;
 
  cout<<endl<<endl<<"Factorial of "<<N<<" is "<<fact;
 
  getch();
}

Here the for loop stars from 1 and not 0. As anything multiplied by 0 would also be zero.

factorial-cpp


fact *= i; is the compact representation of fact = fact * i;

Video Tutorial: Factorial in cpp


[youtube https://www.youtube.com/watch?v=ovrdIQZ6Hu8]

YouTube Link: https://www.youtube.com/watch?v=ovrdIQZ6Hu8 [Watch the Video In Full Screen.]



Output:
Enter a number
5
Factorial of 5 is 120

Biggest of 3 Numbers Using Ternary Operator: C++

Video tutorial to find the biggest of the three integer numbers in C++, using if-else control statements and Ternary Operator.

Before watching this video, please make sure to watch and understand this short video: Find Biggest of 3 Numbers: C++

Full Source Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include<iostream .h>
#include<conio .h>
 
void main()
{
int a, b, c;
clrscr();
 
cout< <"Enter 3 no's\n";
cin>>a>>b>>c;
 
int big =  ( a>b && a>c )?a:(b>c?b:c);
/*
if( a > b && a > c )
  big = a;
else if( b > c )
  big = b;
else
  big = c;
*/cout< <"\nAmong "<<a<<" , "<<b<<" , "<<c<<" Biggest is "<<big;
getch();
}

In this program we take 3 integer values from the user and using ternary operator decide the biggest of 3 numbers.

int big =  ( a>b && a>c )?a:(b>c?b:c);

if ( a > b && a > c ) is true, then value of a will be stored in variable big; else ( b > c ? b : c ) will be evaluated. Here, if value of b is greater than c, value of b will be stored in variable big else value of c will be stored in the variable big.

Video Tutorial: Biggest of 3 Integer Numbers


[youtube https://www.youtube.com/watch?v=gfmqT4PwfY0]

YouTube Link: https://www.youtube.com/watch?v=gfmqT4PwfY0 [Watch the Video In Full Screen.]



Output
Enter 3 no’s
10
20
30
Among 10, 20, 30 Biggest is 30

Find Biggest of 3 Numbers: C++

Video tutorial to find the biggest of the three integer numbers in C++, using if-else control statements.

Full Source Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include<iostream .h>
#include<conio .h>
 
void main()
{
int a, b, c;
clrscr();
 
cout< <"Enter 3 no\n";
cin>>a>>b>>c;
 
int big;
 
if( a > b )
 big = a;
else
{
 if( b > c )
  big = b;
 else
  big = c;
}
cout< <"\nBiggest is: "<<big;
getch();
}

In this program we get three integer variables from the user.
If value of variable a is bigger, then we store it inside the variable big; else we compare b with c. Now will store the biggest number inside the variable big.
Video Tutorial: Biggest of 3 Integer Numbers


[youtube https://www.youtube.com/watch?v=YRkCpS_GMFw]

YouTube Link: https://www.youtube.com/watch?v=YRkCpS_GMFw [Watch the Video In Full Screen.]



Output
Enter 3 no’s
404
501
301
Biggest is: 501