C++ Articles

Page 286 of 597

C++ Program to Multiply two Numbers

Samual Sam
Samual Sam
Updated on 11-Mar-2026 3K+ Views

Multiplication of two numbers a and b yields their product. Value of a is added as many times as the value of b to get the product of a and b.For example.5 * 4 = 20 7 * 8 = 56 9 * 9 = 81Program to Multiply two Numbers using * OperatorA program to multiply two numbers using the * operator is given as follows −Example#include using namespace std; int main() {    int a = 6, b = 8;    cout

Read More

C++ Program to Find ASCII Value of a Character

karthikeya Boyini
karthikeya Boyini
Updated on 11-Mar-2026 10K+ Views

There are 128 characters in the ASCII (American Standard Code for Information Interchange) table with values ranging from 0 to 127.Some of the ASCII values of different characters are as follows −CharacterASCII ValueA65a97Z90z122$36&38?63A program that finds the ASCII value of a character is given as follows −Example#include using namespace std; void printASCII(char c) {    int i = c;    cout

Read More

What does '?' do in C/C++?

Arjun Thakur
Arjun Thakur
Updated on 11-Mar-2026 2K+ Views

The operator ‘?’ is known as ternary operator as it requires three operands to act upon. It can be represented by “ ? : ”. It is also known as conditional operator. The operator improves the performance and reduces the lines of code.Here is the syntax of ternary operator in C language, Expression1 ? Expression2 : Expression3Here is an example of Ternary Operator in C language, Example#include int main() {    int a = -1;    double b = 26.4231;    int c = a? printf("True value : %lf", b):printf("False value : 0");    return 0; }OutputHere is ...

Read More

Why are global and static variables initialized to their default values in C/C++?

karthikeya Boyini
karthikeya Boyini
Updated on 11-Mar-2026 3K+ Views

Global and static variables are initialized to their default values because it is in the C or C++ standards and it is free to assign a value by zero at compile time. Both static and global variable behave same to the generated object code. These variables are allocated in .bss file and at the time of loading it allocates the memory by getting the constants alloted to the variables.The following is an example of global and static variables.Example#include int a; static int b; int main() {    int x;    static int y;    int z = 28;   ...

Read More

Calling class method through NULL class pointer in C++

Arjun Thakur
Arjun Thakur
Updated on 11-Mar-2026 534 Views

A class method can be can be called using a NULL class pointer.Note − This is undefined behaviour and there is not guarantee about the execution of the program. The actual results depend on the compiler used.A program that demonstrates this is given as follows.Example#include using namespace std; class Example {    public :    void func() {       cout func();    return 0; }OutputThe output of the above program is as follows.The function is called through Null class pointer.Now, let us understand the above program.The class Example contains a member function func(). This function displays "The ...

Read More

C++ Program to Subtract Complex Number using Operator Overloading

Samual Sam
Samual Sam
Updated on 11-Mar-2026 3K+ Views

Operator overloading can be done with most of the built-in operators in C++. The overloaded operators are functions with the keyword operator followed by the operator symbol that is defined. The overloaded operators have a return type and a parameter list like any function.A program that subtracts complex numbers using operator overloading is as follows −Example#include using namespace std; class ComplexNum {    private:    int real, imag;    public:    ComplexNum(int r = 0, int i =0) {       real = r;       imag = i;    }    ComplexNum operator - (ComplexNum const &obj1) ...

Read More

C++ Program to Implement Sparse Matrix

Ankith Reddy
Ankith Reddy
Updated on 11-Mar-2026 10K+ Views

A sparse matrix is a matrix in which majority of the elements are 0. An example for this is given as follows.The matrix given below contains 5 zeroes. Since the number of zeroes is more than half the elements of the matrix, it is a sparse matrix.5 0 0 3 0 1 0 0 9A program to implement a sparse matrix is as follows.Example#include using namespace std; int main () {    int a[10][10] = { {0, 0, 9} , {5, 0, 8} , {7, 0, 0} };    int i, j, count = 0;    int row = 3, col = 3;    for (i = 0; i < row; ++i) {       for (j = 0; j < col; ++j){          if (a[i][j] == 0)          count++;       }    }    cout

Read More

C++ Program to Find the Number of Vowels, Consonants, Digits and White Spaces in a String

karthikeya Boyini
karthikeya Boyini
Updated on 11-Mar-2026 2K+ Views

A string is a one dimensional character array that is terminated by a null character. There can be many vowels, consonants, digits and white spaces in a string.For example.String: There are 7 colours in the rainbow Vowels: 12 Consonants: 15 Digits: 1 White spaces: 6A program to find the number of vowels, consonants, digits and white spaces in a string is given as follows.Example#include using namespace std; int main() {    char str[] = {"Abracadabra 123"};    int vowels, consonants, digits, spaces;    vowels = consonants = digits = spaces = 0;    for(int i = 0; str[i]!='\0'; ++i) ...

Read More

How to get last 2 characters from string in C# using Regex?

Sreemaha
Sreemaha
Updated on 11-Mar-2026 1K+ Views

Set the string −string str = "Cookie and Session";Use the following Regex to get the last 2 characters from string −Regex.Match(str,@"(.{2})\s*$")The following is the code −Exampleusing System; using System.Text.RegularExpressions; public class Demo {    public static void Main() {       string str = "Cookie and Session";       Console.WriteLine(Regex.Match(str,@"(.{2})\s*$"));    } }Outputon

Read More

Difference between static, auto, global and local variable in C++

George John
George John
Updated on 11-Mar-2026 4K+ Views

There are two separate concepts here − scope, which determines where a name can be accessed - global and local storage duration, which determines when a variable is created and destroyed - static and auto Scope Local variables can be used only by statements that are inside that function or block of code. Local variables are not known to functions on their own. Example #include using namespace std; int main () { // Local variable declaration: int a, b; int c; // actual initialization a = 10; b = 20; c = a + b; cout

Read More
Showing 2851–2860 of 5,961 articles
« Prev 1 284 285 286 287 288 597 Next »
Advertisements