// Making a function that will take a integer as argument and return print the code
void Print(int statuscode){
// starting the switch statement on the interger sent
switch(statuscode)
case 200: // for case 200
cout << "Ok,Fullfilled"<< endl;
case 403: // for case 403
cout << "Forbidden" << endl;
case 404: // for case 404
cout << "Not Found" <<endl;
case 500: // for case 500
cout << "Server Error "<< endl;
default: // default case
cout << " Wrong code"<<endl;
}
Answer:
see explaination for program code
Explanation:
scalar_product = 0
li=[]
li2=[]
#reading numbers1.txt and numbers2.txt intoli and li2 respectively
with open('numbers1.txt') as n1, open('numbers2.txt') as n2:
for line1 in n1:
li.append(int(line1))
for line2 in n2:
li2.append(int(line2))
#storing min list size into variable l
a=len(li)
b=len(li2)
if a<b:
l=a
else:
l=b
#calculating scalar product
for i in range(l):
scalar_product=scalar_product+li[i]*li2[i]
print("scalar product is",scalar_product)
Answer:
The method sumDigits() and the test program is given in the explanation section below:
Explanation:
import java.util.Scanner;
public class num8 {
public static int sumDigits(long n){
long sum = 0;
while (n != 0)
{
sum = sum + n % 10;
n = n/10;
}
return (int)sum;
}
public static void main(String[] args) {
System.out.println("Enter a long integer");
Scanner in = new Scanner(System.in);
long digit = in.nextLong();
System.out.println(sumDigits(digit));
}
}
The steps required to implement this method are given in the question. A while is used to repeatedly extract and remove the digit until all the digits are extracted. In the main method, the Scanner class is used to prompt a user to enter a digit, the method is called and the digit is passed as an argument.