Question

CODE MUST BE IN C 1. Given an int variable n that has been initialized to...

CODE MUST BE IN C

1. Given an int variable n that has been initialized to a positive value and, in addition, int variables k and total that have alreadybeen declared, use a while loop to compute the sum of the cubes of the first n whole numbers, and store this value in total. Thus if n equals 4, your code should put 1*1*1 + 2*2*2 + 3*3*3 + 4*4*4 into total. Use no variables other than n, k, and total.

2. Given an int variable n that has already been declared and initialized to a positive value, and another int variable j that has already been declared, use a do...while loop to print a single line consisting of n asterisks. Thus if n contains 5, five asterisks will be printed. Use no variables other than n and j.

3. Given int variables k and total that have already been declared, use a do...while loop to compute the sum of the squares of the first 50 positive integers and store this value in total. Thus your code should put 1*1 + 2*2 + 3*3 +... + 49*49 + 50*50 into total. Use no variables other than k and total.

4. Given an int variable n that has been initialized to a positive value and, in addition, int variables k and total that have alreadybeen declared, use a do...while loop to compute the sum of the cubes of the first n whole numbers, and store this value in total. Thus if n equals 4, your code should put 1*1*1 + 2*2*2 + 3*3*3 + 4*4*4 into total. Use no variables other than n, k, and total.

5.

Given that two int variables, total and amount, have been declared, write a sequence of statements that:

  • initializes total to 0
  • reads three values into amount, one at a time.

After each value is read in to amount, it is added to the value in total (that is, total is incremented by the value in amount).

6. Given that two int variables, total and amount, have been declared, write a loop that reads non-negative values into amountand adds them into total. The loop terminates when a value less than 0 is read into amount. Don't forget to initialize total to 0.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Note: Could you plz go through this code and let me know if u need any changes in this.Thank You
=================================

1)

#include <stdio.h>
#include <math.h>


int main() {
  
int n=4,k=1,total=0;
  
while(k<=4)
{
   total+=pow(k,3);
   k++;
   }
  
   printf("Sum of cubes :%d",total);
  
   return 0;
  
}

===========================

output:

==============================

2)

#include <stdio.h>
#include <math.h>


int main() {
  
int n=4,j=0;
  
do
{
   printf("*");
   j++;
  
   }while(j!=n);
  
   return 0;
  
}

============================

Output:

===========================

3)

#include <stdio.h>
#include <math.h>


int main() {
  
int k=1,total;

do
{
    total=k*k;
    k++;
}while(k!=50);  

printf("Sum :%d",total);
   return 0;
  
}
================================

Output:

===============================

4)

#include <stdio.h>
#include <math.h>


int main() {
  
int n=4,k=1,total=0;
  
while(k<=4)
{
   total+=pow(k,3);
   k++;
   }
  
   printf("Sum of cubes :%d",total);
  
   return 0;
  
}

===========================

output:

============================

5)

#include <stdio.h>
#include <math.h>


int main() {
int total=0,amount;
  
printf("Enter first value :");
scanf("%d",&amount);
  
total+=amount;
  
printf("Enter first value :");
scanf("%d",&amount);
  
total+=amount;

printf("Enter first value :");
scanf("%d",&amount);
  
total+=amount;
  
   printf("Total :%d",total);
  

   return 0;
  
}

================================

Output:

===============================

6)

#include <stdio.h>
#include <math.h>


int main() {
int total=0,amount;
  
printf("Enter number :");
scanf("%d",&amount);
while(amount>0)
   {
total+=amount;
     
   printf("Enter number :");
scanf("%d",&amount);      
   }   

printf("Total :%d",total);


   return 0;
  
}

=============================

Output:

=====================Could you plz rate me well.Thank You

Add a comment
Know the answer?
Add Answer to:
CODE MUST BE IN C 1. Given an int variable n that has been initialized to...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT