Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 10 additions & 8 deletions Programs/armstrong.cpp
Original file line number Diff line number Diff line change
@@ -1,26 +1,28 @@
#include <iostream>
#include <cmath>
using namespace std;

int main() {
int num, originalNum, remainder, result = 0;
int main(void) {
size_t originalNum, remainder, result = 0;
int input;
cout << "Enter a three-digit integer: ";
cin >> num;
originalNum = num;
cin >> input;
originalNum = abs(input);

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

result += remainder * remainder * remainder;
result += pow(remainder,3);

// removing last digit from the orignal number
originalNum /= 10;
}

if (result == num)
cout << num << " is an Armstrong number.";
if (result == abs(input))
cout << abs(input) << " is an Armstrong number.";
else
cout << num << " is not an Armstrong number.";
cout << abs(input) << " is not an Armstrong number.";

return 0;
}
20 changes: 12 additions & 8 deletions Programs/armstrong_between_intervals.cpp
Original file line number Diff line number Diff line change
@@ -1,26 +1,30 @@
#include <iostream>
#include <cmath>
using namespace std;

int main()
int main(void)
{
int num1, num2, i, num, digit, sum;
size_t start,end, i, currentDigit, digit, sum;
int num1,num2;

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++)
start = abs(num1);end = abs(num2);
cout << "Armstrong numbers between " << start << " and " << end << " are: " << endl;
for(i = start; i <= end; ++i)
{
sum = 0;
num = i;
currentDigit = i;

for(; num > 0; num /= 10)
while( currentDigit > 0 )
{
digit = num % 10;
sum = sum + digit * digit * digit;
digit = currentDigit % 10;
sum += pow(digit,3);
currentDigit /= 10;
}

if(sum == i)
Expand Down
10 changes: 5 additions & 5 deletions Programs/ascii.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ using namespace std;

int main()
{
char c;
cout << "Enter a character: ";
cin >> c;
cout << "ASCII Value of " << c << " is " << int(c);
return 0;
char c;
cout << "Enter a character: ";
cin >> c;
cout << "ASCII Value of " << c << " is " << int(c)<<endl;
return 0;
}
6 changes: 3 additions & 3 deletions Programs/bin_to_octal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,20 @@

using namespace std;

int convertBinarytoOctal(long long);
int BinarytoOctal(long long);
int main()
{
long long binaryNumber;

cout << "Enter a binary number: ";
cin >> binaryNumber;

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

return 0;
}

int convertBinarytoOctal(long long binaryNumber)
int BinarytoOctal(long long binaryNumber)
{
int octalNumber = 0, decimalNumber = 0, i = 0;

Expand Down
33 changes: 15 additions & 18 deletions Programs/check_prime.cpp
Original file line number Diff line number Diff line change
@@ -1,35 +1,32 @@
#include <iostream>
using namespace std;

bool checkPrimeNumber(int);
bool isPrimeNumber(long long);

int main() {
int n;
int main(void) {
long long input;

cout << "Enter a positive integer: ";
cin >> n;
cin >> input;

if (checkPrimeNumber(n))
cout << n << " is a prime number.";
if (isPrimeNumber( input ) )
cout << input << " is a prime number.";
else
cout << n << " is not a prime number.";
cout << input << " is not a prime number.";
return 0;
}

bool checkPrimeNumber(int n) {
bool isPrime = true;
bool isPrimeNumber(long long n) {

// 0 and 1 are not prime numbers
if (n == 0 || n == 1) {
isPrime = false;
// 0 or 1 or negative numbers are not prime numbers
if (n == 0 || n == 1 || n < 0) {
return false;
}
else {
for (int i = 2; i <= n / 2; ++i) {

for (long long i = 2; i <= n / 2; ++i) {
if (n % i == 0) {
isPrime = false;
break;
return false;
}
}
}
return isPrime;
return true;
}
6 changes: 3 additions & 3 deletions Programs/complex_num_subtr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class Complex
}

// Operator overloading
Complex operator - (Complex c2)
Complex operator - (Complex const &c2)
{
Complex temp;
temp.real = real - c2.real;
Expand All @@ -38,10 +38,10 @@ int main()
{
Complex c1, c2, result;

cout<<"Enter first complex number:\n";
cout<<"Enter first complex number\n";
c1.input();

cout<<"Enter second complex number:\n";
cout<<"Enter second complex number\n";
c2.input();

// In case of operator overloading of binary operators in C++ programming,
Expand Down
8 changes: 4 additions & 4 deletions Programs/convert_bin_to_dec.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,20 @@

using namespace std;

int convertBinaryToDecimal(long long);
int BinaryToDecimal(long long);

int main()
int main(void)
{
long long n;

cout << "Enter a binary number: ";
cin >> n;

cout << n << " in binary = " << convertBinaryToDecimal(n) << "in decimal";
cout << n << " in binary = " << BinaryToDecimal(n) << "in decimal";
return 0;
}

int convertBinaryToDecimal(long long n)
int BinaryToDecimal(long long n)
{
int decimalNumber = 0, i = 0, remainder;
while (n!=0)
Expand Down
15 changes: 10 additions & 5 deletions Programs/fact_recur.cpp
Original file line number Diff line number Diff line change
@@ -1,21 +1,26 @@
//working till 65
#include<iostream>
#include <cmath>
using namespace std;

int factorial(int n);
unsigned long long factorial(unsigned long long n);

int main()
{
int n;
long long input;

cout << "Enter a positive integer: ";
cin >> n;
cin >> input;

cout << "Factorial of " << n << " = " << factorial(n);
if(input <= 20)
cout << "Factorial of " << llabs(input) << " = " << factorial( llabs(input) );
else
cout << "Results are too long to calculate!";

return 0;
}

int factorial(int n)
unsigned long long factorial(unsigned long long n)
{
if(n > 1)
return n * factorial(n - 1);
Expand Down
9 changes: 7 additions & 2 deletions Programs/factorial.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,18 @@ using namespace std;

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

cout << "Enter a positive integer: ";
cin >> n;

if(n > 20){
cout << "Results are too long to calculate!";
return 0;
}

for(int i = 1; i <=n; ++i)
for(unsigned i = 1; i <=n; ++i)
{
factorial *= i;
}
Expand Down
2 changes: 1 addition & 1 deletion Programs/factors.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ using namespace std;

int main()
{
int n, i;
unsigned long long n, i;

cout << "Enter a positive integer: ";
cin >> n;
Expand Down
10 changes: 5 additions & 5 deletions Programs/fibb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,31 @@ using namespace std;

int main()
{
int n, t1 = 0, t2 = 1, nextTerm = 0;
unsigned long long 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 << " ";
cout <<" "<<t2 << " ";
continue;
}
nextTerm = t1 + t2;
t1 = t2;
t2 = nextTerm;

cout << nextTerm << " ";
}
}
return 0;
}
2 changes: 1 addition & 1 deletion Programs/gcd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ int main()
n2 -= n1;
}

cout << "HCF = " << n1;
cout << "GCF = " << n1;
return 0;
}
6 changes: 3 additions & 3 deletions Programs/hello_world.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// Your First C++ Program

#include <iostream>
using namsespace std;
using namespace std;

int main() {
cout << "Hello World!";
return 0;
}
}
2 changes: 1 addition & 1 deletion Programs/palindrom_num.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ int main()
{
digit = num % 10;
rev = (rev * 10) + digit;
num = num / 10;
num /= 10;
} while (num != 0);

cout << " The reverse of the number is: " << rev << endl;
Expand Down