Coen 243 Assignment 1
Coen 243 Assignment 1
Coen 243 Assignment 1
Problem 1:
#include <iostream>
using namespace std;
int main(){
long int number{0},m{1},z{0};
int count{0};
cout << "Enter any number or enter -1 to exit: ";
cin >> number;
if(number==-1){
return 0;
}
long int dummy = number;
if(dummy<0){
dummy*=-1;
}
else if(dummy == 0){
dummy = 1;
}
while(dummy >0){
dummy/=10;
count++;
}
unsigned int x{0},y{0};
double sum{0},product{1};
while(x<count){
y = x;
m=1;
while(y<(count-1)){
m*=10;
y++;
}
z = number/m;
number=number%m;
sum+=z;
product*=z;
x++;
}
if(sum<0){
sum=-sum;
}
if(product<0){
product = -product;
}
cout <<"The sum of digits is: "<< sum<<endl;
cout <<"The product of digits is: "<< product<<endl;
return main();
}
Problem 2:
#include <iostream>
using namespace std;
int main(){
long int number{0},m{1},z{0};
int count{0};
cout << "Enter any numer or enter -1 to exit: ";
cin >> number;
if(number==-1){
return 0;
}
if(number<0){
number = -number;
}
long int dummy = number;
if(dummy == 0){
dummy = 1;
}
while(dummy >0){
dummy/=10;
count++;
}
int x{0},y{0};
long int number1{0};
int i{0},j{0},k{0},l{0},n{0},o{0},p{0},q{0},r{0},s{0},d{0};
while(x<count){
y = x;
m=1;
while(y<(count-1)){
m*=10;
y++;
}
z = number/m;
number=number%m;
number1 = number;
if(z==0){
d++;
if(i==2){
s++;
}
}
else if(z==1){
i++;
if(i==2){
s++;
}
}else if(z==2){
j++;
if(j==2){
s++;
}
}else if(z==3){
k++;
if(k==2){
s++;
}
}else if(z==4){
l++;
if(l==2){
s++;
}
}else if(z==5){
n++;
if(n==2){
s++;
}
}else if(z==6){
o++;
if(o==2){
s++;
}
}else if(z==7){
p++;
if(p==2){
s++;
}
}else if(z==8){
q++;
if(q==2){
s++;
}
}else if(z==9){
r++;
if(r==2){
s++;
}
}x++;
}
if(s>=2){
cout <<"The digits: ";
}
else{
cout <<"The digit: ";
}
if(d>=2){
cout<<"0 ";
}if(i>=2){
cout<<"1 ";
}if(j>=2){
cout<<"2 ";
}if(k>=2){
cout<<"3 ";
}if(l>=2){
cout<<"4 ";
}if(n>=2){
cout<<"5 ";
}if(o>=2){
cout<<"6 ";
}if(p>=2){
cout<<"7 ";
}if(q>=2){
cout<<"8 ";
}if(r>=2){
cout<<"9 ";
}
if(s>=2){
cout <<"are repeated"<<endl;
}
else{
cout <<"is repeated"<<endl;
}
return main();
}
Problem 3:
#include <iostream>
using namespace std;
int main(){
double number{0};
cout<< "Enter a positve number or enter -1 to exit: ";
cin>>number;
if(number==-1){
return 0;
}
while(number<0){
cout<< "Invalid number! Please enter a positive number: ";
cin>>number;
}
int x{1};
double fac{1};
while(x<=number){
fac*=x;
x++;
}
cout<<"The factorial of number "<<number<<" is "<<fac<<endl;
return main();
}
Problem 4:
#include <iostream>
using namespace std;
int main(){
double number{0};
cout<< "Enter a positve number or enter -1 to exit: ";
cin>>number;
if(number == -1){
return 0;
}
while(number<0){
cout<< "Invalid number! Please enter a positive number: ";
cin>>number;
}
int x{1};
double sum{1};
while(x<=number){
sum*=x;
x+=2;
}
cout<<"The product of all odd numbers smaller than or equal to "<< number<<" is "<< sum<<endl;
return main();
}
Problem 5:
#include <iostream>
using namespace std;
int main(){
double amount1{0},amount2{0};
int number1{0};
int diff{0};
int x{0},y{0},z{0},i{0},j{0};
cout<<"Enter the total amount of the bill or enter -1 to exit: ";
cin>>amount1;
if(amount1 == -1){
return 0;
}
while(amount1<0){
cout<<"Invalid number! Please enter a valid amount of the bill: ";
cin>>amount1;
}
cout<<"Enter the amount paid by the customer: ";
cin>>amount2;
while(amount2<0){
cout<<"Invalid number! Please enter a valid amount paid by the customer: ";
cin>>amount2;
}
diff=amount2-amount1;
if(diff<0){
diff=-diff;
}
number1=diff%100;
if(number1==diff){
number1=diff;
}
else{
x=(diff-number1)/100;
diff=diff-x*100;
}
number1=diff%20;
if(number1==diff){
number1=diff;
}
else{
y=(diff-number1)/20;
diff=diff-y*20;
}
number1=diff%10;
if(number1==diff){
number1=diff;
}
else{
z=(diff-number1)/10;
diff=diff-z*10;
}
number1=diff%5;
if(number1==diff){
number1=diff;
}
else{
i=(diff-number1)/5;
diff=diff-i*5;
}
number1=diff%1;
if(number1==diff){
number1=diff;
}
else{
j=(diff-number1)/1;
diff=diff-j*1;
}
if((amount2-amount1)>=0){
cout<<"You should return to the customer the following:"<<endl;
}
if((amount2-amount1)<0){
cout<<"The customer owned you the following: "<<endl;
}
cout<<"100$ bills: "<<x<<endl;
cout<<"20$ bills: "<<y<<endl;
cout<<"10$ bills: "<<z<<endl;
cout<<"5$ bills: "<<i<<endl;
cout<<"1$ bills: "<<j<<endl;
return main();
}