C++programs Anshul
C++programs Anshul
C++programs Anshul
int main()
{
cout << "Hello, World!";
return 0;
}
/* ******************************************************************
C++ PROGRAM TO PRINT NUMBER ENTERED BY USER
****************************************************************** */
#include <iostream>
using namespace std;
int main()
{
int number;
int main()
{
int divisor, dividend, quotient, remainder;
return 0;
}
/* ********************************************************
C++ PROGRAM TO REVERSE AN INTEGER
******************************************************** */
#include <iostream>
using namespace std;
int main()
{
int n, reversedNumber = 0, remainder;
while(n != 0)
{
remainder = n%10;
reversedNumber = reversedNumber*10 + remainder;
n /= 10;
}
return 0;
}
/* *******************************************************************************
C++ PROGRAM TO CHECK WHETHER A CHARACTER IS VOWEL OR
CONSONANT
*********************************************************************************
*/
#include <iostream>
using namespace std;
int main()
{
char c;
int isLowercaseVowel, isUppercaseVowel;
#include <iostream>
using namespace std;
int main()
{
int n;
if ( n % 2 == 0)
cout << n << " is even.";
else
cout << n << " is odd.";
return 0;
}
/* ********************************************************
C++ PROGRAM TO SWAP TWO NUMBERS
******************************************************** */
#include <iostream>
using namespace std;
int main()
{
int a , b, temp;
cout <<"Enter value of a and b :";
cin >>a>>b;
cout << "Before swapping." << endl;
cout << "a = " << a << ", b = " << b << endl;
temp = a;
a = b;
b = temp;
return 0;
}
/*********************** ********************************************************
C++ PROGRAM TO FIND LARGEST NUMBER AMONG THREE NUMBERS
***************************************************************************** */
#include <iostream>
using namespace std;
int main()
{
float n1, n2, n3;
return 0;
}
/* ******************************************************************
C++ PROGRAM TO FIND SUM OF NATURAL NUMBERS
********************************************************************* */
#include <iostream>
using namespace std;
int main()
{
int n, sum = 0;
int main()
{
int 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;
}
/* *************************************************************
C++ PROGRAM TO DISPLAY FIBONACCI SERIES
************************************************************* */
#include <iostream>
using namespace std;
int main()
{
int n, t1 = 0, t2 = 1, nextTerm = 0;
int main()
{
int n;
return 0;
}
/* *************************************************************
C++ PROGRAM TO MAKE HALF PYRAMID USING *
***************************************************************** */
#include <iostream>
using namespace std;
int main()
{
int rows;
int main()
{
int rows;
int main()
{
char input, alphabet = 'A';
cout << "Enter the uppercase character you want to print in the last row: ";
cin >> input;
#include <iostream>
using namespace std;
int main()
{
int rows;
return 0;
}
/* ********************************************************************************
C++ PROGRAM TO MAKE INVERTED HALF PYRAMID USING NUMBER
******************************************************************************** */
#include <iostream>
using namespace std;
int main()
{
int rows;
return 0;
}
/* ********************************************************
C++ PROGRAM TO MAKE FULL PYRAMID USING *
******************************************************** */
#include <iostream>
using namespace std;
int main()
{
int space, rows;
while(k != 2*i-1)
{
cout << "* ";
++k;
}
cout << endl;
}
return 0;
}
/* ***********************************************************************
C++ PROGRAM TO MAKE FULL PYRAMID USING NUMBER
************************************************************************* */
#include <iostream>
using namespace std;
int main()
{
int rows, count = 0, count1 = 0, k = 0;
while(k != 2*i-1)
{
if (count <= rows-1)
{
cout << i+k << " ";
++count;
}
else
{
++count1;
cout << i+k-2*count1 << " ";
}
++k;
}
count1 = count = k = 0;
int main()
{
int rows;
return 0;
}
/* ********************************************************
C++ PROGRAM TO PRINT PASCAL`S TRIANGLE
******************************************************** */
#include <iostream>
using namespace std;
int main()
{
int rows, coef = 1;
return 0;
}
/* ********************************************************
C++ PROGRAM TO PRINT FLOYD`S TRIANGLE
******************************************************** */
#include <iostream>
using namespace std;
int main()
{
int rows, number = 1;
return 0;
}
/* ********************************************************
C++ PROGRAM TO FIND HCF
******************************************************** */
#include <iostream>
using namespace std;
int main()
{
int n1, n2;
while(n1 != n2)
{
if(n1 > n2)
n1 -= n2;
else
n2 -= n1;
}
int main()
{
unsigned int n;
unsigned long long factorial = 1;
cout << "Factorial of " << n << " = " << factorial;
return 0;
}
/* ********************************************************
C++ PROGRAM TO FIND LCM
******************************************************** */
#include <iostream>
using namespace std;
int main()
{
int n1, n2, max;
do
{
if (max % n1 == 0 && max % n2 == 0)
{
cout << "LCM = " << max;
break;
}
else
++max;
} while (true);
return 0;
}
/* **************************************************************
C++ PROGRAM TO CALCULATE POWER OF A NUMBER
************************************************************** */
#include <iostream>
using namespace std;
int main()
{
int exponent;
float base, result = 1;
cout << base << "^" << exponent << " = ";
while (exponent != 0) {
result *= base;
--exponent;
}
return 0;
}
/* ********************************************************************************
C++ PROGRAM TO CHECK WHETHER A NUMBER IS ARMSTRONG OR NOT
******************************************************************************** */
#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;
}
/* *******************************************************************************
C++ PROGRAM TO DISPLAY ARMSTRONG NUMBER BETWEEN INTERVAL
******************************************************************************** */
#include <iostream>
using namespace std;
int main()
{
int num1, num2, i, num, digit, sum;
cout << "Armstrong numbers between " << num1 << " and " << num2 << " are: " << endl;
for(i = num1; i <= num2; i++)
{
sum = 0;
num = i;
if(sum == i)
{
cout << i << endl;
}
}
return 0;
}
/* *****************************************************************************
C++ PROGRAM TO DISPLAY PRIME NUMBER BETWEEN NUMBERS
***************************************************************************** */
#include <iostream>
using namespace std;
int main()
{
int low, high, i, flag;
cout << "Prime numbers between " << low << " and " << high << " are: ";
if (flag == 0)
cout << low << " ";
++low;
}
return 0;
}
/* ****************************************************************************
C++ PROGRAM TO CHECK WHETHER A NUMBER IS PRIME OR NOT
****************************************************************************** */
#include <iostream>
using namespace std;
int main()
{
int n, i;
bool isPrime = true;
return 0;
}
/**********************************************************************************
C++ PROGRAM TO CHECK WHETHER A NUMBER IS PALINDROME OR NOT
******************************************************************************** */
#include <iostream>
using namespace std;
int main()
{
int n, num, digit, rev = 0;
n = num;
do
{
digit = num % 10;
rev = (rev * 10) + digit;
num = num / 10;
} while (num != 0);
cout << " The reverse of the number is: " << rev << endl;
if (n == rev)
cout << " The number is a palindrome.";
else
cout << " The number is not a palindrome.";
return 0;
}
/* *************************************************************
C++ PROGRAM TO FIND PRODUCT OF TWO NUMBERS
************************************************************* */
#include <iostream>
using namespace std;
int main()
{
double firstNumber, secondNumber, productOfTwoNumbers;
cout << "Enter two numbers: ";
// Stores two floating point numbers in variable firstNumber and secondNumber respectively
cin >> firstNumber >> secondNumber;
return 0;
}
/* ********************************************************
C++ PROGRAM TO FIND ASCII VALUE
******************************************************** */
#include <iostream>
using namespace std;
int main()
{
char c;
cout << "Enter a character: ";
cin >> c;
cout << "ASCII Value of " << c << " is " << int(c);
return 0;
}
/* ********************************************************
C++ PROGRAM TO FIND FACTORS OF A NUMBER
******************************************************** */
FACTOR OF A NUMBER
#include <iostream>
using namespace std;
int main()
{
int n, i;
cout << "Factors of " << n << " are: " << endl;
for(i = 1; i <= n; ++i)
{
if(n % i == 0)
cout << i << endl;
}
return 0;
}
/* *******************************************************************************
C++ PROGRAM TO EXPRESS A NUMBER AS SUM OF PRIME NUMBERS
******************************************************************************** */
#include <iostream>
using namespace std;
int main()
{
int n, i;
bool flag = false;
if (!flag)
cout << n << " can't be expressed as sum of two prime numbers.";
return 0;
}
/* **********************************************************
C++ PROGRAM OF SIMPLE CALCULATOR
*********************************************************** */
# include <iostream>
using namespace std;
int main()
{
char op;
float num1, num2;
switch(op)
{
case '+':
cout << num1+num2;
break;
case '-':
cout << num1-num2;
break;
case '*':
cout << num1*num2;
break;
case '/':
cout << num1/num2;
break;
default:
// If the operator is other than +, -, * or /, error message is shown
cout << "Error! operator is not correct";
break;
}
return 0;
}
/* **********************************************************************
C++ PROGRAM TO CONVERT BINARY NUMBER TO DECIMAL
************************************************************************ */
#include <iostream>
#include <cmath>
int main()
{
long long n;
cout << n << " in binary = " << convertBinaryToDecimal(n) << " in decimal";
return 0;
}
/* *********************************************************************
C++ PROGRAM TO CONVERT OCTAL NUMBER TO DECIMAL
********************************************************************** */
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
int octalNumber;
cout << "Enter an octal number: ";
cin >> octalNumber;
cout << octalNumber << " in octal = " << octalToDecimal(octalNumber) << " in decimal";
return 0;
}
cout << binaryNumber << " in binary = " << convertBinarytoOctal(binaryNumber) << " in octal ";
return 0;
}
while(binaryNumber != 0)
{
decimalNumber += (binaryNumber%10) * pow(2,i);
++i;
binaryNumber/=10;
}
i = 1;
while (decimalNumber != 0)
{
octalNumber += (decimalNumber % 8) * i;
decimalNumber /= 8;
i *= 10;
}
return octalNumber;
}
/* ********************************************************
C++ PROGRAM TO ADD TWO MATRICES
******************************************************** */
#include <iostream>
using namespace std;
int main()
{
int r, c, a[100][100], b[100][100], sum[100][100], i, j;
cout << "Enter number of rows (between 1 and 100): ";
cin >> r;
cout << "Enter number of columns (between 1 and 100): ";
cin >> c;
cout << endl << "Enter elements of 1st matrix: " << endl;
int main()
{
int a[10][10], trans[10][10], r, c, i, j;
int main()
{
int a, b, c;
cout << "Value after swapping numbers in cycle: " << endl;
cout << "a, b and c respectively are: " << a << ", " << b << ", " << c << endl;
return 0;
}
int main()
{
char line[150];
int vowels, consonants, digits, spaces;
return 0;
}
/* ********************************************************
C++ PROGRAM TO FIND LENGTH OF A STRING
******************************************************** */
#include <iostream>
using namespace std;
int main()
{
string str = "C++ Programming";
return 0;
}
/* ********************************************************
C++ PROGRAM TO CONCATENAE TWO STRINGS
******************************************************** */
#include <iostream>
using namespace std;
int main()
{
string s1, s2, result;
result = s1 + s2;
return 0;
}
/* ********************************************************
C++ PROGRAM TO COPY STRING
******************************************************** */
#include <iostream>
using namespace std;
int main()
{
string s1, s2;
s2 = s1;
return 0;
}
/*******************************************************************************
C++ PROGRAM TO SORT ELEMENTS IN LEXICOGRAPHICAL ORDER
(DICTIONARY ORDER)
*********************************************************************************
*/
#include <iostream>
using namespace std;
int main()
{
string str[10], temp;
struct TIME
{
int seconds;
int minutes;
int hours;
};
int main()
{
struct TIME t1, t2, difference;
cout << endl << "TIME DIFFERENCE: " << t1.hours << ":" << t1.minutes << ":" << t1.seconds;
cout << " - " << t2.hours << ":" << t2.minutes << ":" << t2.seconds;
cout << " = " << difference.hours << ":" << difference.minutes << ":" << difference.seconds;
return 0;
}
void computeTimeDifference(struct TIME t1, struct TIME t2, struct TIME *difference){
if(t2.seconds > t1.seconds)
{
--t1.minutes;
t1.seconds += 60;
}
difference->seconds = t1.seconds - t2.seconds;
if(t2.minutes > t1.minutes)
{
--t1.hours;
t1.minutes += 60;
}
difference->minutes = t1.minutes-t2.minutes;
difference->hours = t1.hours-t2.hours;
}
/* ****************************************************************
C++ PROGRAM TO CALCULATE STANDARD DEVIATION
**************************************************************** */
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
int i;
float data[10];
return 0;
}
int i;
mean = sum/10;
struct student
{
char name[50];
int roll;
float marks;
};
int main()
{
student s;
cout << "Enter information," << endl;
cout << "Enter name: ";
cin >> s.name;
cout << "Enter roll number: ";
cin >> s.roll;
cout << "Enter marks: ";
cin >> s.marks;
struct Distance{
int feet;
float inch;
}d1 , d2, sum;
/*******************************************************
C++ PROGRAM TO ADD DISTANCE(FEET)
***************************************************** */
int main()
{
cout << "Enter 1st distance," << endl;
cout << "Enter feet: ";
cin >> d1.feet;
cout << "Enter inch: ";
cin >> d1.inch;
sum.feet = d1.feet+d2.feet;
sum.inch = d1.inch+d2.inch;
cout << endl << "Sum of distances = " << sum.feet << " feet " << sum.inch << " inches";
return 0;
}