0% found this document useful (0 votes)
75 views

C++programs Anshul

The document contains C++ programs for printing "Hello World", taking user input, calculating quotient and remainder, reversing an integer, checking if a character is a vowel or consonant, checking if a number is even or odd, swapping two numbers, finding the largest of three numbers, calculating the sum of natural numbers, checking if a year is a leap year, printing the Fibonacci series, generating a multiplication table, printing half pyramids using asterisks, numbers and alphabets, printing inverted half pyramids using asterisks and numbers, and printing a full pyramid using asterisks. Each program contains the necessary header files, uses namespaces, defines the main function and utilizes loops and conditional statements to demonstrate basic C++ concepts

Uploaded by

anshul siwach
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
75 views

C++programs Anshul

The document contains C++ programs for printing "Hello World", taking user input, calculating quotient and remainder, reversing an integer, checking if a character is a vowel or consonant, checking if a number is even or odd, swapping two numbers, finding the largest of three numbers, calculating the sum of natural numbers, checking if a year is a leap year, printing the Fibonacci series, generating a multiplication table, printing half pyramids using asterisks, numbers and alphabets, printing inverted half pyramids using asterisks and numbers, and printing a full pyramid using asterisks. Each program contains the necessary header files, uses namespaces, defines the main function and utilizes loops and conditional statements to demonstrate basic C++ concepts

Uploaded by

anshul siwach
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 51

/* ********************************************************

C++ PROGRAM OF HELLO, WORLD!


******************************************************** */
#include <iostream>
using namespace std;

int main()
{
cout << "Hello, World!";
return 0;
}
/* ******************************************************************
C++ PROGRAM TO PRINT NUMBER ENTERED BY USER
****************************************************************** */

#include <iostream>
using namespace std;

int main()
{
int number;

cout << "Enter an integer: ";


cin >> number;

cout << "You entered " << number;


return 0;
}
/********************************************************************************
C++ PROGRAM TO COMPUTE QUOTIENT AND REMAINDER
******************************************************************************* */
#include <iostream>
using namespace std;

int main()
{
int divisor, dividend, quotient, remainder;

cout << "Enter dividend: ";


cin >> dividend;

cout << "Enter divisor: ";


cin >> divisor;

quotient = dividend / divisor;


remainder = dividend % divisor;

cout << "Quotient = " << quotient << endl;


cout << "Remainder = " << remainder;

return 0;
}
/* ********************************************************
C++ PROGRAM TO REVERSE AN INTEGER
******************************************************** */

#include <iostream>
using namespace std;

int main()
{
int n, reversedNumber = 0, remainder;

cout << "Enter an integer: ";


cin >> n;

while(n != 0)
{
remainder = n%10;
reversedNumber = reversedNumber*10 + remainder;
n /= 10;
}

cout << "Reversed Number = " << reversedNumber;

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;

cout << "Enter an alphabet: ";


cin >> c;
// evaluates to 1 (true) if c is a lowercase vowel
isLowercaseVowel = (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u');
// evaluates to 1 (true) if c is an uppercase vowel
isUppercaseVowel = (c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U');
// evaluates to 1 (true) if either isLowercaseVowel or isUppercaseVowel is true
if (isLowercaseVowel || isUppercaseVowel)
cout << c << " is a vowel.";
else
cout << c << " is a consonant.";
return 0;
}
/* *****************************************************************************
C++ PROGRAM TO CHECK WHETHER NUMBER IS 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;
}
/* ********************************************************
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;

cout << "\nAfter swapping." << endl;


cout << "a = " << a << ", b = " << b << endl;

return 0;
}
/*********************** ********************************************************
C++ PROGRAM TO FIND LARGEST NUMBER AMONG THREE NUMBERS
***************************************************************************** */
#include <iostream>
using namespace std;

int main()
{
float n1, n2, n3;

cout << "Enter three numbers: ";


cin >> n1 >> n2 >> n3;

if(n1 >= n2 && n1 >= n3)


{
cout << "Largest number: " << n1;
}

if(n2 >= n1 && n2 >= n3)


{
cout << "Largest number: " << n2;
}

if(n3 >= n1 && n3 >= n2) {


cout << "Largest number: " << n3;
}

return 0;
}
/* ******************************************************************
C++ PROGRAM TO FIND SUM OF NATURAL NUMBERS
********************************************************************* */
#include <iostream>
using namespace std;

int main()
{
int n, sum = 0;

cout << "Enter a positive integer: ";


cin >> n;

for (int i = 1; i <= n; ++i) {


sum += i;
}

cout << "Sum = " << sum;


return 0;
}
/* ********************************************************
C++ PROGRAM TO CHECK 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;
}
/* *************************************************************
C++ PROGRAM TO DISPLAY FIBONACCI SERIES
************************************************************* */
#include <iostream>
using namespace std;

int main()
{
int n, t1 = 0, t2 = 1, nextTerm = 0;

cout << "Enter the number of terms: ";


cin >> n;

cout << "Fibonacci Series: ";

for (int i = 1; i <= n; ++i)


{
// Prints the first two terms.
if(i == 1)
{
cout << " " << t1;
continue;
}
if(i == 2)
{
cout << t2 << " ";
continue;
}
nextTerm = t1 + t2;
t1 = t2;
t2 = nextTerm;

cout << nextTerm << " ";


}
return 0;
}
/* *********************************************************************
C++ PROGRAM TO GENERATE MULTIPLICATION TABLE
*********************************************************************** */
#include <iostream>
using namespace std;

int main()
{
int n;

cout << "Enter a positive integer: ";


cin >> n;

for (int i = 1; i <= 10; ++i) {


cout << n << " * " << i << " = " << n * i << endl;
}

return 0;
}
/* *************************************************************
C++ PROGRAM TO MAKE HALF PYRAMID USING *
***************************************************************** */

#include <iostream>
using namespace std;

int main()
{
int rows;

cout << "Enter number of rows: ";


cin >> rows;

for(int i = 1; i <= rows; ++i)


{
for(int j = 1; j <= i; ++j)
{
cout << "* ";
}
cout << "\n";
}
return 0;
}
/* *************************************************************************
C++ PROGRAM TO MAKE HALF PYRAMID USING NUBMER
*************************************************************************** */
#include <iostream>
using namespace std;

int main()
{
int rows;

cout << "Enter number of rows: ";


cin >> rows;

for(int i = 1; i <= rows; ++i)


{
for(int j = 1; j <= i; ++j)
{
cout << j << " ";
}
cout << "\n";
}
return 0;
}
/* ***************************************************************************
C++ PROGRAM TO MAKE HALF PYRAMID USING ALPHABETS
*************************************************************************** */
#include <iostream>
using namespace std;

int main()
{
char input, alphabet = 'A';

cout << "Enter the uppercase character you want to print in the last row: ";
cin >> input;

for(int i = 1; i <= (input-'A'+1); ++i)


{
for(int j = 1; j <= i; ++j)
{
cout << alphabet << " ";
}
++alphabet;

cout << endl;


}
return 0;
}
/* *************************************************************************
C++ PROGRAM TO MAKE INVERTED HALF PYRAMID USING *
************************************************************************** */

#include <iostream>
using namespace std;

int main()
{
int rows;

cout << "Enter number of rows: ";


cin >> rows;

for(int i = rows; i >= 1; --i)


{
for(int j = 1; j <= i; ++j)
{
cout << "* ";
}
cout << endl;
}

return 0;
}
/* ********************************************************************************
C++ PROGRAM TO MAKE INVERTED HALF PYRAMID USING NUMBER
******************************************************************************** */
#include <iostream>
using namespace std;

int main()
{
int rows;

cout << "Enter number of rows: ";


cin >> rows;

for(int i = rows; i >= 1; --i)


{
for(int j = 1; j <= i; ++j)
{
cout << j << " ";
}
cout << endl;
}

return 0;
}
/* ********************************************************
C++ PROGRAM TO MAKE FULL PYRAMID USING *
******************************************************** */
#include <iostream>
using namespace std;

int main()
{
int space, rows;

cout <<"Enter number of rows: ";


cin >> rows;

for(int i = 1, k = 0; i <= rows; ++i, k = 0)


{
for(space = 1; space <= rows-i; ++space)
{
cout <<" ";
}

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;

cout << "Enter number of rows: ";


cin >> rows;

for(int i = 1; i <= rows; ++i)


{
for(int space = 1; space <= rows-i; ++space)
{
cout << " ";
++count;
}

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;

cout << endl;


}
return 0;
}
/* *************************************************************************
C++ PROGRAM TO MAKE INVERTED FULL PYRAMID USING *
************************************************************************* */
#include <iostream>
using namespace std;

int main()
{
int rows;

cout << "Enter number of rows: ";


cin >> rows;

for(int i = rows; i >= 1; --i)


{
for(int space = 0; space < rows-i; ++space)
cout << " ";

for(int j = i; j <= 2*i-1; ++j)


cout << "* ";

for(int j = 0; j < i-1; ++j)


cout << "* ";

cout << endl;


}

return 0;
}
/* ********************************************************
C++ PROGRAM TO PRINT PASCAL`S TRIANGLE
******************************************************** */
#include <iostream>
using namespace std;

int main()
{
int rows, coef = 1;

cout << "Enter number of rows: ";


cin >> rows;

for(int i = 0; i < rows; i++)


{
for(int space = 1; space <= rows-i; space++)
cout <<" ";

for(int j = 0; j <= i; j++)


{
if (j == 0 || i == 0)
coef = 1;
else
coef = coef*(i-j+1)/j;

cout << coef << " ";


}
cout << endl;
}

return 0;
}
/* ********************************************************
C++ PROGRAM TO PRINT FLOYD`S TRIANGLE
******************************************************** */
#include <iostream>
using namespace std;

int main()
{
int rows, number = 1;

cout << "Enter number of rows: ";


cin >> rows;

for(int i = 1; i <= rows; i++)


{
for(int j = 1; j <= i; ++j)
{
cout << number << " ";
++number;
}

cout << endl;


}

return 0;
}
/* ********************************************************
C++ PROGRAM TO FIND HCF
******************************************************** */
#include <iostream>
using namespace std;

int main()
{
int n1, n2;

cout << "Enter two numbers: ";


cin >> n1 >> n2;

while(n1 != n2)
{
if(n1 > n2)
n1 -= n2;
else
n2 -= n1;
}

cout << "HCF = " << n1;


return 0;
}
/* ********************************************************
C++ PROGRAM TO FIND FACTORIAL
******************************************************** */
#include <iostream>
using namespace std;

int main()
{
unsigned int n;
unsigned long long factorial = 1;

cout << "Enter a positive integer: ";


cin >> n;

for(int i = 1; i <=n; ++i)


{
factorial *= i;
}

cout << "Factorial of " << n << " = " << factorial;
return 0;
}
/* ********************************************************
C++ PROGRAM TO FIND LCM
******************************************************** */
#include <iostream>
using namespace std;

int main()
{
int n1, n2, max;

cout << "Enter two numbers: ";


cin >> n1 >> n2;

// maximum value between n1 and n2 is stored in max


max = (n1 > n2) ? n1 : n2;

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 << "Enter base and exponent respectively: ";


cin >> base >> exponent;

cout << base << "^" << exponent << " = ";

while (exponent != 0) {
result *= base;
--exponent;
}

cout << result;

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 << "Enter first number: ";


cin >> num1;

cout << "Enter second number: ";


cin >> num2;

cout << "Armstrong numbers between " << num1 << " and " << num2 << " are: " << endl;
for(i = num1; i <= num2; i++)
{
sum = 0;
num = i;

for(; num > 0; num /= 10)


{
digit = num % 10;
sum = sum + digit * digit * digit;
}

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 << "Enter two numbers(intervals): ";


cin >> low >> high;

cout << "Prime numbers between " << low << " and " << high << " are: ";

while (low < high)


{
flag = 0;

for(i = 2; i <= low/2; ++i)


{
if(low % i == 0)
{
flag = 1;
break;
}
}

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;

cout << "Enter a positive integer: ";


cin >> n;

for(i = 2; i <= n / 2; ++i)


{
if(n % i == 0)
{
isPrime = false;
break;
}
}
if (isPrime)
cout << "This is a prime number";
else
cout << "This is not a prime number";

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;

cout << "Enter a positive number: ";


cin >> num;

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;

// Performs multiplication and stores the result in variable productOfTwoNumbers


productOfTwoNumbers = firstNumber * secondNumber;

cout << "Product = " << productOfTwoNumbers;

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 << "Enter a positive integer: ";


cin >> n;

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;

bool checkPrime(int n);

int main()
{
int n, i;
bool flag = false;

cout << "Enter a positive integer: ";


cin >> n;

for(i = 2; i <= n/2; ++i)


{
if (checkPrime(i))
{
if (checkPrime(n - i))
{
cout << n << " = " << i << " + " << n-i << endl;
flag = true;
}
}
}

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;

cout << "Enter operator either + or - or * or /: ";


cin >> op;

cout << "Enter two operands: ";


cin >> 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>

using namespace std;

int convertBinaryToDecimal(long long);

int main()
{
long long n;

cout << "Enter a binary number: ";


cin >> 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 octalToDecimal(int octalNumber);

int main()
{
int octalNumber;
cout << "Enter an octal number: ";
cin >> octalNumber;
cout << octalNumber << " in octal = " << octalToDecimal(octalNumber) << " in decimal";

return 0;
}

// Function to convert octal number to decimal


int octalToDecimal(int octalNumber)
{
int decimalNumber = 0, i = 0, rem;
while (octalNumber != 0)
{
rem = octalNumber % 10;
octalNumber /= 10;
decimalNumber += rem * pow(8, i);
++i;
}
return decimalNumber;
}
int convertBinaryToDecimal(long long n)
{
int decimalNumber = 0, i = 0, remainder;
while (n!=0)
{
remainder = n%10;
n /= 10;
decimalNumber += remainder*pow(2,i);
++i;
}
return decimalNumber;
}
/* ******************************************************************
C++ PROGRAM TO COVERT BINARY NUMBER TO OCTAL
******************************************************************* */
#include <iostream>
#include <cmath>

using namespace std;

int convertBinarytoOctal(long long);


int main()
{
long long binaryNumber;

cout << "Enter a binary number: ";


cin >> binaryNumber;

cout << binaryNumber << " in binary = " << convertBinarytoOctal(binaryNumber) << " in octal ";

return 0;
}

int convertBinarytoOctal(long long binaryNumber)


{
int octalNumber = 0, decimalNumber = 0, i = 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;

// Storing elements of first matrix entered by user.


for(i = 0; i < r; ++i)
for(j = 0; j < c; ++j)
{
cout << "Enter element a" << i + 1 << j + 1 << " : ";
cin >> a[i][j];
}

// Storing elements of second matrix entered by user.


cout << endl << "Enter elements of 2nd matrix: " << endl;
for(i = 0; i < r; ++i)
for(j = 0; j < c; ++j)
{
cout << "Enter element b" << i + 1 << j + 1 << " : ";
cin >> b[i][j];
}

// Adding Two matrices


for(i = 0; i < r; ++i)
for(j = 0; j < c; ++j)
sum[i][j] = a[i][j] + b[i][j];

// Displaying the resultant sum matrix.


cout << endl << "Sum of two matrix is: " << endl;
for(i = 0; i < r; ++i)
for(j = 0; j < c; ++j)
{
cout << sum[i][j] << " ";
if(j == c - 1)
cout << endl;
}
return 0;
}
/* *************************************************************
C++ PROGRAM TO FIND TRANSPOSE OF A MATRIX
************************************************************** */
#include <iostream>
using namespace std;

int main()
{
int a[10][10], trans[10][10], r, c, i, j;

cout << "Enter rows and columns of matrix: ";


cin >> r >> c;
// Storing element of matrix entered by user in array a[][].
cout << endl << "Enter elements of matrix: " << endl;
for(i = 0; i < r; ++i)
for(j = 0; j < c; ++j)
{
cout << "Enter elements a" << i + 1 << j + 1 << ": ";
cin >> a[i][j];
}
// Displaying the matrix a[][]
cout << endl << "Entered Matrix: " << endl;
for(i = 0; i < r; ++i)
for(j = 0; j < c; ++j)
{
cout << " " << a[i][j];
if(j == c - 1)
cout << endl << endl;
}
// Finding transpose of matrix a[][] and storing it in array trans[][].
for(i = 0; i < r; ++i)
for(j = 0; j < c; ++j)
{
trans[j][i]=a[i][j];
}

// Displaying the transpose,i.e, Displaying array trans[][].


cout << endl << "Transpose of Matrix: " << endl;
for(i = 0; i < c; ++i)
for(j = 0; j < r; ++j)
{
cout << " " << trans[i][j];
if(j == r - 1)
cout << endl << endl;
}
return 0;
}
/* ************************************************************************
C++ PROGRAM TO SWAP TWO NUMBER USING CALL BY REFERENCE
************************************************************************* */
#include<iostream>
using namespace std;

void cyclicSwap(int *a, int *b, int *c);

int main()
{
int a, b, c;

cout << "Enter value of a, b and c respectively: ";


cin >> a >> b >> c;

cout << "Value before swapping: " << endl;


cout << "a, b and c respectively are: " << a << ", " << b << ", " << c << endl;

cyclicSwap(&a, &b, &c);

cout << "Value after swapping numbers in cycle: " << endl;
cout << "a, b and c respectively are: " << a << ", " << b << ", " << c << endl;

return 0;
}

void cyclicSwap(int *a, int *b, int *c)


{
int temp;
temp = *b;
*b = *a;
*a = *c;
*c = temp;
}
/* ********************************************************************************
C++ PROGRAM TO FIND THE NUMBER OF VOWELS, CONSONANTS,
DIGITS IN A STRING
******************************************************************************** */
#include <iostream>
using namespace std;

int main()
{
char line[150];
int vowels, consonants, digits, spaces;

vowels = consonants = digits = spaces = 0;

cout << "Enter a line of string: ";


cin.getline(line, 150);
for(int i = 0; line[i]!='\0'; ++i)
{
if(line[i]=='a' || line[i]=='e' || line[i]=='i' ||
line[i]=='o' || line[i]=='u' || line[i]=='A' ||
line[i]=='E' || line[i]=='I' || line[i]=='O' ||
line[i]=='U')
{
++vowels;
}
else if((line[i]>='a'&& line[i]<='z') || (line[i]>='A'&& line[i]<='Z'))
{
++consonants;
}
else if(line[i]>='0' && line[i]<='9')
{
++digits;
}
else if (line[i]==' ')
{
++spaces;
}
}

cout << "Vowels: " << vowels << endl;


cout << "Consonants: " << consonants << endl;
cout << "Digits: " << digits << endl;
cout << "White spaces: " << spaces << endl;

return 0;
}
/* ********************************************************
C++ PROGRAM TO FIND LENGTH OF A STRING
******************************************************** */
#include <iostream>
using namespace std;

int main()
{
string str = "C++ Programming";

// you can also use str.length()


cout << "String Length = " << str.size();

return 0;
}
/* ********************************************************
C++ PROGRAM TO CONCATENAE TWO STRINGS
******************************************************** */
#include <iostream>
using namespace std;

int main()
{
string s1, s2, result;

cout << "Enter string s1: ";


getline (cin, s1);

cout << "Enter string s2: ";


getline (cin, s2);

result = s1 + s2;

cout << "Resultant String = "<< result;

return 0;
}
/* ********************************************************
C++ PROGRAM TO COPY STRING
******************************************************** */
#include <iostream>
using namespace std;

int main()
{
string s1, s2;

cout << "Enter string s1: ";


getline (cin, s1);

s2 = s1;

cout << "s1 = "<< s1 << endl;


cout << "s2 = "<< s2;

return 0;
}
/*******************************************************************************
C++ PROGRAM TO SORT ELEMENTS IN LEXICOGRAPHICAL ORDER
(DICTIONARY ORDER)
*********************************************************************************
*/

#include <iostream>
using namespace std;

int main()
{
string str[10], temp;

cout << "Enter 10 words: " << endl;


for(int i = 0; i < 10; ++i)
{
getline(cin, str[i]);
}

for(int i = 0; i < 9; ++i)


for( int j = i+1; j < 10; ++j)
{
if(str[i] > str[j])
{
temp = str[i];
str[i] = str[j];
str[j] = temp;
}
}

cout << endl<<"In lexicographical order: " << endl;

for(int i = 0; i < 10; ++i)


{
cout << str[i] << endl;
}
return 0;
}
/* ********************************************************************************
C++ PROGRAM TO COMPUTE DIFFERENCE BETWEEN TWO TIME
INTERVALS
******************************************************************************** */
#include <iostream>
using namespace std;

struct TIME
{
int seconds;
int minutes;
int hours;
};

void computeTimeDifference(struct TIME, struct TIME, struct TIME *);

int main()
{
struct TIME t1, t2, difference;

cout << "Enter start time." << endl;


cout << "Enter hours, minutes and seconds respectively: ";
cin >> t1.hours >> t1.minutes >> t1.seconds;
cout << "Enter stop time." << endl;
cout << "Enter hours, minutes and seconds respectively: ";
cin >> t2.hours >> t2.minutes >> t2.seconds;

computeTimeDifference(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;

float calculateSD(float data[]);

int main()
{
int i;
float data[10];

cout << "Enter 10 elements: "<<endl;


for(i = 0; i < 10; ++i)
cin >> data[i];

cout << endl << "Standard Deviation = " << calculateSD(data);

return 0;
}

float calculateSD(float data[])


{
float sum = 0.0, mean, standardDeviation = 0.0;

int i;

for(i = 0; i < 10; ++i)


{
sum += data[i];
}

mean = sum/10;

for(i = 0; i < 10; ++i)


standardDeviation += pow(data[i] - mean, 2);

return sqrt(standardDeviation / 10);


}
/*********************************************************************************
C++ PROGRAM TO STORE AND DISPLAY INFORMATION USING
STRUCTURE
******************************************************************************** */
#include <iostream>
using namespace std;

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;

cout << "\nDisplaying Information," << endl;


cout << "Name: " << s.name << endl;
cout << "Roll: " << s.roll << endl;
cout << "Marks: " << s.marks << endl;
return 0;
}
//Add Distances Using Structures
#include <iostream>
using namespace std;

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;

cout << "\nEnter information for 2nd distance" << endl;


cout << "Enter feet: ";
cin >> d2.feet;
cout << "Enter inch: ";
cin >> d2.inch;

sum.feet = d1.feet+d2.feet;
sum.inch = d1.inch+d2.inch;

// changing to feet if inch is greater than 12


if(sum.inch > 12)
{
++ sum.feet;
sum.inch -= 12;
}

cout << endl << "Sum of distances = " << sum.feet << " feet " << sum.inch << " inches";
return 0;
}

You might also like