In: Computer Science
4. Construct a flowchart and the C ++ program that, receiving a four-digit integer as data, determines whether all the digits of the number are even. For example, if the number were 5688, it would not meet the condition since the most significant digit (5) is odd; if on the contrary, the number were 6244, it would be true, since all the digits are even.
Answer:
FLOW CHART:
I WROTE THE CODE ALONG WITH THE COMMENTS
CODE:
#include <iostream>
using namespace std;
int main()
{
//variables declaration.
int num,digit,flag=0,temp;
//scan input.
cout<<"Enter four digit number: ";
cin>>num;
temp=num;
while(temp) //conditon temp not equal to zero.
{
digit=temp%10; //take each digit.
if(digit%2==0) //conditon digit is even
{
temp=temp/10; //take remainig numbers after digit checking.
flag=1;
}
else
{
flag=0;
break;
}
}
if(flag==1) //conditon flag 1 means contain all even digits.
cout<<num<<" contain all even digits.";
else
cout<<num<<" contains odd digts.";
return 0;
}
OUTPUT:
SCREENSHOT OF THE CODE:
5. Make the flowchart and the C ++ program that, when receiving N integers as data, obtains only the sum of the positive numbers.
Answer:
FLOW CHART:
I WROTE THE CODE ALONG WITH THE COMMENTS
CODE:
#include <iostream>
using namespace std;
int main()
{
int N,sum=0,i; //variables declaration.
cout<<"Enter how many elements you want: ";
cin>>N;
int arr[N];
cout<<"enter elements: ";
for(i=0;i<N;i++) //for loop used to scan elements.
cin>>arr[i];
for(i=0;i<N;i++) //for loop used to traverse each element.
{
if(arr[i]>0) //conditon element is positive.
sum=sum+arr[i]; //calculate sum.
}
cout<<"Sum of positive integers: "<<sum; //print
sum.
return 0;
}
OUTPUT:
SCREENSHOT OF THE CODE: