Biggest of 2 Numbers Using Function: C++

Find the biggest of two numbers using function and ternary operator.

Basics of functions:

A function is a sub program to perform a specific task.
OR
a group of instructions to perform specific task.

Along with main() it is possible to define our own functions by following these steps:
1. Function prototype or Function declaration.
2. Function call.
3. Function definition.

Function Prototype:

Giving information about the function such as return type, function name and arguments type to the compiler is known as function prototype; It is written at the declaration section.
Syntax:

< return_type > < function_name >( arguments_type );

Example:
int findsum(int a, int b);
float findsum(int a, int b);
void findsum(int a, int b);
int findsum(int a[], int size);
int findsum(int, int);

Function Definition:
Writing the actual code of the function in a block. It is at this stage the task of the function is defined. It is written after the main function.
Syntax:

< return_type >< function_name >(parameters)
{
 
}

Example:

int findsum(int a, int b)
{
 int sum;
 
 sum = a + b;
 return(sum);
}

Function Call:
It is a technique used to invoke a function.
Syntax:

[variable] < function_name >([arguments]);

Example:
res = findsum(10, 20);
res = findsum(x, y);
res = findsum();

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 Big(int x,int y);  // Prototype
 
  int a, b;
  clrscr();
 
  cout< <"Enter 2 numbers\n";
  cin>>a>>b;
 
  int res = Big(a, b);   // Function call
 
  cout< <"Biggest = "<<res;
  getch();
}
 
int Big(int x, int y)   // Function Definition
{
  return( x>y?x:y );
}

You must also watch these videos, before continuing with this program:
Find Biggest of 2 Numbers: C++
Biggest of Two Numbers Using Ternary Operator: C++

Video Tutorial: Biggest of 2 Numbers Using Function


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

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



Output:
Enter 2 numbers
420
305
Biggest = 420

(Basic) Find Sum Using Dynamic Memory Allocation: C++

This video tutorial illustrates basics of Dynamic memory allocation in C++. It shows the use of new and delete operator for allocating and deallocating the memory dynamically.

Find the sum of entered elements using dynamic memory allocation in c++.

In cpp, dynamic memory management can be done using the operators new and delete.
Operator new is used to allocate the memory during execution time or run time, the dynamically allocated memory can be freed / released using the operator delete.
Syntax:

< data_type >  < pointer_variable > = new < data_type >[size];

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
#include< iostream .h>
#include< conio .h>
 
void main()
{
  int sum=0, N;
  clrscr();
 
  cout< <"Enter array size\n";
  cin>>N;
 
  int *a = new int[N];
  cout< <"\nEnter "<<N<<" integer numbers"<<endl;
  for(int i=0; i<N; i++)
   cin>>a[i];
 
   cout< <"Input array is.."<<endl;
   for(i=0; i<N; i++)
   {
    cout<<a[i]<<endl;
    sum = sum + a[i]; // sum += a[i];
   }
   cout<<"Total Sum: "<<sum;
 
   delete(a);
   getch();
}

We need not include any extra header file to perform dynamic memory allocation or de-allocation.

int *ptr = new int[N];

here it is mandatory to take pointer variable for dynamic memory allocation.

for de-allocation we use delete operator: delete(ptr);

NOTE:
Student *p = new Student[3];

where Student is a user defined data type, maybe structure or class.

Student *p = new Student; // This is for only 1 student.

Video Tutorial:(Basic) Find Sum Using Dynamic Memory Allocation


[youtube https://www.youtube.com/watch?v=Iqqfa-UjONo]

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



Output:
Enter array size
5
Enter 5 integer numbers
1
2
3
4
5
Input array is..
1
2
3
4
5
Total Sum: 15

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