Solved

Calculate a firsts numbers

Posted on 2003-10-27
1
172 Views
Last Modified: 2010-04-01
I have a program but this don't work, this programe had to calculate a firsts numbers of a serie, thanks.

#include <stdio.h>
#include <string.h>
#include <math.h>
void main()
{
        char c[255];
      long int m=1,cont1,cont2,b=2;
        printf("\n Tria si vols imprimir una llista de nombre parells,\n o una llista de nombres primers: ");
      scanf("%s",&c);
      printf("\nIntrodueix el primer numero de la serie: ");
      scanf("%ld",&cont1);
      printf("\nIntrodueix el segon numero de la serie: ");
      scanf("%ld",&cont2);
      if (!strcmp (c,"primers")){
            while (cont1<cont2){
                  while ((b>(cont1/2)) && (m!=0)){
                  if((cont1%b)!=0) b=b+1;
                  if((cont1%b)==0) m=0;
                  }
                  if (m==0){
                         printf("\nEl nombre %ld és primer",cont1);
                         m=1;
                         }
                  ++cont1;
                  }
      }
      if (!strcmp (c,"parells")){
            if((cont1%2)!=0) ++cont1;
            while (cont1<cont2){
                  printf("%ld\n", cont1);
                  ++cont1;
                  ++cont1;
                  }
              if((cont2/2)==0) printf("%ld",cont2);
      }        
}
0
Comment
Question by:FxSedge
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 13

Accepted Solution

by:
SteH earned 50 total points
ID: 9632232
Is this homework? I guess yes, so I give just hints:
put the line
printf ("%d %d %d\n", cont1, b, m);
after while (cont1<cont2) and look whether the values are as you expect.

Another thing:

 if (test != 0) do1;
 if (test == 0) do2;

can be rewritten as

if (test != 0)
     do1;
else
     do2;
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
C++ mouse_event mouse look 7 131
HTTPSendRequest with WinINet delays on first call 11 119
boost::uuid crashes 17 63
Winapi.Windows.hpp problem 7 44
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question