Question

In: Computer Science

Q1) Write programs with iterators that computes: • The sum of all even numbers between 2...

Q1) Write programs with iterators that computes:

• The sum of all even numbers between 2 and 100 (inclusive).

• The sum of all squares between 1 and 100 (inclusive).

• The product of all powers of 2 from 20 up to 220 .

• The product of all odd numbers between a and b (inclusive), where a and b are inputs. sum would be 3 + 7 + 7 = 17.)

Q2) Recall the Fibonacci sequence: 1, 1, 2, 3, 5, 8, 13, . . .

In general we can generate Fibonacci-like sequences by making linear combinations of the formula that gives us the nth term of the sequence.

Consider the following general case for generating Fibonacci-like sequences:

F1 = 1

F2 = 1

Fn = aFn−1 + bFn−2

where a, b are integers. Write a function that given values a, b and n will print a Fibonacci-like sequece up to the nth term of the squence.

(eg, if a = 1 and b = 2 the resulting sequence is 1, 1, 3, 5, 11, 21, 43 . . .)

Solutions

Expert Solution

*note: Since no particular language is mentioned, I have written code in C++

Ans 1: a.

#include<iostream>
using namespace std;
int main(){
int total=0;
for(int i=2;i<=100;i++){
if(i%2==0){
total=total+i;
}
}
cout<<"Sum of even number between 2 and 100 is :"<<total;
return 0;
}
Ans 1. b.

#include<iostream>
using namespace std;
int main(){
int total=0;
for(int i=1;i<=10;i++){
total=total+(i*i);
}
cout<<"sum of all squares between 1 and 100 is :"<<total;
return 0;
}

Ans 1. c.

#include<bits/stdc++.h>
using namespace std;
int main(){
int j=4,prod=1,i=1;
while(pow(2,j)<=220){
i=pow(2,j);
if(i>20){
cout<<i<<endl;
prod=prod*i;
}
j++;
}
cout<<"product of all powers of 2 from 20 up to 220 :"<<prod;
return 0;
}
Ans 1. d.

#include<bits/stdc++.h>
using namespace std;
int main(){
int a,b,prod=1;
cout<<"Enter value of a and b :";
cin>>a>>b;
for(int i=a;i<=b;i++){
if(i%2==1){
prod=prod*i;
}
}
cout<<"The product of all odd numbers between a and b :"<<prod;
return 0;
}

Ans 2.

#include <iostream>
using namespace std;
int fib(int n,int a,int b) {
if((n==1)||(n==0)) {
return(n);
}else {
return((a*fib(n-1,a,b))+(b*fib(n-2,a,b)));
}
}
int main() {
int n,i=1,a,b;
cout<<"Enter the value of a and b :";
cin>>a>>b;
cout<<"Enter the length for series: ";
cin>>n;
cout<<endl<<"Series :";
while(i<=n) {
cout<<" "<<fib(i,a,b);
i++;
}
return 0;
}


Related Solutions

Write assembly instructions that compute the following: The sum of all even numbers between 2 and...
Write assembly instructions that compute the following: The sum of all even numbers between 2 and 100 (inclusive) -- the answer should be 2550 Prompt the user for 2 values, put them in variables a and b, then find the sum of all odd numbers between a and b. The sum of all the squares between 1 and 100 (inclusive) . -- the answer should be 338350 All of the statements above should print the answers using print_int MUST BE...
Write a loop that will calculate the sum of all even numbers from 2 to 30...
Write a loop that will calculate the sum of all even numbers from 2 to 30 ( including 30) store the result in the variable called thirtySum. Declare and initialize all variables. Answer using programming in c.
Write a program in C++ that computes the sum of odd numbers between 1 and 117....
Write a program in C++ that computes the sum of odd numbers between 1 and 117. Execute the program and submit a screen capture of the program and its results.
Write a function that takes two integer inputs and returns the sum of all even numbers...
Write a function that takes two integer inputs and returns the sum of all even numbers between these inputs, and another function that takes two integer inputs and returns the sum of odd numbers between these inputs .In main function, the program will asks the user to enter two integer numbers and then passes them to these two functions and display the result of each of them
Input 10 integers and display the following: a. the sum of even numbers. b. the sum...
Input 10 integers and display the following: a. the sum of even numbers. b. the sum of odd numbers. c. the largest integer d. the smallest integer e. the total count of even numbers f. the total count of odd numbers. Using C++ program with for loop..
Write a Python program that calls a function to sum all the numbers in a list...
Write a Python program that calls a function to sum all the numbers in a list and returns the result to the caller. The main program creates a list (with hard-coded or user input) and passes the list as an argument to the function. You may not use the built-in function, sum. The program calls a second function to multiply all the numbers in a list passed to it by main and returns the product back to the caller. List...
Write a MIPS program that asks the user for 2 numbers. Output the sum of the...
Write a MIPS program that asks the user for 2 numbers. Output the sum of the 2 numbers. The difference between the 2 numbers (num1-num2) and (num2-num1) The value that is 305 more than the 1st number. The value that is 305 less than the 2nd number
Sum of numbers between 1 and 10
Calculate the values from the  smallest number to the largest number
Write a JavaScript program that computes the average of a collection of numbers and then outputs...
Write a JavaScript program that computes the average of a collection of numbers and then outputs the total number of values that are greater than the average. An A grade is any score that is at least 20% greater than the average. The B grade is any score that is not an A, but is at least 10% greater than the average. An F grade is any score that is at least 20% less than the average. The D grade...
Write a program that generates all prime numbers between 2 and 1000, using the Sieve of...
Write a program that generates all prime numbers between 2 and 1000, using the Sieve of Eratosthenes method. You can find many articles that describe the method for finding primes in this manner on the Internet. Display all the prime values. This program should be in assembly language.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT