Sign in to follow this  
Cornel Iulian

[C] Suma si produsul a N termeni introdusi [Limbaj C]

1 post in this topic

Cerinta: Se preia de la consola numarul termenilor ce urmeaza a fi prelucrati. Se cere sa se scrie un program care sa calculeze suma termenilor si produsul termenilor introdusi.

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

int main(void)
{
	int i,n;
	double x,s,p;
	printf("Introduceti numarul de termeni: ");
	if(scanf("%d",&n)!=1||n<0)
	{
		printf("Date eronate");
		exit(1);
	}
	s=0;
	p=1.0f;
	for(i=1;i<=n;i++)
	{
		printf("x%d= ",i);
		if(scanf("%lf",&x)!=1)
		{
			printf("Date eronate");
			exit(1);
		}
		s+=x;
		p*=x;
		
	}
	printf("Suma: %g\t\tProdus: %g",s,p);
	
		
	return 0;
}

 

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