-8

I have to write a program in C which calculates and displays the following expression:

P=1*2*(1/3)*4*5*(1/6)

I tried running this code ( this is the whole code) but I cant compile it because it shows errors in the fourth and eleventh row in C++..What am I doing wrong?

   #include <stdio.h>

    int i,n,f=1;
    for(i=1;i<=n;i++)
    {
        if(i%3==0)
           f=f/i;
        else
           f=f*i;
    }
    printf("%d\n", f);
    return 0;

I cant write here the erros because they are soooo many..

Linga
  • 10,379
  • 10
  • 52
  • 104

2 Answers2

4

You are missing a definition of the main() function. Also, n is uninitialized; you need to assign it a value.

#include <stdio.h>

int main(void)
{
    int i,n,f=1;
    for(i=1;i<=n;i++)
    {
        if(i%3==0)
            f=f/i;
        else
            f=f*i;
    }
    printf("%d\n", f);
    return 0;
}
Graham Borland
  • 60,055
  • 21
  • 138
  • 179
0

You may need to declare f as float, I am not getting any error in the following:

#include <stdio.h>

int main(void)
{
    int i,n;
    float f = 1;
    printf("Enter value of n:");
    scanf("%d",&n);
    for(i=1;i<=n;i++)
    {
        if(i%3==0)
            f=f/i;
        else
            f=f*i;
    }
    printf("%f\n", f);
    return 0;
}
Dipto
  • 2,720
  • 2
  • 22
  • 42