Powered by Blogger.
  • Home
  • C++ Tutorials
  • Cpp Programs
  • Privacy Policy
  • About
  • Contact
facebook instagram Email

Programming Vital - Learn to Code for Free

Simple Calculator in C++

Calculator

An electronic calculator is typically a portable electronic device used to perform calculations, ranging from basic arithmetic to complex mathematics. Calculators are widely used device nowadays. Calculators are used to everyone in daily life. A simple calculator can be made using a C++ program which can perform addition, subtraction, multiplication division and power Operation. It takes two operands entered by the user. The switch and break statements are used to create a calculator.

Logic to make Simple Calculator Program in C++

  1. Input numbers along with operator
  2. Use switch statement to check for operator and execute respective case.
  3. Store Result in a variable and display it.

Write a program to make simple calculator in C++ which add, subtract, multiply, divide or take power.


// C++ Program to make simple calculator
#include <iostream>
#include <conio.h>
using namespace std;
int main() {
                int a,b,sum,i,sum1,P;
                char op;
                cout << "Calculator (+,-,*,/,^)"<<endl;
                cout<<"Enter Expression: ";
                cin>>a>>op>>b;

                switch(op) {
                                case'+':
                                                sum=a+b;
                                                break;
                                case'-':
                                                sum=a-b;
                                                break;
                                case'*':
                                                sum=a*b;
                                                break;
                                case'/':
                                                sum=a/b;
                                                break;
                                case'^':
                                                P=a;
                                                for(i=1; i<b; i++) {

                                                                sum=(a*P);
                                                                P=P*a;
                                                }
                                                break;
                                default:
                                                break;
                }
                cout<<"Answer is: "<<sum<<endl;
                _getch();
}


Output
Calculator (+,-,*,/,^)
Enter Expression: 2*3
Answer is: 6
Calculator (+,-,*,/,^)
Enter Expression: 2^3
Answer is: 8
Calculator (+,-,*,/,^)
Enter Expression: 4/2
Answer is: 2

Share
Tweet
Pin
Share
No comments
C++ Program to Perform Matrix Multiplication using Multidimensional Arrays


Matrix Multiplication

In mathematics, matrix multiplication or matrix product is a binary operation that produces a matrix from two matrices. Two matrices are multipliable if number of columns of 1st matrix is equal to the number of rows of 2nd matrix. Learn How to Multiply two matrices

C++ Program to Perform Matrix Multiplication using Multidimensional Arrays. The program multiplies two matrices and prints the result matrix. The program takes two matrices and multiplies them. If number of columns of matrix A is equal to number of rows of matrix B, then these matrices can be Multiply.

Logic to Multiply two matrices

  1. Ask user to enter rows and columns for both matrices and make two multidimensional arrays
  2. Ask user to input their fields and store them in multidimensional arrays
  3. Display both matrices
  4. Multiply them and display the resultant matrix 

Write a C++ Program to Perform Matrix Multiplication using Multidimensional Arrays.


// C++ Program to Perform Matrix Multiplication using Multidimensional Arrays
#include<iostream>
#include<conio.h>
using namespace std;
int main() {
                int row1, col1, row2, col2;
                cout << "Enter Number of Rows and Colums of 1st Matrix: ";
                cin >> row1 >> col1;
                int arr1[row1][col1]= {0};
                cout << "Enter Number of Rows and Colums of 2nd Matrix: ";
                cin >> row2 >> col2;
                cout << endl;
                int arr2[row2][col2]= {0};
                if(col1==row2) {
                                for(int i=0; i<row1; i++) {
                                                for(int j=0; j<col1; j++) {
                                                                cout<<"Enter A"<<i+1<<j+1<<": ";
                                                                cin>>arr1[i][j];
                                                }
                                }
                                cout<<"\n1st Matrix: "<<endl;
                                for(int i=0; i<row1; i++) {
                                                for(int j=0; j<col1; j++) {
                                                                cout<<arr1[i][j]<<"\t";
                                                }
                                                cout<<endl;
                                }
                                cout << endl;
                                for(int i=0; i<row2; i++) {
                                                for(int j=0; j<col2; j++) {
                                                                cout<<"Enter B"<<i+1<<j+1<<": ";
                                                                cin>>arr2[i][j];
                                                }
                                }
                                cout<<"\n2nd Matrix: "<<endl;
                                for(int i=0; i<row2; i++) {
                                                for(int j=0; j<col2; j++) {
                                                                cout<<arr2[i][j]<<"\t";
                                                }
                                                cout<<endl;
                                }
                                int arr3[row1][10]= {0};

                                for(int i=0; i<row1; i++) {
                                                for(int j=0; j<col2; j++) {
                                                                for(int k=0; k<col1; k++) {
                                                                                arr3[i][j]+=arr1[i][k]*arr2[k][j];
                                                                }
                                                }
                                }
                                cout<<"\nAfter Multiplication: "<<endl;
                                for(int i=0; i<row1; i++) {
                                                for(int j=0; j<col2; j++) {
                                                                cout<<arr3[i][j]<<"\t";
                                                }
                                                cout<<endl;

                                }
                } else {
                                cout<<"\n\nThese Matrices Cannot Be Multiplied......"<<endl;
                                cout<<"\t\tEnter Again\n\n"<<endl;
                                main();
                }
}

Output

Enter Number of Rows and Colums of 1st Matrix: 2 3
Enter Number of Rows and Colums of 2nd Matrix: 3 2

Enter A11: 1
Enter A12: 2
Enter A13: 3
Enter A21: 4
Enter A22: 5
Enter A23: 6

1st Matrix:
1       2       3
4       5       6

Enter B11: 6
Enter B12: 5
Enter B21: 4
Enter B22: 3
Enter B31: 2
Enter B32: 1

2nd Matrix:
6       5
4       3
2       1

After Multiplication:
20      14
56      41


Share
Tweet
Pin
Share
No comments

Armstrong Number

Armstrong number is a number that is equal to the sum of the cubes of its digits. For example, 0, 1, 153, 370, 371 and 407 are the Armstrong numbers. Let's try to understand why 153 is an Armstrong number. Let’s take a look at example.

Logic to check Armstrong Number

  1. Count number of digits in a number (entered by user)
  2. Make a new number which is equal to the sum of the cube of its digits.
  3. Check if the previous number is equal to the new number then it is Armstrong Number.

Write a program to check that the number (entered by user) is Armstrong number or not?


// C++ Program to check Armstrong Number
#include <iostream>
#include <conio.h>
#include <math.h>
using namespace std;
int main() {
                int num,copy,n=0,rem,ans=0;
                cout<<"Enter Any Number: ";
                cin>>num;
                copy=num;
                // while loop to count number of digits
                while(copy!=0) {
                                copy/=10;
                                n++;
                }
                copy=num;
                // while loop to make new number (ans)
                while(copy!=0) {
                                rem=copy%10;
                                ans=ans+pow(rem,n);
                                copy/=10;
                }
                // if else statement to compare previous number with new number
                if(ans==num) cout << num << " is an Armstrong Number.";
                else cout << num << " is not an Armstrong Number.";
                _getch();
}

Output
Enter Any Number: 153
153 is an Armstrong Number.
Enter Any Number: 456
456 is not an Armstrong Number.

Share
Tweet
Pin
Share
No comments
Hello World! Program in Cpp with explanation

Here is a Simple C++ Program with Explanation which Prints Hello World! Welcome to C++ on the  Screen.


1.       // A Simple C++ Program
2.       #include <iostream>
3.       #include <conio.h>
4.       using namespace std;
5.       // Function main begins program execution
6.       int main()
7.       {
8.             cout << "Hello World!";
9.             cout << "\nWelcome to C++!";
10.         return 0;
11.         _getch();

12.   }


Output
Hello World!
Welcome to C++!


Comment: (Line 1 & 5)

// A Simple C++ Program
The line beginning with a double backslash (//) is a comment. Commenting on your program makes it readable for a large number of people. Consider you are making a program, and later you send it to your friend. Your friend might feel difficult to understand your code. Here commenting will benefit both of you.

Pre-Processor Directive: (Line 2 & 3)

#include <iostream> & #include <conio.h>
Before the actual compilation, the preprocessor is a program invoked by the compiler that modifies the source code. This type of modification is done according to the preprocessor directives that we include in our source files. The directives all start with a hash sign (#). So they are easily distinguished from normal programming code. The first directive we’ll look at is #include, which inserts the contents of a specific file into the current one. If the filename is enclosed between angular brackets <>, the compiler will search for the file in the default directory where it is configured to look for the standard header files. If we instead specify the filename between double-quotes, the compiler first searches for the file in the same directory as the source file, and in case it is not there it will then search among the standard header files.

using namespace std: (Line 4)

using namespace std;
Std is the standard namespace. Cout, cin and many other things are defined in it. (This means that one way to call them is by using std::cout and std::cin.) The keyword using technical means, use this whenever you can. It refers, in this case, to the std namespace.

Function Definition: (Line 6 – 12)

This portion consists of two parts; Function Header and Function Definition (Line 7-12).

Function Header (int main())

Function Header contains return Data-type (int), Function Name (main) and input(s) in parentheses. Return data-type should be according to what value the function will return. It may be int, float or a character. A function name is an identifier, and it should be named carefully.

Read this article to name an identifier.

Its syntax would be
return data-type functionName(inputs)

Function Body (Line 7-12)

All the code enclosed in braces {} is called function body. Function body defines the work of the function.

cout: (Line 8 & 9)

Cout is an object lying in standard namespace. It is used for printing something on the console window.

return statement: (Line 10)

Return statement used to return something from the function. Here Return 0 means successful execution and return 1 indicate unsuccessful execution.

getch(): (Line 11)

_getch() or getch() is a function lying in conio.h header file. It is mostly used in the Visual Studio. Its function is that: It waits for the user to press any key for the output screen to disappear.

About Main Function:

The main function is a user-defined function, as it depends upon the user what he has written inside it. Execution of any C++ programs always starts from the main function. It is the main point of any C++ Program. Without it, no program will run, since execution begins from the main function and also ends on it. There must be the main function in every program.

Thanks for reading. If you like this article then give a Thumbs up. Don't Forget to write your opinion in the comment box and if you think that this article will help any of your friends then do share this article.

Note: C++ is a case sensitive programming language. Make sure you use correct spelling and capitalization. Here in this article, there may be mistakes because it follows basic English rule to be written. So Kindly follow the method told in the program.

Share
Tweet
Pin
Share
1 comments
Identifiers in C++

Identifier

C++ identifiers are names used to identify variable, function, class or any other user-defined items. For Example Variable name, Function name, Class name, structure name.

An identifier starts with a letter either capitals (A to Z) or small (a to z) or an underscore _ that does not begin with a digit. C++ does not allow us to use punctuation characters such as -, @, $ and % within identifiers. C++ is a case sensitive programming language. Thus, Demo and demo are two different identifiers.

Valid Identifiers: Foo, _demo, abc, move_name, a_123, myname50, _temp, a23b9, retVal

Invalid Identifiers: 1Demo, a-123, demo@dss, Amount$100, a%b

Rules for naming an identifier

  1. Identifier name must begin with a letter, either capital (A to Z) or small (a to z).
  2. Identifier name does not begin with a digit (0-9).
  3. It must not contain any space or tab.
  4. Keywords cannot be used as an identifier.
  5. Special Symbols are not allowed in identifier names.
  6. Length of an identifier is at most 32 characters.
  7. Identifiers are case sensitive.

Let’s discuss some examples

Name
Use
Explanation
1sal
No
Because identifier does not begin with a digit.
first name
No
Because there should be no space or tab.
age@
No
Because it does not contain special letters.
While
Yes
Because While is not a keyword. (W is capital and small w is for keyword).
_rent
Yes
Begins with an underscore.
for
No
Because it is a keyword.
last_name
Yes
Because it contains letters and underscore and there is no space between them.


Conventions for naming identifiers

Following conventions is a good practice because it increases the readability of code. It makes the code easier to understand and thus help many people understand the code.

  • Use only letters either capital or small.
  • Use camelCase style for naming an identifier.
  • Choose a name which genuinely shows its work.

Thanks for reading. If you like this article then give a Thumbs up. Don't Forget to write your opinion in the comment box and if you think that this article will help any of your friends then do share this article.

Share
Tweet
Pin
Share
No comments
History and Introduction of C++


In this article, we will see you what exactly is C Plus Plus. When it was developed and why? You know a bit of information about the history of the C++.

What exactly is C Plus Plus(CPP)?


All of you know that a computer is an electronic device that can perform many computational tasks. However, since it is a machine, these computers really can't do anything on their own. So to get any job done using the computer, we need to give instructions to this computer, and according to our guidelines, these computers will work. These instructions which are provided to this computer are called the “Program,” and the person who writes these instructions or who is going to give these instructions is called as a “Programmer.”

The language in which these instructions are written, or these programs are written is called as the “Programming Language.” So here the programmer will use any of the programming language available and write the computer program or the instructions, and according to this program, the computer will work. So here C Plus Plus is one of the programming languages that can be used to write computer programs. There are many programming languages available. For example, we have C, we have C Sharp, we have Java, we have PHP, and we have Python. You know many programming languages are available, and C Plus Plus (CPP) is one of the programming languages. All Right.

Why this C++ was created and when?

This C Plus Plus programming language was created by “Bjarne Stroustrup” and his team in 1979 at the Bell Laboratories of New Jersey. When this language was created, they didn't directly call it as “C Plus Plus” Instead they called it as “C with classes.” During the 1970s C programming language was one of the most used and the most popular programming language, and even to this day, the C programming language is one of the most famous programming languages. However, the C programming language has its limitations. Year by year the computational capacity of the computers increased and also the programming complexity increased.

The problem with C programming language was as the programming complexity increased and the programs tend to become larger; it was difficult to manage and write the code using the C language for those complex computational task. So Bjarne Strostroup and his team they started working on the C programming language they added many features, the main feature they added was the object-oriented programming features and then they created this new language called as “C with classes.”

This “class” it is a part of the object-oriented programming. This object-oriented programming is an entirely different programming approach from the programming approach used in the C language. The object-oriented programming features which were added to this “C with classes” was mainly influenced by another object-oriented programming language which was existing during that time called as the SIMULA-67. Now this new language “C with classes” had the features from two great programming languages some peculiarities from the C programming language and also it had some peculiarities from the SIMULA-67.

The new language which was created in which is the “C with classes” was a better choice for writing the programs where the programmers have to perform some Complex programming operations. And after that in 1983 they renamed this “C with classes” with “C++” Now the exciting thing is this ++ it is actually an operator from the C language and this ++ is an increment operator and since this new language is an incrementation or the next step of the C language they named it as C++. So this is about some information about what exactly is C Plus Plus and about its history. 

Now if you like this article then give a Thumbs up. Don't Forget to write your opinion in the comment box and if you think that this article will help any of your friends then do share this article.

Share
Tweet
Pin
Share
No comments

About me

I am a Passionate Blogger. I am a student of Bachelor of Science in Software Engineering (BSSE) at UOS. I am learning C++ Programming Language. I wants to share what I have learned and my programs that's why I created this blog. Hope you guys will find it useful.

Follow Us

  • facebook
  • instagram
  • twitter

Categories

recent posts

Facebook

Contact Form

Name

Email *

Message *

Created with by ThemeXpose | Programming Vital | Disclaimer| Terms and Conditions