Write a program that will repeatedly ask the user to enter a floating-point numb
ID: 2080062 • Letter: W
Question
Write a program that will repeatedly ask the user to enter a floating-point number. the program should continue to ask the user to enter numbers until a zero or negative number is entered. At that point, the program should display the largest number entered. Example: the largest number entered was: 47.5 Write a program that will ask the user to enter an integer. the program should add all the integers from 1 to the entered number using a "while" loop, and display the sum. Show how the "while" loop in problem 3 can be re-written using a "for" loop instead. Write a program that includes a function called sprout () that returns the squareroot of a positive integer. in the function, use a loop to find the squareroot . If the number does not have an integer squareroot , the function should return a value of zero. in the main () function, ask the user to enter a positive integer, then call the function and display the result. If a zero is returned, it should display "No integer squareroot ".Explanation / Answer
2) Code is given below
#include <stdio.h>
int main(void) {
float n, large=0;
while(1) // The loop will continue till 0 is entered
{
printf(" Enter a number: "); // Prompts user to enter a number
scanf("%f",&n);
if(n>large)
large=n;
if(n==0) //When 0 is entered, the loop is exited
break;
}
printf("The largest number was: %f",large); // prints the result
return 0;
}
3) Code is given below
#include <stdio.h>
int main(void)
{
int n, sum=0, i=0;
printf("Enter an integer: ");
scanf("%d",&n);
while(i<=n){
sum=sum+i;
i++;
}
printf(" The sum of first %d numbers is %d ",n,sum);
return 0;
}
4) The code using for loop. In for loop
#include <stdio.h>
int main(void)
{
int n, sum=0, i;
printf("Enter an integer: ");
scanf("%d",&n);
for(i=0;i<=n;i++){
sum=sum+i;
}
printf(" The sum of first %d numbers is %d ",n,sum);
return 0;
}
5) The code is given below. The logic is that you add the first 'n' odd numbers till the sum equals the entered integer. Then 'n' will be the square root. Only integer square roots can be properly calculated here as required by the question
#include<stdio.h>
int sqroot(long int n) //Function to calculate the square root
{
long int root=0,x=0,i;
for(i=1;x<n;i+=2)
{
x = x + i;
root++;
}
if (root*root!=n)
return 0;
else
return root;
}
int main()
{
long int n,root;
printf("Enter an integer: ");
scanf("%ld",&n);
root=sqroot(n);
if(root==0)
printf(" No integer square root ");
else
printf("Square root of the integer is: %ld ",root);
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.