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

Board C++ProgramsC_watermark

The document contains C++ programs for various tasks including checking Armstrong numbers, calculating factorials, generating Fibonacci series, identifying prime numbers, counting characters in a string, checking for palindromes, and replacing spaces with hyphens in strings. Each program includes a brief explanation and the corresponding C++ code. The examples illustrate the functionality of each program with sample inputs and outputs.

Uploaded by

chavantanvi.127
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Board C++ProgramsC_watermark

The document contains C++ programs for various tasks including checking Armstrong numbers, calculating factorials, generating Fibonacci series, identifying prime numbers, counting characters in a string, checking for palindromes, and replacing spaces with hyphens in strings. Each program includes a brief explanation and the corresponding C++ code. The examples illustrate the functionality of each program with sample inputs and outputs.

Uploaded by

chavantanvi.127
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Board C++ Programs

March 2023
1) Armstrong no or Not
Input:153
Output: Yes
Explanation: 153 is an Armstrong number, 1*1*1 + 5*5*5 + 3*3*3 = 153

Program:
#include <iostream>
#include <conio.h>
void main()
{
int num, originalNum, remainder, result = 0;
cout << "Enter a three-digit integer: ";
cin >> num;
originalNum = num;

while (originalNum != 0)
{
// remainder contains the last digit
remainder = originalNum % 10;

result = result + remainder * remainder * remainder;

// removing last digit from the original number


originalNum = originalNum / 10;
}

if (result = = num)
{
cout << num << " is an Armstrong number.";
}
else
{
cout << num << " is not an Armstrong number.";
}
getch();
}
2) factorial of a positive number:
For example,
The factorial of a positive number n, say 5, is denoted by 5! and is given by:
5! = 1 * 2 * 3 * 4 * 5 = 120

Program:
#include <iostream>
#include <conio.h>
class factorial
{
public:
int n;
long factorial = 1;

void factorial()
{
cout << "Enter a positive integer: ";
cin >> n;
if (n < 0)
{
cout << "Error! Factorial of a negative number doesn't exist.";
}
else
{
for(int i = 1; i <= n; i++)
{
factorial = factorial *i;
}
cout << "Factorial of " << n << " = " << factorial;
}
};

void main()
{
clrscr();
factorial objF;

getch();
}
3) Fibonacci series of n number:
In mathematics, the Fibonacci sequence is a sequence in which each element is
the sum of the two elements that precede it. Numbers that are part of the
Fibonacci sequence are known as Fibonacci numbers, commonly denoted Fn .
Many writers begin the sequence with 0 and 1, although some authors start it from
1 and 1[1][2] and some (as did Fibonacci) from 1 and 2. Starting from 0 and 1, the
sequence begins

0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, ...

#include <iostream>
#include <conio.h>
int main()
{
int a=0, b=1 ,c, n, i=3;

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


cin >> n;

// Prints the first two terms.


cout << a<<”\t”<<b<<”\t";

while(i<=n)
{

c=a+b;
cout << c<<”\t”;
a=b;
b=c;

i++;
}
return 0;
}
Output

Enter the number of terms: 5


Fibonacci Series: 0, 1, 1, 2, 3
March 2022
4) Prime number
A positive integer which is only divisible by 1 and itself is known as prime
number.
For example: 13 is a prime number because it is only divisible by 1 and 13 but, 15
is not prime number because it is divisible by 1, 3, 5 and 15.

#include <iostream>
#include <conio.h>
void main()
{
/* variable definition and initialization */
int n, i, c = 0;

/* Get user input */


cout << "Enter any number n: "; cin>>n;

/*logic*/
for (i = 1; i <= n; i++)
{
if (n % i == 0)
{
c++;
}
}
if (c == 2)
{
cout << "number is a Prime number" << endl;
}
else
{
cout << " number is not a Prime number" << endl;
}
getch();
}

Output:
Enter a positive integer: 29
number is a prime number
5) The program takes a string and counts the number of
characters in it.
#include <iostream.h>
#include <conio.h>
#include <stdio.h>
#include <string.h>

void main()
{
char str[100];
int i, totChar;
totChar=0;

//gets and store string from user


cout<<"Please enter the string for count characters\n";
gets(str);

//count characters of a string without space


for(i=0; str[i] != '\0'; i++)
{
if(str[i]!=' ') // this condition is used to avoid counting space
{
totChar++;
}
}
cout<<"The total characters of the given string= "<<totChar;
getch();
}

Output:
Please enter the string
Manvita
The total characters of the given string= 7
6) The program to count and print occurrence of the
character ‘M’ in the given string of maximum 79
characters.
#include <iostream.h>
#include <conio.h>
#include <stdio.h>
#include <string.h>
void main()
{
char str[80];
int i, totChar;
totChar=0;
//gets and store string from user
cout<<"Please enter the string \n";
gets(str);

//count characters of a string without space


for(i=0; str[i] != '\0'; i++)
{
// this condition is used to avoid counting space and counting ‘M’
if(str[i]!=' ' && str[i]=='M' )
{
totChar++;
}
}
cout<<"The total ‘M’ characters of the given string= "<<totChar;
getch();
}

Output:
Please enter the string
Manvita
The total ‘M’ characters of the given string= 1
October 2021
7) number is a palindrome or not
If the reversed integer is equal to the integer entered by user then, that number
is a palindrome if not that number is not a palindrome.

#include <iostream>
#include <conio.h>

void 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 and n > 0) // Negative numbers are not palindromic


{
cout << " The number is a palindrome.";
}
else
{
cout << " The number is not a palindrome.";
}
getch();
}

Output:
Enter a positive number:121
The reverse of the number is:121
The number is a palindrome
8) Replace every space with hyphen in String
#include <iostream.h>
#include <conio.h>
#include <stdio.h>
#include <string.h>

void main()
{
// Get the String
string str = "A computer science portal";

// Traverse the string character by character.


for (int i = 0; i < str.length(); ++i)
{
// Changing the i th character to '-' if it's a space.
if (str[i] == ' ')
{
str[i] = '-';
}
}

// Print the modified string.


cout << str << endl;

getch();
}

Output:
A computer science portal
A-computer-science-portal

You might also like