Programs in C++

C++ program using two constructor to calculate minutes and seconds with hour as input from user

Write a class with name Time. Make a constructor which calculates the given time in minutes {if input time is 1hr, then the constructor should return 60minutes. }. Make another constructor which calculates the time in seconds. { if the input time is 1hr, then the constructor should return 60*60=3600 seconds}. Display the contents of both the constructors #include<iostream.h> #include<conio.h> class Time { public: Time(int); Time(float); }; Time::Time(int b) { cout << “Time in seconds: ” << b*60*60 << “n”; } Time::Time(float b) { cout << “Time in Minutes: ” << int(b*60) << “n”; } void main() { clrscr(); int hr; cout << “Enter the time in Hours: “; cin >> hr; Time t1(hr); Time t2(float(hr)); getch(); } Output Enter the time in Hours: 1 Time in seconds: 3600 Time in Minutes: 60

Program in C++ to find weekday based on weekday number

The below program in C++ will find out weekday based on the weekday number input. We will be following the below logic. 1 -> Sunday 2 -> Monday 3 -> Tuesday 4 -> Wednesday 5 -> Thursday 6 -> Friday 7 -> Saturday Here we are accepting the week day number as input from the user and use switch statement to display the weekday. Below is the program in C++. #include<iostream.h> #include #include void main(){ int day; clrscr(); cout << “Enter the day number: “; cin >> day; switch(day){ case 1: cout << “nIts Sunday”; break; case 2: cout << “nIts Monday”; break; case 3: cout << “nIts Tuesday”; break; case 4: cout << “nIts Wednesday”; break; case 5: cout << “nIts Thursday”; break; case 6: cout << “nIts Friday”; break; case 7: cout << “nIts Saturday”; break; default: cout << “nPlease enter number between 1 to 7”; break; }...

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

Understanding the program in C++

In this article we will understand the program written in C++ programming language in step-by-step manner. Consider the below simple C++ program that performs multiplication of two integers and displays the result. /*Program that performs multiplication of two integers and displays the result*/ //Include header files #include <iostream> #include “conio.h” using namespace std; //main program starts from here void main() { //declare variables int result, variable1, variable2; //assign values to variable1 and variable2 variable1 = 10; variable2 = 2; /*Perform multiplication of variable1 and variable2. Store the result in the variable named result*/ result = variable1 * variable2; //Display the result in the output window cout << “The result is: ” << result << endl; } Result of the above program: The result is: 20 Let us understand the above program in detail: Line 1 thru 2: This is a multiline comment. We have used it to describe the purpose...