Question

I need this written in both shell script and C. Assume that it goes on infinitely...

I need this written in both shell script and C. Assume that it goes on infinitely

Program 3: nested loop. e.g. 1*2 + 2*3 + 3*4 + ...(n-1)*n  (Only nested loops)

Homework Answers

Answer #1

C Program:

#include <stdio.h>

int main(void) {
  printf("Enter Size(N) : ");
  int N, sum = 0;
  scanf("%d", &N);

  for (int i=0; i<=N-1; i++)
  {
    for (int j=i+1; j<=N; j++)
    {
      if ((j-i) == 1)
      {
        sum += (i*j);
      }
    }
  }

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

Shell Script Program:

echo "Enter Size(N)"
read N

i=1
sum=0

for (( i = 1; i <= N-1; i++ ))      ### Outer for loop ###
do
    for (( j = i+1 ; j <= N; j++ )) ### Inner for loop ###
    do
    if [ $((j - i)) -eq 1 ]
    then
          sum=$((sum + (i * (j))))
    fi
    done
    
done

echo "Sum : "$sum

Output:

Thumbs Up Please !!!

Know the answer?
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
Similar Questions
C++ PROBLEM: Create a program that calculates the hyperfactorial of any positive integer n, where the...
C++ PROBLEM: Create a program that calculates the hyperfactorial of any positive integer n, where the hyperfactorial is equivalent to value obtained by the operation: 1^1*2^2*3^3*…..n^n . If user inputs the value that is not a positive value, display a message informing the user that the input is not valid and request a new input. Calculate the hyperfactorial first by creating a program that uses only nested “For loop” structure.
1- Create a new script that is called HW9.m and save it. The script will do...
1- Create a new script that is called HW9.m and save it. The script will do the following. Test your code for each of the cases and upload your script as a .m file. [15 points] a. Create a random integer numbers that goes from 0 to 5 and assign it to a variable y using rand and round commands. Hint: Note that rand function only creates random number between 0 and 1. You need to scale the values to...
I only need question (C) answered. I have written out the previous two questions with their...
I only need question (C) answered. I have written out the previous two questions with their answers so that it can assist you in the question (C) THANK YOU (a) Calculate the angular momentum of the Moon due to its orbital motion about Earth. In your calculation use 3.84 ✕ 108 m as the average Earth-Moon distance and 2.36 ✕ 106 s as the period of the Moon in its orbit. (Use 7.36 ✕ 1022 kg for the mass of...
Write a C++ Program to print the first letter of your first and last name using...
Write a C++ Program to print the first letter of your first and last name using stars. Note: 1) Using nested For Loop 2) The number of lines is given by user. 3) Using one Outer loop to print your letters. 4) Print the letters beside each other my name's Aya amro
Java Script I need the code to produce these 3 output. I can only get it...
Java Script I need the code to produce these 3 output. I can only get it to produce input #3 I need it to produce the following: // Input #1: // http://www.example.com // Output // http://www.example.com // Input #2: // http://www.example.com?name=r2d2 // Output // http://www.example.com // name: r2d2 // Input #3: // http://www.example.com?name=r2d2&email=r2d2%40me.com&human=no // Output // http://www.example.com // name: r2d2 // email: [email protected] // human: no THIS IS WHAT I HAVE SO FAR: HTML <!DOCTYPE html> <html lang="en"> <head> <meta...
Write and upload a MATLAB script to do the following. Compute the sequence S(n+1) = (2...
Write and upload a MATLAB script to do the following. Compute the sequence S(n+1) = (2 – K) S(n) – S(n-1) ;   Assume S(1) = 0, S(2) = 1; (a)    Case 1, Assume K = 1 (b)    Case 2, Assume K = 2 (c)     Case 3, Assume K = 4 Plot all of these on the same plot, for N = 1 to 20
ONLY NEED ANSWER TO C 1/ I know the answer to the other questions Consider the...
ONLY NEED ANSWER TO C 1/ I know the answer to the other questions Consider the following cash flows on two mutually exclusive projects for the Bahamas Recreation Corporation (BRC). Both projects require an annual return of 16 percent.    Year Deepwater Fishing New Submarine Ride 0 −$ 995,000 −$ 1,940,000 1 415,000 990,000 2 546,000 845,000 3 465,000 840,000 a-1. Compute the IRR for both projects. (Do not round intermediate calculations and enter your answers as a percent rounded...
VBA LOOPS The two macros in this homework should be assigned to separate buttons and should...
VBA LOOPS The two macros in this homework should be assigned to separate buttons and should be located on the same worksheet. When you are done, upload your saved workbook below. Be sure to write your code in a module in the workbook you will submit, not in your personal workbooks or sheets. To access the correct module, go to your “Visual Basic,” right-click “Microsoft Excel Objects” in the workbook you are submitting, and then click “Insert” and “Module.” Problem...
Written in MASM Assembly Problem Definition: Write a program to calculate Fibonacci numbers. • Display the...
Written in MASM Assembly Problem Definition: Write a program to calculate Fibonacci numbers. • Display the program title and programmer’s name. Then get the user’s name, and greet the user. • Prompt the user to enter the number of Fibonacci terms to be displayed. Advise the user to enter an integer in the range [1 .. 46]. • Get and validate the user input (n). • Calculate and display all of the Fibonacci numbers up to and including the nth...
Written Problem: Induction from a falling magnet We have a small magnet with a magnetic moment...
Written Problem: Induction from a falling magnet We have a small magnet with a magnetic moment of m = 0.1 Am2 (remember: magnetic moment is defined as m = IA - see page 932 of book for the definition). We also have coils of wire. The coils are made out of 100 circular loops of a single wire. A single loop has a radius of 10 cm. The thickness of the wire has a circular cross section with a 0.5...