I need to write a program that will get 2 integers. The program will then display the
following:
a. The sum of all integers that are both divisible by 4 and 6 between the two numbers inputted.
b. The average of all integers both divisible by 3 and 7 between the two numbers inputted.
I tried doing the code down below:
#include <iostream>
using namespace std;
int Sum46(int a, int b)
{
int sum = 0;
for (int i = a; i <= b; i++) {
if (i % 4 == 0 && i % 6 == 0) {
sum = sum + i;
}
}
return sum;
}
int Sum37(int j, int k)
{
int sum1 = 0;
for (int i = j; i <= k; i++) {
if (i % 3 == 0 && i % 7 == 0) {
sum1 = sum1 + i;
}
}
return sum1;
}
int Count(int f, int g)
{
int inputCount = 0;
for (int i = f; i < g; i++) {
if (i % 3 == 0 && i % 7 == 0) {
inputCount++;
}
}
return inputCount;
}
int Ave(int t, int u)
{
int total = 0;
total = t / u;
return total;
}
void display(int o, int l)
{
cout << "The sum of all integers that are both divisible by 4 and 6 between two numbers is " << o << endl;
cout << "The sum of all integers that are both divisible by 3 and 7 between two numbers is " << l << endl;
}
int main()
{
int num1, num2;
int probA, probB, comp, bilang;
cout << "Input first number : ";
cin >> num1;
cout << "Input second number : ";
cin >> num2;
probA = Sum46(num1, num2);
comp = Sum37(num1, num2);
bilang = Count(num1, num2);
probB = Ave(comp, bilang);
display(probA, probB);
return 0;
}
My expected result should be
Input first number : 4
Input second number : 12
The sum of all integers that are both divisible by 4 and 6 between two numbers is 12
The sum of all integers that are both divisible by 3 and 7 between two numbers is 0
"since 12 is the only number that is both divisible by 4 and 6 and no number is divisible by 3 and 7 on the inputted numbers."
But the actual results are
Input first number : 4
Input second number : 12
"That is the only result and I was not able to produce any output of the functions"
You have to check for the case where u is 0.
Change this:
int Ave(int t, int u)
{
int total = 0;
total = t / u;
return total;
}
To this:
int Ave(int t, int u)
{
if( u == 0 )
return -1;
return t/u;
}
Related
#include<bits/stdc++.h>
using namespace std;
int main() {
int n,m,z;
cout<<"enter n: ";
cin>>n;
z=n;
int count=0;
while(n>0){
m = n % 10;
if(z%m == 0){
count++;
}
n=n/10;
}
cout<<count;
}
Code should work like that ex - for n = 12, it is divisible by both 1 , 2 so, the output will be 2
if i am taking any value which have '0' in their last then it is not working ..and i am getting an error "Floating-point exception (SIGFPE)".
Could anyone help me to get rid out of this.
This while loop
while(n>0){
m = n % 10;
if(z%m == 0){
count++;
}
n=n/10;
}
does not make a great sense. For example m can be equal to 0 after this statement
m = n % 10;
and as a result this statement
if(z%m == 0){
produces a run-time error.
The program can look for example the following way
#include <iostream>
int main()
{
unsigned int count = 0;
int n;
std::cout << "enter n: ";
if ( std::cin >> n )
{
const int Base = 10;
int tmp = n;
do
{
int digit = tmp % Base;
if ( digit != 0 && n % digit == 0 ) ++count;
} while ( tmp /= Base );
}
std::cout << "count = " << count << '\n';
}
For this program, I will input a Binary number and it will convert into a decimal number. At the end I wanted to return the number of digits in the Binary number that I had input. For example, 1001 - 4 Binary digits.The output of the digits of Binary number is always 0. Should I use size_type to do it ?
#include<iostream>
#include<string>
#include<bitset>
#include<limits>
#include<algorithm>
using namespace std;
int multiply(int x);
int multiply(int x)
{
if (x == 0)
{
return 1;
}
if (x == 1)
{
return 2;
}
else
{
return 2 * multiply(x - 1);
}
}
int count_set_bit(int n)
{
int count = 0;
while (n != 0)
{
if (n & 1 == 1)
{
count++;
}
n = n >> 1;
}
return count;
}
int main()
{
string binary;
cout << "\n\tDualzahlen : ";
cin >> binary;
reverse(binary.begin(), binary.end());
int sum = 0;
int size = binary.size();
for (int x = 0; x < size; x++)
{
if (binary[x] == '1')
{
sum = sum + multiply(x);
}
}
cout << "\tDezimal : " << sum << endl;
int n{};
cout << "\tAnzahl der Stelle : " << count_set_bit(n) << endl;
}
It looks like you are on the right track for unsigned integers. Signed integers in a multiply are generally converted to positive with a saved result sign.
You can save some time with a bit data solution, as value testing is not cost free plus 'and' has no carry delay. Of course, some CPUs can ++ faster than += 1, but hopefully the compiler knows how to use that:
int bits( unsigned long num ){
retVal = 0 ;
while ( num ){
retVal += ( num & 1 );
num >>= 1 ;
}
return retVal ;
}
I recall the H-4201 multiplied 2 bits at a time, using maybe shift, maybe add, maybe carry/borrow, so 0 was no add, 1 was add, 2 was shift and add, 3 was carry/borrow add (4 - 1)! That was before IC multiply got buried in circuits and ROMs. :D
I was trying to solve this problem with the following code. But the answers aren't accurate for all inputs.
Problem Statement
Ikbal has two arrays a and b of length N, initially all values equals to zero. We have Q operation. Let's define three types of operations on this arrays:
1 l r c Increase al,al+1,...,ar by c.
2 l r c Increase bl,bl+1,...,br by c.
3 l r Print (al∗bl)+(al+1∗bl+1)+...+(ar∗br) in modulo 1000000007
Input Format
First line of the input consists of N and Q. Next Q lines contain one of the three types of operations.
Constraints
1≤N≤109
1≤Q≤200000
1≤c≤10000
1≤l≤r≤N
Output Format
Whenever you get a type 3 operation, you should print the answer in a new line.
Sample Input
5 3
1 1 5 5
2 2 4 2
3 3 4
Sample Output
20
Explanation
After first operation arrays look like this:
a=5,5,5,5,5
b=0,0,0,0,0
After second operation arrays look like this:
a=5,5,5,5,5
b=0,2,2,2,0
Answer of the third operation: 5∗2+5∗2=20
**MY code **
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
vector<int> a,b,c;
int n,q,r,p;
cin >> n;
cin >> q;
for(int i=0;i<q;i++) {
cin >> r;
a.push_back(r);
if(r==3) {
p = 3;
} else {
p = 4;
}
for(int j=1;j<p;j++) {
cin >> r;
a.push_back(r);
}
}
vector<int> aa(n,0),bb(n,0);
int g,start,endd,val,anss=0;
for(int i=0;i<a.size();) {
if(a[i]==3) {
start = a[i+1]-1;
endd = a[i+2]-1;
if(start==endd) {
anss = (aa[start]*bb[start])%1000000007;
} else {
anss = (aa[start]*bb[start] + aa[endd]*bb[endd])%1000000007;
}
cout << anss << endl;
i+= 3;
} else {
start = a[i+1] - 1;
endd = a[i+2];
val = a[i+3];
if(a[i]==1) {
for(int j=start;j<endd;j++) {
aa[j] += val;
}
} else {
for(int j=start;j<endd;j++) {
bb[j] += val;
}
}
i+= 4;
}
}
/*
for(int i=0;i<n;i++) {
cout << aa[i] << " " ;
cout << bb[i] << endl;
}
for(int i=0;i<a.size();i++) {
cout << a[i] << endl;
} */
return 0;
}
Expected output for given input :
http://i.stack.imgur.com/4OsSo.jpg
It's not an overflow problem. This:
if(start==endd)
anss = (aa[start]*bb[start])%1000000007;
else
anss = (aa[start]*bb[start] + aa[endd]*bb[endd])%1000000007;
is flat wrong. You misread the instructions.
Be careful not to cause overflow.
You have to calculate (al∗bl)+(al+1∗bl+1)+...+(ar∗br), not (al∗bl)+(ar∗br)
At least, the output for the given input
10 20
1 9 9 6768
2 5 5 2202
3 7 7
2 3 9 1167
2 1 7 8465
3 1 5
2 1 1 1860
3 9 9
2 5 5 2153
1 5 7 749
3 1 1
2 8 10 3129
3 1 1
1 2 10 2712
2 1 8 79
1 1 6 4645
1 7 7 1358
3 2 10
1 9 9 8677
3 8 10
is corrected.
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int add(int a, int b) {
int r = a + b;
if (r >= 1000000007) r -= 1000000007;
return r;
}
int mul(int a, int b) {
int r = 0;
while (b > 0) {
if (b % 2 != 0) r = add(r, a);
a = add(a, a);
b /= 2;
}
return r;
}
int main() {
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
vector<int> a,b,c;
int n,q,r,p;
cin >> n;
cin >> q;
for(int i=0;i<q;i++) {
cin >> r;
a.push_back(r);
if(r==3) {
p = 3;
} else {
p = 4;
}
for(int j=1;j<p;j++) {
cin >> r;
a.push_back(r);
}
}
vector<int> aa(n,0),bb(n,0);
int g,start,endd,val,anss=0;
for(int i=0;i<a.size();) {
if(a[i]==3) {
start = a[i+1]-1;
endd = a[i+2]-1;
#if 0
if(start==endd) {
anss = (aa[start]*bb[start])%1000000007;
} else {
anss = (aa[start]*bb[start] + aa[endd]*bb[endd])%1000000007;
}
#else
anss = 0;
if (start <= endd) {
for(int j=start;j<=endd;j++)anss = add(anss, mul(aa[j], bb[j]));
} else {
for(int j=endd;j<=start;j++)anss = add(anss, mul(aa[j], bb[j]));
}
#endif
cout << anss << endl;
i+= 3;
} else {
start = a[i+1] - 1;
endd = a[i+2];
val = a[i+3];
if(a[i]==1) {
for(int j=start;j<endd;j++) {
#if 0
aa[j] += val;
#else
aa[j] = add(aa[j], val);
#endif
}
} else {
for(int j=start;j<endd;j++) {
#if 0
bb[j] += val;
#else
bb[j] = add(bb[j], val);
#endif
}
}
i+= 4;
}
}
/*
for(int i=0;i<n;i++) {
cout << aa[i] << " " ;
cout << bb[i] << endl;
}
for(int i=0;i<a.size();i++) {
cout << a[i] << endl;
} */
return 0;
}
I think I've almost got it, but I feel like I'm go in circles trying to figure this out.
The challenge to out cout without using strings or arrays. I took the number 56 as an example and 56 should equal 111000 this is not the case as it goes through fine till 7 then the number equals number*2 + number%2 makes it equal to 15 and outputs all 1's. Idk anymore, this is driving me to the moon and back.
#include <iostream>
using namespace std;
int main()
{
int number = 0;
int n = 1;
int x = n;
cin>>number;
cout<<n%2;
while(n <= number)
{
if(n%2 == 0)
{
n = n*2;
cout<<0;
}
else
{
n = n*2 + n%2;
cout<<n%2;
}
}
}
You can use the binary operator & to check if a single bit is 1 or 0.
for (int i=512; i>0; i/=2) {
cout << ( ( number & i ) != 0 ) ;
}
Note that this WILL print leading 0's.
Also, I'm assuming you only want to print positive integers.
Alternative:
for (int i=512; i>0; i/=2) {
if (number >= i) {
cout << 1;
number -= i;
} else {
count << 0;
}
}
You can use recursion
void decimal_to_binary(int decimal)
{
int remainder = decimal % 2;
if (decimal < 1)
return;
decimal_to_binary(decimal / 2);
cout << remainder;
}
This function will take the decimal, get its remainder when divided to 2. Before it the function call itself again, it checks if the decimal is less than 1(probably 0) and return to execute the printing of 1's and 0's
I had this type of problem assigned to me recently. This code example work up to a maximum of 10 binary digits (per the problem guidelines) and keep prompting for input until 0 is entered (sentinel value). This can certainly be improved but the math is correct:
#include <iostream>
#include <cmath>
using namespace std;
int main ()
{
//Declare Variables
int inputValue = 0;
int workingValue = 0;
int conversionSum = 0;
//Begin Loop
do{
//Prompt for input
cout << "Enter a binary integer (0 to quit): ";
cin >> inputValue;
//Reset Variables
workingValue = inputValue;
conversionSum = 0;
//Begin processing input
//10 digits max, so 10 iterations
for (int i=0; i<10; i++) {
//Check for non-binary entry
if ((workingValue % 10) != 1 && (workingValue % 10 != 0)){
cout << "Invalid!\n";
workingValue = 0;
conversionSum = 0;
break;
}
//check to see if 2^i should be added to sum
if (workingValue%2 == 1){
conversionSum += pow(2,i);
workingValue--;
}
//divide by 10 and continue loop
workingValue= workingValue / 10;
}
//output results
cout << "converted to decimal is: " << conversionSum << endl;
}while (inputValue != 0);
}
#include<iostream>
#include<cmath>
using namespace std;
int main()
{
cout << "enter a number";
int number, n, a=0;
cin >> number;
n = number;
do
{
n=n/2;
a=a+1;
}
while (n>=1);
cout << "a is" << a;
int c = a;
int b = a;
cout << "binary is";
for(int i=0; i<=c; i++)
{
int k = number / pow(2,b);
cout << k;
number = number - k * pow(2,b);
b = b-1;
}
return 0;
}
Although asked in C I have used C++. I have used the logic that if you have to convert decimal to binary we have to find the maximum power of 2 contained in the number which when added by 1 becomes the number of digit of required binary .. leftmost digit is the number of highest available power of 2 (ex in 8 highest power of 2 is 3 and 1 such is available)...then subtract this from the number and (ex 8-8=0)and search for number of next highest available power of 2 and so on.
I'm doing an online challenge and the challenge is the following:
"Kids are playing a game called "Counting digits". For given numbers S and K, they firstly write all numbers between those numbers and then count how many times each digit appears (0,1,2,3,4,5,6,7,8,9). For example, S=767, K=772, numbers will be: 767,768,769,770,771,772
So, 0 will show once (in 770), 1 will show once (in 771) and so on..
Basically, my program have to do the following (given example):
Input:
1 9
(These are numbers 1,2,3,4,5,6,7,8,9)
Output:
0 1 1 1 1 1 1 1 1 1
(0 doesn't show, other numbers show once)."
I'm stuck on this code... out of ideas.
#include <iostream>
using namespace std;
int main()
{
int s,k;
int array[10];
int c0=0,c1=0,c2=0,c3=0,c4=0,c5=0,c6=0,c7=0,c8=0,c9=0;
cin >> s >> k;
int saves = s;
int savek = k;
cout << s%10;
for(int i=s;i<=k;i++)
{
int savei=i;
while(savei!=0)
{
savei=savei%10;
}
}
Any pseudo code/snippet/code/hint is appreciated.
Purely numeric solution to a purely numeric problem:
#include <iostream>
int main()
{
int s, k, i, tmp;
std::cin >> s >> k;
int count[10] = { 0 };
for (i = s; i <= k; i++) {
tmp = i;
do {
count[tmp % 10]++;
tmp /= 10;
} while(tmp);
}
for (i = 0; i < 10; i++) {
std::cout << i << " appears " << count[i] << " times" << std::endl;
}
return 0;
}
My solution is like this:
int main(){
int s,k;
cin >> s >> k;
int numbers[10]={0};
string sum;
for(int i=s;i<=k;i++)
{
sum=to_string(i);
for(int i=0;i<sum.length();i++){
numbers[(int)sum.at(i)-48]++;
}
}
for(int i=0;i<10;i++){
cout<<numbers[i]<<endl;
}
return 0;
}
public static void getDigitsInBook(int n) {
for(int i=0;i<10;i++) {
int x = n,val=0,k=1;
while(x!=0) {
int left = x/10;
int num = x%10;
int right = n%k;
if(i == 0) {
val = val+ (left*k);
}
else if(i<num) {
val = val + ((left+1)*k);
}
else if(i==num) {
val = val + (left*k) + right+1;
}
else {
val = val+ (left*k);
}
k=k*10;
x = n/k;
}
System.out.println(val);
}
}
What you usually do with such tasks is calculating the number between 0 and S and between 0 and K and subtracting those.
How many are between 0 and 767? First count the numbers of the last digit. There are 77 times 0, 1, 2, 3, 4, 5, 6, 7 each and 76 times 8 and 9. More formally, 767/10+1 between 0 and 767%10 and 767/10+1 on the rest. Then calculate the number of occurences of the last digit for 767/10=76, multiply by 10, add 7 times 7 and 6 (for the error on the last one) and do the same for the remaining digits, here 76/10=7. Finally, add the results up.
This solves the problem in O(log_10 K).
try this code:
for(int n=s ; n<=k ; n++)
{
tempN = abs(n);
while(tempN > 0)
{
tempDigit = tempN % 10;
tempN /= 10;
//count tempDigit here
}
}
assuming your variables are ints, "tempN /= 10;" should be no problem.