Board C++ProgramsC_watermark
Board C++ProgramsC_watermark
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;
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
#include <iostream>
#include <conio.h>
int main()
{
int a=0, b=1 ,c, n, i=3;
while(i<=n)
{
c=a+b;
cout << c<<”\t”;
a=b;
b=c;
i++;
}
return 0;
}
Output
#include <iostream>
#include <conio.h>
void main()
{
/* variable definition and initialization */
int n, i, c = 0;
/*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;
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);
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;
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";
getch();
}
Output:
A computer science portal
A-computer-science-portal