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
I need a program written in C, not C+ or C++. I have no idea how...
I need a program written in C, not C+ or C++. I have no idea how to go about doing it. Basically, I need it to take in the following information and spit out the following for a number table. The operation should be able to take plus, minus, division or multiplication and the low/high row/column numbers should be able to go up to where ever. But I'm sure the following example is fine. Enter low row number: 1 Enter...
Subject: Shell Scripting Practice A File: practice-script-a Create File practice-script-a that: 1. Accepts any number of...
Subject: Shell Scripting Practice A File: practice-script-a Create File practice-script-a that: 1. Accepts any number of userids on the command line 2. For each userid, display the userid, PID, CPU time, and current command for each process owned by that userid Your output should look similar to this example: practice-script-a doug.jones User: doug.jones PID: 8748 TIME: 00:00:00 COMMAND: /usr/lib/systemd/systemd --user User: doug.jones PID: 8749 TIME: 00:00:00 COMMAND: (sd-pam)    User: doug.jones PID: 8750 TIME: 00:00:00 COMMAND: sshd: doug.jones@pts/5 User: doug.jones...
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.
*********I need question 6 answered which is from question 5 which is ********* Question 5 :...
*********I need question 6 answered which is from question 5 which is ********* Question 5 : Program Correctness I (1 point) Use the loop invariant (I) to show that the code below correctly computes n P−1 k=0 2k (this sum represents the sum of the first n even integers where n ≥ 1). Algorithm 1 evenSum(int n) 1: p = 2(n − 1) 2: i = n − 1 3: while i > 0 do 4: //(I) p = nP−1...
JAVA / I KNOW THERE IS MORE THAN ONE QUESTION BUT THEY ARE SO EASY FO...
JAVA / I KNOW THERE IS MORE THAN ONE QUESTION BUT THEY ARE SO EASY FO YOU I NEED YOUR HELP PLEASE HELP ME. I WILL GIVE UPVOTE AND GOOD COMMENT THANK YOU SO MUCH !!! QUESTION 1 What is the output after the code executes? Assume there are no syntax errors and the code will compile. int x = 10; do { System.out.println ( x ); x -= 3; } while (x > 0); A. 10 7 4 1...
I need code for a problem in Matlab Create a single Matlab script that accomplishes the...
I need code for a problem in Matlab Create a single Matlab script that accomplishes the following: Defines a blank tic tac toe board, a 3 x 3 matrix, called board with blank spots represented by zeros. Asks player 1 for a row and column, then puts 1 in a given location. Asks player 2 for a row and column, then puts 2 in a given location
The first script you need to write is login.sh, a simple script that you might run...
The first script you need to write is login.sh, a simple script that you might run when you first log in to a machine. We'll expand this script later, but for now it should include your name, username, hostname, current directory, and the current time. Here is some sample output to help guide you. Note that the bolded lines that start with "[user@localhost ~]$" are the terminal prompts where you type in a command, not part of the script. Of...
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...
Assume the earth’s continents drift about 1.25 centimeters per year. Write a Python program/script using a...
Assume the earth’s continents drift about 1.25 centimeters per year. Write a Python program/script using a for-loop to display the total number of centimeters that the continents will have moved each year for the next 50 years. a. What tasks need to be repeated (write in words)? b. What is the start point of the loop and why? c. What is the end point of the loop and why? d. What is the start value for the initial drift and...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT