c++ programs list and cpp basic program list
In this article today learn c++ programs list and CPP basic program list. basic program means primary stage program hello world program, leap year program if statement program, loop program, and basic mathematics program so let's start c++ programs list and CPP basic program list.
1.cpp hello world program:
#include <iostream>
using namespace std;
int main()
{
cout << "Hello, World!";
return 0;
}
2.Add two number program in cpp :
#include <iostream>
using namespace std;
int main()
{
int
firstNumber, secondNumber, sumOfTwoNumbers;
cout
<< "Enter two integers: ";
cin
>> firstNumber >> secondNumber;
// sum
of two numbers in stored in variable sumOfTwoNumbers
sumOfTwoNumbers = firstNumber + secondNumber;
//
Prints sum
cout
<< firstNumber << " + " << secondNumber
<< " = " << sumOfTwoNumbers;
return
0;
}
3.Armstrong number program in cpp :
#include <iostream>
using namespace std;
int main()
{
int origNum, num, rem, sum = 0;
cout <<"Enter a positive integer:
";
cin >> origNum;
num = origNum;
while(num != 0)
{
rem = num % 10;
sum += rem * rem * rem;
num /= 10;
}
if(sum == origNum)
cout << origNum << " is
an Armstrong number.";
else
cout << origNum << " is
not an Armstrong number.";
return 0;
}
4. c++ even or odd :
#include <iostream>
using namespace std;
int main()
{
int n;
cout << "Enter an integer:
";
cin >> n;
if ( n % 2 == 0)
cout << n << " is
even.";
else
cout << n << " is
odd.";
return 0;
}
5. CPP program leap year :
#include <iostream>
using namespace std;
int main()
{
int year;
cout << "Enter a year: ";
cin >> year;
if (year % 4 == 0)
{
if (year % 100 == 0)
{
if (year % 400 == 0)
cout << year <<
" is a leap year.";
else
cout << year << "
is not a leap year.";
}
else
cout << year << "
is a leap year.";
}
else
cout << year << " is
not a leap year.";
return 0;
}
6. c++ Check Prime or Not in Number:
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int num,i,count=0;
cout<<"Enter a number:";
cin>>num;
for(i=2;i<num;i++)
{
if(num%i==0)
{
count++;
break;
}
}
if(count==0)
{
cout<<"This is a
prime number";
}
else
{
cout<<"This is
not a prime number";
}
getch();
}
7. cpp Pyramid Patterns program :
#include <iostream>
using namespace std;
void pypart(int n)
{
for (int i=0; i<n; i++)
{
for(int j=0; j<=i; j++ )
{
cout << "* ";
}
cout << endl;
}
}
int main()
{
int n = 5;
pypart(n);
return 0;
}
8. cpp Rectangle Area program :
#include<iostream.h>
#include<conio.h>
void main()
{
int
length,breath,area_rec=0,parameter=0;
clrscr();
cout<<"Enter Length
and Breath of Rectangle: ";
cin>>length>>breath;
area_rec=length*breath;
parameter=2*(length+breath);
cout<<"\nArea of
Ractangle: "cout<<area_rec;
cout<<"\nParameters
of Rectangle: "cout<<parameter;
getch();
}
9. cpp Reverse String program :
#include<iostream>
#include<string.h>
using namespace std;
int main ()
{
char str[50], temp;
int i, j;
cout << "Enter a string :
";
gets(str);
j = strlen(str) - 1;
for (i = 0; i < j; i++,j--)
{
temp = str[i];
str[i] = str[j];
str[j] = temp;
}
cout << "\nReverse string :
" << str;
return 0;
}
10. cpp Simple Calculator program :
#include<iostream.h>
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
clrscr();
float a, b, res;
char choice, ch;
do
{
cout<<"1.Addition\n";
cout<<"2.Subtraction\n";
cout<<"3.Multiplication\n";
cout<<"4.Division\n";
cout<<"5.Exit\n\n";
cout<<"Enter Your
Choice : ";
cin>>choice;
switch(choice)
{
case '1'
: cout<<"Enter two number : ";
cin>>a>>b;
res=a+b;
cout<<"Result
= "<<res;
break;
case '2'
: cout<<"Enter two number : ";
cin>>a>>b;
res=a-b;
cout<<"Result
= "<<res;
break;
case '3'
: cout<<"Enter two number : ";
cin>>a>>b;
res=a*b;
cout<<"Result
= "<<res;
break;
case '4'
: cout<<"Enter two number : ";
cin>>a>>b;
res=a/b;
cout<<"Result
= "<<res;
break;
case '5'
: exit(0);
break;
default
: cout<<"Wrong Choice..!!";
break;
}
cout<<"\n------------------------------------\n";
}while(choice!=5 && choice!=getchar());
getch();
}
11. cpp If Program :
#include <iostream>
using namespace std;
int main()
{
int number;
cout << "Enter an integer: ";
cin >> number;
// checks if the number is positive
if ( number > 0)
{
cout << "You entered a
positive integer: " << number
<< endl;
}
cout << "This statement is always
executed.";
return 0;
}
12. cpp If Else Program :
#include <iostream>
using namespace std;
int main () {
// local variable declaration:
int a = 100;
// check the boolean condition
if( a < 20 ) {
// if condition is true then print the
following
cout << "a is less than
20;" << endl;
} else {
// if condition is false then print the
following
cout << "a is not less than
20;" << endl;
}
cout << "value of a is : "
<< a << endl;
return 0;
}
13. cpp If Else If Program :
#include <iostream>
using namespace std;
int main () {
// local variable declaration:
int a = 100;
// check the boolean condition
if( a == 10 ) {
// if condition is true then print the
following
cout << "Value of a is
10" << endl;
} else if( a == 20 ) {
// if else if condition is true
cout << "Value of a is 20" << endl;
} else if( a == 30 ) {
// if else if condition is true
cout << "Value of a is
30" << endl;
} else {
// if none of the conditions is true
cout << "Value of a is not
matching" << endl;
}
cout << "Exact value of a is :
" << a << endl;
return 0;
}
14.cpp Switch Statment Program :
#include <iostream>
using namespace std;
int main()
{
char o;
float num1, num2;
cout << "Enter an operator (+,
-, *, /): ";
cin >> o;
cout
<< "Enter two operands: ";
cin >> num1 >> num2;
switch (o)
{
case '+':
cout << num1 << "
+ " << num2 << " = " << num1+num2;
break;
case '-':
cout << num1 << "
- " << num2 << " = " << num1-num2;
break;
case '*':
cout << num1 << "
* " << num2 << " = " << num1*num2;
break;
case '/':
cout << num1 << "
/ " << num2 << " = " << num1/num2;
break;
default:
// operator is doesn't match any case
constant (+, -, *, /)
cout << "Error! operator
is not correct";
break;
}
return
}
15. cpp For Loop Program :
#include <iostream>
using namespace std;
int main () {
// for loop execution
for( int a = 10; a < 20; a = a + 1 ) {
cout << "value of a: "
<< a << endl;
}
return 0;
}
16. cpp While Loop Program :
#include <iostream>
using namespace std;
int main()
{
int number, i = 1, factorial = 1;
cout << "Enter a positive
integer: ";
cin >> number;
while ( i >= number) {
factorial *= i; //factorial = factorial * i;
++i;
}
cout<<"Factorial of
"<< number <<" = "<< factorial;
return 0;
}
17. cpp Do While Loop Program :
#include <iostream>
using namespace std;
int main () {
// Local variable declaration:
int a = 10;
// do loop execution
do {
cout << "value of a:
" << a << endl;
a = a + 1;
} while( a < 20 );
return 0;
}
No comments:
Post a Comment