C++ Programming Blog

Program in C++ to find sum of first “n” natural numbers

The below program will accept the number “n” from the user and calculates the sum of first “n” natural numbers. For example, if the input provided by the user is 5 then it will calculate the sum of first 5 natural numbers i.e., 1+2+3+4+5 /* Program Name: Program in C++ to find sum of first “n” natural numbers Author: LearnCPPOnline.com */ #include #include “conio.h” using namespace std; void main() { int sum, digit, n; sum = 0; digit = 1; cout << “Enter a number n: “; cin >> n; while(digit <= n) { sum = sum + digit; digit++; } cout << “Sum of first ” << n << ” natural numbers: ” << sum << endl; getch(); } Output Enter a number n: 10 Sum of first 10 natural numbers: 55 Note: The above program is written and tested using Microsoft Visual Studio 2008

Program in C++ to display the name of the day in a week

Program in C++ to display the name of the day in a week, depending upon the number entered through the keyboard. The program will accept integer value between 1 and 7. Depending on the number entered, the program will display the name of the day as follows: 1 => “Monday” 2 => “Tuesday” 3 => “Wednesday” 4 => “Thursday” 5 => “Friday” 6 => “Saturday” 7 => “Sunday” /* Program Name: Program in C++ to display the name of the day in a week, depending upon the numberentered through the keyboard Author: LearnCPPOnline.com */ #include <iostream> #include “conio.h” using namespace std; void main() { int day; cout << “Enter a number between 1 to 7: “; cin >> day; switch(day) { case 1: cout << “Monday” << endl; break; case 2: cout << “Tuesday” << endl; break; case 3: cout << “Wednesday” << endl; break; case 4: cout << “Thursday”...

Program in C++ to read three numbers and compute their average

The below program will accept three numbers from the user, compute their average and display it. #include <iostream> #include “conio.h” using namespace std; void main() { int m1, m2, m3; float avg; cout << “Enter 3 numbers” << endl; cin >> m1 >> m2 >> m3; avg = (m1 + m2 + m3) / 3; cout << “Average = ” << avg << endl; getch(); } Output: Enter 3 numbers 10 20 30 Average = 20 Note: The above program is written and tested using Microsoft Visual Studio 2008

Pointers to functions in C++

In C++, just as we can have pointers to variables, we can have pointers to functions as well. The general syntax of a pointers to a functions is: return-type (*pointer-name) (list of parameter); For example: void (*p) (float, int); In the above declaration, a pointer to a function returns void and takes the two formal arguments of float and int types respectively. After declaring a pointer to a function, the address of the function must be assigned to a pointer as follows: p = &function-name; where p is a pointer variable. Through pointer to function, the function is called as follows: A = (*p) (x, y); where, x and y are actual parameters, and A is a variable in which the function’s return value is stored, p is a pointer name which points to the function Let us illustrate pointer to function with a programming example as follows: #include<iostream.h> void...

Void Pointers in C++

For understanding the concept of void pointers, we should note that we cannot assign the address of one data-type variable to a pointer of another data-type variable. Consider the following: Note that we can not assign the address of float type variable to a pointer to int as: float y; int *p; p = &y; //illegal statement Similarly, see the following: float *p; int x; p = &x; //illegal statement That means, if variable-type and pointer-to-type is same then only we can assign the address of that variable to pointer variable. If both, i.e., the data-type of the variable and the data-type pointed to by the pointer variable, are different then we can not assign the address of variable to pointer variable. But this impossible thing is made possible in C++ by declaring pointer variables as void as follows: void *p; The above pointer is called pointer to void type....

Accessing of the Variables pointed to by the Pointer

Accessing of the variable pointed to by the pointer needs to be understood from two angles, i.e., Accessing of normal basic data-type variables Accessing of class-type objects Accessing of normal basic data-type variables: Let us straight-away learn the accessing of normal basic data-type (line int, float, etc.) variables by seeing a simple example as follows: int *p; int x; x = 20; p = &x; //read as assign address of x (&x) to pointer variable p cout << *p; //read as print content at pointer variable p The statement: cout << *p; prints the value 20 because *p means the value at the address p. Similarly: *p = *p + 10 is same as: x = x + 10; Note that if we write the following statement: cout << p; this prints the address of x. Similarly the statement: cout << &x; also prints the addresss of x. Accessing the...