Solved

Calculate a firsts numbers

Posted on 2003-10-27
1
166 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
1 Comment
 
LVL 13

Accepted Solution

by:
SteH earned 50 total points
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

772 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now