Sign in to follow this  
Cornel Iulian

[C] Suma si produsul primelor n termeni [Functia FOR]

1 post in this topic

Cerinta: Sa se calculeze pentru o valoare n preluata de la consola, suma si produsul primelor n numere naturale folosind instructiunea FOR

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
	int n,s,i;
	float p;
	printf("Introduceti o valoare n: ");
	if(scanf("%d",&n)!=1)
	{
		printf("Date eronate");
		exit(1);
	}
	s=0;
	p=1.0f;
	for(i=1;i<=n;i++)
	{
		s+=i;
		p*=i;
	}
	
	printf("Suma: %d\t\tProdus: %g",s,p);
}

 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this