Write a program which reads a positive integer number n, calculates and shows the following: 

  • $$1 + \frac{1}{2} + \frac{1}{3} + \frac{1}{4} + ... + \frac{1}{n}$$ if $$n$$ is divisible by 7
  • $$n + \frac{n}{2} + \frac{n}{3} + \frac{n}{4} + ... + \frac{n}{n-1} + 1$$ otherwise

Difficulty level
This exercise is mostly suitable for students
#include<stdio.h>
int main()
{
	int n, i;
	double sum;
	
	sum =0;
	
	do{
	    printf("Enter n: ");
	    scanf("%d",&n);
	}while(n<0);
	
	if(n%7==0)
	{
	    for(i=1;i<=n;i++)
	        sum+= 1.0/i;
	}
	else
	{
	    for(i=1;i<=n;i++)
	        sum+= 1.0*n/i;
	}
	printf("Sum = %.4lf\n",sum);
 
	return 0;
}

 
 

Back to the list of exercises
Looking for a more challenging exercise, try this one !!
Recursively reverse a stack