Program 1: Design a program in C which, when executed, will do the following in
ID: 3871520 • Letter: P
Question
Program 1:
Design a program in C which, when executed, will do the following in the indicated sequence:
Ask the user to type on the keyboard a user-supplied positive integer number nstart.
Ask the user to type on the keyboard a user-supplied larger ending positive integer number nstop.
The program will then compute for one time only, using the “for” looping command, the value of the function called zNo(nstart,nstop) which is defined as the sum of the squares of all the integers lying between and including nstart and nstop, i.e.,
zNo(nstart,nstop) = nstart2 + (nstart+1)2 + (nstart+2)2 + …+ (nstop-1)2 + nstop2.
As an example, let nstart=2 and nstop=5, then zNo(2,5)=22+32+42+52=4+9+16+25=54.
The program is then to display on the console the user-supplied values of nstart and nstop as well as the corresponding value of zNo(nstart,nstop) in the format shown below:
nstart=2 nstop=5 ====> zNo(2,5)=54.
For your sample output, use the values nstart=2 and nstop=5
Program 2:
Modify program 1 above so that it computes and then displays on the console computations of zNo(nstart,nstop) for user-specified values of nstart and nstop as in Program 1 but now in an infinite loop.
Do three sample outputs corresponding to the following values of nstart and nstop:
SAMPLE OUTPUT 1: nstart=1, nstop=3
SAMPLE OUTPUT 1: nstart=2, nstop=4
SAMPLE OUTPUT 1: nstart=3, nstop=5.
Program 3:
Modify program 1 above so that it computes and then displays on the console computations of zNo(nstart,nstop) for user-specified values of nstart and nstop as in Program 1 but now for a user-specified number of times denoted ntimes. For your sample output, use ntimes=3, with the three sets of values of nstart and nstop as in Program
Program 4:
Modify program 3 above so that it computes and then displays on the console computations of zNo(nstart,nstop) for user-specified values of nstart and nstop as in Program 3 but now for an arbitrary number of times.
Flowchart:
Draw a flowchart for Program 4.
Explanation / Answer
#include <stdio.h>
#include <stdlib.h>
int main()
{
int nstart,nstop ; //declaring integer variables to take input
printf("Asking user to enter nstart value ");
scanf("%d",&nstart); // taking input from user
if(nstart >= 0) // checking whether given input is postive
{
printf("Asking user to enter nstop value ");
scanf("%d",&nstop);
}
if(nstop >= 0) // checking whether given input is postive
{
int i,sum=0; // declaring integer i to run for loop and sum to store the squares of numbers
for(i = nstart; i<= nstop ;i++)
{
sum = sum+i*i; // caluculating sum of squares of numbers between nstart and nstop using for loop
}
printf("nstart = %d ",nstart);
printf("nstop = %d",nstop);
printf("======> zNo(%d,%d) = %d",nstart,nstop,sum);
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.