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)
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 !!!
Get Answers For Free
Most questions answered within 1 hours.