Found 7346 Articles for C++

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

karthikeya Boyini
Updated on 26-Jun-2020 09:14:20

2K+ Views

We can calculate the execution time of a code snippet by using following syntax −auto start = high_resolution_clock::now(); // Start time // Code snippet auto stop = high_resolution_clock::now(); // Stop time auto duration = duration_cast(stop - start); // DurationThe class high_resolution_clock is defined in “chrono” header file. The function now() is returning a value corresponding to the call’s point in time.A header file is used to record the time taken by that particular code.#include using namespace std::chrono;The following is an example to calculate execution time of a code snippet.Example Live Demo#include #include using namespace std::chrono; using namespace std; ... Read More

How to use enums in C++?

karthikeya Boyini
Updated on 26-Jun-2020 09:17:06

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.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, spade=3, club};The following is an example of ... Read More

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

Samual Sam
Updated on 26-Jun-2020 09:05:26

89 Views

The main() function is a global function. It is used to start the execution of program. Every program should have main(). The command line arguments argc and argv are optional.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 where program runs.argv − pointer to the first element of an array.The following is an example of main()Example Live Demo#include using namespace std; int sum(int x, int y) {    int s = x + y;    cout

Write a power (pow) function using C++

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

1K+ 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

271 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

Samual Sam
Updated on 26-Jun-2020 09:04:47

6K+ Views

The following is an example to find fibonacci series using iteration.Example Live Demo#include using namespace std; void fib(int num) {    int x = 0, y = 1, z = 0;    for (int i = 0; i < num; i++) {       cout

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

karthikeya Boyini
Updated on 26-Jun-2020 08:38:49

972 Views

The following is an example to check whether a number can be expressed as sum of two prime numbers.Example Live Demo#include using namespace std; int func(int num) {    int i;    int flag = 1;    for(i = 2; i num;    for(i = 2; i

C++ Program to Compute Combinations using Factorials

Samual Sam
Updated on 26-Jun-2020 08:39:33

810 Views

The following is an example to compute combinations using factorials.Example Live Demo#include using namespace std; int fact(int n) {    if (n == 0 || n == 1)    return 1;    else    return n * fact(n - 1); } int main() {    int n, r, result;    coutn;    coutr;    result = fact(n) / (fact(r) * fact(n-r));    cout

C++ Program to Find Factorial of Large Numbers

karthikeya Boyini
Updated on 26-Jun-2020 08:40:22

1K+ Views

The following is an example to find the factorial.Example#include using namespace std; int fact(unsigned long long int n) {    if (n == 0 || n == 1)    return 1;    else    return n * fact(n - 1); } int main() {    unsigned long long int n;    coutn;    cout

C++ Program to Find Fibonacci Numbers using Recursion

Samual Sam
Updated on 06-Sep-2023 21:11:54

52K+ Views

The following is an example of fibonacci series using recursion.Example Live Demo#include using namespace std; int fib(int x) {    if((x==1)||(x==0)) {       return(x);    }else {       return(fib(x-1)+fib(x-2));    } } int main() {    int x , i=0;    cout x;    cout

Advertisements