Found 7197 Articles for C++

Swapping two variable value without using third variable in C/C++

Samual Sam
Updated on 26-Jun-2020 09:12:20

540 Views

The following is an example of swapping two variables.Example Live Demo#include int main() {    int a, b;    printf("Enter the value of a : ");    scanf("%d", &a);    printf("Enter the value of b : ");    scanf("%d", &b);    a += b -= a = b - a;    printf("After Swapping : %d\t%d", a, b);    return 0; }OutputEnter the value of a : 23 Enter the value of b : 43 After Swapping : 4323In the above program, two variables a and b are declared and initialized dynamically at run time.int a, b; printf("Enter the value of ... Read More

Variable initialization in C++

Tapas Kumar Ghosh
Updated on 22-Apr-2025 15:17:33

9K+ Views

In C++, variables are name given by the user to store data. While datatype is used to declare and initialize a variable that allocates memory to that variable. There are various ways to initialize the data types such as int, char, float, etc. to allocate the memory to that variable. Syntax Following is the syntax of variable initialization in C++: datatype variable_name = value; Here, datatype : The datatype of variables like int, char, float, etc. variable_name : This is the name of variable given by user. value : Any value to initialize the variable. By default, it ... Read More

How to catch a divide by zero error in C++?

Samual Sam
Updated on 26-Jun-2020 09:13:25

734 Views

The following is an example to catch a divide by zero error.Example Live Demo#include using namespace std; int display(int x, int y) {    if( y == 0 ) {       throw "Division by zero condition!";    }    return (x/y); } int main () {    int a = 50;    int b = 0;    int c = 0;    try {       c = display(a, b);       cout

How to calculate Execution Time of a Code Snippet in C++?

Tapas Kumar Ghosh
Updated on 02-May-2025 18:01:54

2K+ Views

To calculate the execution time of a program, use the std::chrono library, which was introduced in C++11. This library has two distinct objects: time_point and duration. The time_point represents the variable names that store the start and end times of specific algorithms, functions, or loops used in the program. While duration represents the interval between two different times. The chrono library allows us to subtract these time intervals. Here, we use the high_resolution_clock::now() function, which provides accurate units of time. Calculate the Execution Time of a C++ Program To get the execution time of a program use the chrono header ... Read More

How to use enums in C++?

karthikeya Boyini
Updated on 02-Dec-2024 00:23:27

10K+ Views

Enumeration is a user defined datatype in C/C++ language. It is used to assign names to the integral constants which makes a program easy to read and maintain. The keyword “enum” is used to declare an enumeration. The following is the syntax of enums. Syntax enum enum_name{const1, const2, ....... }; Here, enum_name − Any name given by user. const1, const2 − These are values of type flag. The enum keyword is also used to define the variables of enum type. There are two ways to define the variables of enum type as follows. enum colors{red, black}; enum suit{heart, diamond=8, ... Read More

What is the proper declaration of main in C++?

Tapas Kumar Ghosh
Updated on 21-Apr-2025 18:39:38

144 Views

The main() function is the entry point of every C++ program where execution begins. It is invoked automatically when the program is executed. The main() function returns the execution status to the operating system (indicating whether the program executed successfully or not). You can also use optional command-line arguments, argc and argv, to pass values to the program. Declaration /Prototype of main() The standard prototype of main() function is as follows: int main() { body } Or, int main(int argc, char *argv[]) { body } Here, argc : Number of arguments passed to the program from the environment ... Read More

Write a power (pow) function using C++

Samual Sam
Updated on 26-Jun-2020 09:02:50

2K+ Views

The power function is used to find the power given two numbers that are the base and exponent. The result is the base raised to the power of the exponent.An example that demonstrates this is as follows −Base = 2 Exponent = 5 2^5 = 32 Hence, 2 raised to the power 5 is 32.A program that demonstrates the power function in C++ is given as follows −Example Live Demo#include using namespace std; int main(){    int x, y, ans = 1;    cout > x;    cout > y;    for(int i=0; i

The static keyword and its various uses in C++

karthikeya Boyini
Updated on 26-Jun-2020 09:03:47

395 Views

When static keyword is used, variable or data members or functions can not be modified again. It is allocated for the lifetime of program. Static functions can be called directly by using class name.Static variables are initialized only once. Compiler persist the variable till the end of the program. Static variable can be defined inside or outside the function. They are local to the block. The default value of static variable is zero. The static variables are alive till the execution of the program.The following is the syntax of static keyword.static datatype variable_name = value; // Static variable    static ... Read More

C++ Program to Find Fibonacci Numbers using Iteration

Tapas Kumar Ghosh
Updated on 21-Apr-2025 18:06:34

8K+ Views

The term Fibonacci numbers is used in mathematics, which calculates the sum of two preceding numbers that starts from 0 and 1 and continues till based on the given number say num. Mathemathematically, it is represented by: Fn = Fn-1 + Fn-2 Let us take an example to understand the above formula based on fibonacci numbers. F0 = 0 F1 = 1 F2 = F1 + F0 = 1 + 0 = 1 F3 = F2 + F1 = 1 + 1 = 2 F4 = F3 + F2 = 2 + 1 = 3 ... Read More

C++ Program to Check Whether a Number can be Expressed as Sum of Two Prime Numbers

Tapas Kumar Ghosh
Updated on 09-Jun-2025 11:28:29

1K+ Views

You are given a number and you need to write a C++ program to check whether a number can be expressed as sum of two prime numbers. Input / Output Scenarios Following is the input-output statement: Input: n = 19 Output: 19 can be expressed as the sum of two prime numbers. Explanation: 19 = 17 + 2, and both 17 and 2 are prime numbers. Input: n = 36 Output: 36 can be expressed as the sum of two prime numbers. Explanation: 36 = 31 + 5, and both 31 and 5 are prime numbers. C++ Program ... Read More

Advertisements