Using your favorite editor, write a Script file and save it as myScript.sh that
ID: 3808558 • Letter: U
Question
Using your favorite editor, write a Script file and save it as myScript.sh that asked the users to input a number between 100 and 1000. Use the case statement to do the following:
1. If the number is an odd number then add all the even numbers in between 1 and the number using a for loop structure.
2. If the number is an even number then add all the odd numbers in between 1 and the number using a while loop structure.
3. If the number is divisible by 3 then add all the numbers in between 1 and the number using a until loop structure.
Explanation / Answer
//gcc 5.4.0
#include <stdio.h>
int main()
{
int ind;
int i=1,n;
int sum=0;
printf("Enter Number");
scanf("%d",&n);
if(n>=100 && n<=1000)
{
ind=getIndicator(n);
switch(ind)
{
case 0:
for(i=1;i<=n;i++)
{
if(i%2==0)
{
sum=sum+i;
}
}
printf("sum of all even numbers is:%d",sum);
break;
case 1:
while(i<=n)
{
if(i%2!=0)
{
sum=sum+i;
i++;
}
}
printf("sum of all odd numbers is:%d",sum);
break;
case 3:
do
{
sum=sum+i;
}while(i<=n);
printf("sum of all numbers is:%d",sum);
break;
}
}
else
{
printf("Enter a number within 100 and 1000");
}
}
int getIndicator(int number)
{
int Indicator;
if (number%2==0)
{
Indicator=1;
}
else if (number%2!=0)
{
Indicator=0;
}
else if(number%3==0)
{
Indicator=3;
}
return Indicator;
}
Please save it in Script shell format and run your applicaiton
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.