Write a program which reads a positive integer value N, calculates and shows the result of the expression: 1 + 4 + 7 + 10 +... + N


Difficulty level
Video recording
This exercise is mostly suitable for students
#include<stdio.h>
#include<conio.h>

int main()
{
	int N, i , sum;
	sum = 0;
	
	do{
	    printf("Enter an integer: ");
	    scanf("%d", &N);
	}while(N <0);
	

    	for(i=1; i <=N; i+=3)
        	sum+= i ;
        
    	printf("Sum = %d \n", sum);
 
	return 0;
}

Back to the list of exercises
Looking for a more challenging exercise, try this one !!
Iterative DFS traversal of a graph using an adjacency list