Question 9: Write a program that asks the user to enter a series of single digit numbers with nothing separating them (Input as a string and validate it). The program should display the sum of all the singledigit numbers in the string. The program should also display the highest and lowest digits in the string.
Question 9: Write a program that asks the user to enter a series of single digit numbers with nothing separating them (Input as a string and validate it). The program should display the sum of all the singledigit numbers in the string. The program should also display the highest and lowest digits in the string.
/*
Name: Mudassar Ali
Roll: BCSF15M045 [Morning]
Assignment: No# 05
Teacher: Miss Sadia Shahzad
*/
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
cout<<"This programme will display the digit sum of string and show the highest and lowest digit!!!\n\n";
int str [20]={0}; // To store the digit of the input number string.
int len,num = 0,low,high=0,sum=0; // Variable for the requirements.
cout<<"Enter the number length of string:";
cin>>len; // Asking the length of the number string.
cout<<"Enter the digit series:\n"; // Inputting the number digits and storing after validating numbers digit.
for(int i = 0;i<len;i++)
{
cin>>num; // Inputting digits.
if(num>=0 && num<=9) // Validating the digits are single.
str[i] = num; // Storing number digit by digit in the string.
}
high = str[0]; // For Comparing the first index value for highest value.
low = str[0]; // For Comparing the first index value for lowest value.
for(int x = 0; x<len;x++)
{
sum += str[x]; // Summing up the string number digits.
if(x>0) // To Skip the loop for these statements.
{
if(high < str[x]) // Condition to check the highest value.
high = str[x];
if(low > str[x]) // Condition to check the lowest value.
low = str[x];
}
}
cout<<"Sum = "<<sum<<endl; // Printing of digit sum.
cout<<"\nHighest = "<<high<<endl; // Printing of highest value.
cout<<"\nlowest = "<<low; // Printing of lowest value.
return 0;
}
/*
Name: Mudassar Ali
Roll: BCSF15M045 [Morning]
Assignment: No# 05
Teacher: Miss Sadia Shahzad
*/
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
cout<<"This programme will display the digit sum of string and show the highest and lowest digit!!!\n\n";
int str [20]={0}; // To store the digit of the input number string.
int len,num = 0,low,high=0,sum=0; // Variable for the requirements.
cout<<"Enter the number length of string:";
cin>>len; // Asking the length of the number string.
cout<<"Enter the digit series:\n"; // Inputting the number digits and storing after validating numbers digit.
for(int i = 0;i<len;i++)
{
cin>>num; // Inputting digits.
if(num>=0 && num<=9) // Validating the digits are single.
str[i] = num; // Storing number digit by digit in the string.
}
high = str[0]; // For Comparing the first index value for highest value.
low = str[0]; // For Comparing the first index value for lowest value.
for(int x = 0; x<len;x++)
{
sum += str[x]; // Summing up the string number digits.
if(x>0) // To Skip the loop for these statements.
{
if(high < str[x]) // Condition to check the highest value.
high = str[x];
if(low > str[x]) // Condition to check the lowest value.
low = str[x];
}
}
cout<<"Sum = "<<sum<<endl; // Printing of digit sum.
cout<<"\nHighest = "<<high<<endl; // Printing of highest value.
cout<<"\nlowest = "<<low; // Printing of lowest value.
return 0;
}
No comments: