Skip to main content

Posts

Showing posts from April, 2023

What is Dynamic Memory Allocation in C++ Program

In the computer world, anything that is processed be it an instruction or any data first needs to be loaded and located in internal memory.  In C++ programs also any data that is processed while executing the program is held in the internal memory.  What is Dynamic Memory Allocation? Dynamic Memory allocation means that the memory that will be used during the program is not known beforehand and is allocated dynamically and on the go. It is allocated during the runtime as and when required by the program. In C++ there are two operators used in dynamic memory allocation  1. New  2. Delete New operator in dynamic memory allocation The new operator in C++ is used to create objects of all types. The new operator will allocate memory of the size of the data type specified in the program.  For Example iptr = new int ;  Storing initial values will allocate needed amount of memory from the free store to hold the value of the specified data-type and store the starting address of the newly alloca

C++ Program to input number of week's days and convert to equivalent name of day of the week

In C++ Program, we can write a code to input the number of week's days and convert it to the equivalent name of day of the week This is done by using the switch selection statement in C++ if you want to learn about the selection statements read it here, What are Selection Statements in C++  . Let's look at the code  to input the number of week's days and convert it to the equivalent name of day of the week #include <iostream.h> using namespace std ; int main ()  { int dow ; cout <<"Enter number of week's day(1-7) : " ;  cin >> dow;  switch (dow)  { case 1 : cout<< "\n" <<"Sunday" ; break ;  case 2 : cout<< "\n" <<"Monday" ; break ;  case 3 : cout<< "\n" <<"Tuesday" ; break ;  case 4 : cout<< "\n" <<"Wednesday" ; break ;  case 5 : cout<< "\n" <<"Friday" ; break ; case 6 : cout<< &

What are selection statements in C++ ? If and Switch

There are several statements that we can use in C++ programming that help in choosing a set of instructions for using the code depending upon an expression truth value.  What are Selection Statements in C++? As mentioned above there are several statements that we can use to choose a set of pre-defined instructions within a program in any section of the program as per the need. These are called selection statements in C++  In C++ Program there are two types of Selection statements  1. If  2. Switch  The If Statement of C++ To understand "if statement" let's look at an example.  Example - If you have 10 rupees then you can buy the chocolate.  Here, there is a condition in buying chocolate, if you have 10 rupees then only you can buy it.  This shows that the If is a condition which can be true or false, If it is false it won't be executed, in this example, if you don't have 10 rupees i.e. the condition is false you cant buy the chocolate.  So, If statement checks for

C++ Program to calculate the factorial of an integer

In C++ program you can write a program to calculate the factorial of an integer . Let's look at the code to calculate the factorial of an integer.  #include <iostream> using namespace std ; int main ()  { int i, num, fact=1 ; cout <<"\n Enter integer" ; cin>> num; i = num ;  while (num)  { fact *=num ; --num ; } cout<< "The factorial of " << i ; cout<<"is" << fact <<"\n" ; return 0 ;  } The output of the C++ Code  C++ Program to calculate the factorial of an integer

What is iostream.h in C++ Programing Language ?

In C++ programing language, all header files end with .H extension. In standard C++, all devices are accessed and used as a file.  One such header file is iostream.h in C++ programming language. In this, all input and output devices are treated as files.  Let's quickly look at what are the input and output devices mean in C++ program.  Standard Input Device - Keyboard  Standard Output Device   - Monitor  Standard Error Device - Screen or monitor In C++, input and output are not defined within, and are implemented with the help of a component of the C++ standard library which is I/O library.  A file is read simply as a stream of bytes at the lowest level. But at a user level, a file consists of possibly mixed data types which can be characters, arithmetic values class, objects etc.  What are the predefined streams in I/O Library? As discussed above there are input, output and error library in c++, they have some predefined streams objects as well which are used in the code. Let'

C++ Program to calculate the area of a square

In C++ program you can write a program to calculate the area of a square. Let's look at the C++ Code to calculate the area of a square . #include <iostream.h> #include <conio.h> using namespace std; int main () { float side, area ; cout<<"Enter side of the square : " ; cin >>side ; cout<<"\n The area of the square is :" ; cout<<side*side ; cout<<"square-units" ; return 0 ; } The Output of the C++ Code C++ Program to calculate the area of a square 

C++ Program to convert a given number of days into years, weeks and days

In C++ Programs we can write a program to convert a given number of days into years, weeks and days. Let's look at the C++ Code  to convert a given number of days into years, weeks and days. #include <iostream.h> #include <conio.h> using namespace std ; int main () { clrscr ();  int totdays, years, weeks, days, num1;  cout<<"Ener total number of days : " ; cin>> totdays ;  years = totdays / 366 ;  num1= totdays % 365 ;  weeks = num1 / 7 ; days = num1 % 7 ; cout<<"\n" ; cout<<" Years =" << years <<"," ;  cout<<"weeks ="<< weeks <<"," ;  cout<<"Days ="<< days <<"\n" ; return 0 ; } The output of the C++ Code C++ Program to convert a given number of days into years, weeks and days 

Difference between call by value and call by reference

What is Call By Value in C  In call by value method, the called function creates its own copies of the original values that are sent to it. The changes made in the value are made on the copy of the called function and do not affect the main function.  Example of call by value in c int main () { int a=6 ;  cout<< "a =" << a ; change (a) ; cout <<"\n a=" << a ; return 0;  } void change (int b) {  b = 10 ;   }  What is Call By Reference in C In call by reference method, the called function works with the original value using their reference. The changes made are reflected back on the original code.  Example of call by value in c int main () { int a=5 ;  cout<< "a =" << a ; change (a) ; cout <<"\n a=" << a ; return 0;  } void change (int & b) {  b = 10 ;   } 

C++ Program to check if a string is palindrome or not

The word palindrome means that when read from left to right or right to left it is the same. The spelling of palindromic words is the same on either side.  In C++ Programs we can write a program to check if the string is palindrome or not. C++ Program to check if a string is a palindrome or not #include <iostream> #include <stdlib.h> int main () { system ("cls"); char string[80],c ; std::cout<<"Enter a string (maximum 79 characters only) \n"; std::cin.getline(string,80); for (int len = 0 ; string[len] != '\0'; ++len) ; int i,j,len, flag = 1 ;  for (i = 0, j = len - 1 ; i < len / 2 ; ++i, --j)  { if (string[i]=string[j]) { flag = 0 ; break ;  } } if (flag) std::cout <<"It is a palindrome \n"; else  std::cout<<"It is not a palindrome \n" ; return 0 ; } The output of the C++ Code C++ Program to check if a string is a palindrome or not

C++ Program to count the number of characters entered

In C++ Program you can write a code to count the characters of the word entered. Let's look at the coding. C++ Program to count the number of characters entered #include <iostream.h> #include <conio.h> int main () { int chcount =  0 ; const char ent = '\n' ; char ch ; std::cout << "Enter character\n" ;  std::cin.get(ch) ; while (ch!=ent) { chcount++ ; std::cout.put(ch) ; std::cin.get(ch) ; } std::cout <<"\n The number of characters = " <<chcount <<"\n" ; return 0 ;  } The output of the C++ Program Code C++ Program to count the number of characters entered

C++ Program to Convert Temperature from Fahrenheit to Celsius and Celsius to Fahrenheit (If-else statement)

In C++ programming language we can write a program to convert temperature from Fahrenheit to Celsius and Celsius to Fahrenheit with the simple use ofIf-else statement. Let's us look at the code.  C++ Program to Convert Temperature from Fahrenheit to Celsius and Celsius to Fahrenheit  (If-else statement) #include <iostream.h> int main() { int choice ;  float temp, countemp ;  std::cout<<"Temperature conversion Menu"<<"\n" ; std::cout<<"1. Fahrenheit to Celsius"<<"\n" ; std::cout<<"2. Celsius to Fahrenheit"<<"\n" ; std::cout<<"Enter your choice:" ; std::cin>>choice;  if( choice == 1) { std::cout<<"\n"<<" Enter temperature in Fahrenheit :" ; std::cin >> temp ;  countemp = (temp -32) / 1.8 ; std::cout<<" The temperature in Celsius is : " << countemp <<"\n" ; } else  { std::cout<<&quo

C ++ Program to read radius of a circle to calculate area and perimeter to display them

In C++ Program you can easily write a code to read the radius of a circle to calculate the area of the circle and the perimeter of the circle. Let's have a look at the C ++ Program to read radius of a circle to calculate area and perimeter to display them  #include <iostream.h> #include <stdlib.h> int main () { system ("cls") double radius, area, perimeter ; cout<<"Enter Radius of the circle : "; cin>>radius ; perimeter = 2 * 3.14159 * radius ; area = 3.14159 * radius * radius ; cout <<"The area of the circle is" ; cout<< area ; cout<<"\n The perimeter of the circle is : " ; cout<< perimeter ; return 0 ; } Out Put of the Above C++ Code  C++ Code for Circle area and Perimeter Output

Top online c++ compliers to use for free

In C++ programming compilers plays a vital role. With the use of compilers such as Turbo C++ which is a free C++ compiler to download and use it on a computer.  However, the trends have changed, not everyone has the time to download and set up offline C++ compilers so now there are few Online C++ compilers available for quick testing of codes.  Let's look at some of the Online C++ Compilers that you can use for free List of Online Free C++ Compilers programiz onlinegdb scaler.com/topics/cpp/online-cpp-compiler/ tutorialspoint   w3schools Programiz Programiz Online C++ Complier Onlinegdb onlinegdb Online C++ Compiler scaler.com/topics/cpp/online-cpp-compiler/ Scaler Online C++ Compiler tutorialspoint Tutorialspoint Online C++ Compiler w3schools W3school Online C++ Compiler These are few of the C++ online compilers proved by some of the best names out there on the web. Feel free to visit any of those and use them.   

What are Comments in a C++ Programming?

In C++ programing language we can use comments to define different sections in the program itself.  Comments in C++ are not detected by the C++ compiler, it ignores the comments and simply does not compline or give any error related to comments. However there is a code or say format set to define comments in C++, otherwise it will show an error while compiling the code. Comments are denoted by // , whenever you write a comment in a C++ program you need to put // before it so that the compiler knows it is a comment and ignores it while compiling the program.  C++ program sample with comment Here is a simple code program which also has comment in it // A sample program  #include <iostream.h> void main ( ) { cout<<"Hello There!!" ; /* This is a simple program with both of the comment types */ } In the above program, you can see that there are two types of comments that are used. Lets us see the Two types of Comments in the C++ Program. Types of Comments in C++ Progra

Program to print odd numbers from 100 to 1 in C++

In a C++ program, you can write code to print odd numbers from 100 to 1. Here is how to do it.  C++ Program Code #include <iostream.h> int main () { for (int i=99 ; i > 0 ; i-- ) if (i % 2) std::cout << i << ' ' ; return 0 ;  } The output of the Program