473,378 Members | 1,360 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Syntax error in hailstones sequence program

Hi, I'm trying to write a program that can generate some hailstones, here is my code so far:


Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3.  
  4. void    hailstones(int n);
  5. int main(void)
  6. {
  7.         int a;
  8.         printf("Enter the number: ");
  9.         scanf("&d", &a);
  10.         return 0;
  11. }
  12. void hailstones(int n)
  13. {
  14.         int i = 0;
  15.         while (n != 1){
  16.                 if (n % 2 == 0) {
  17.                         n = n/2;
  18.                         printf("%d", n);
  19.                         i = i + 1;
  20.                 }
  21.                 else (n % 2 != 0) {
  22.                         n = (3 * n) + 1;
  23.                         printf("%d", n);
  24.                         i = i + 1;
  25.                 }
  26.         }
  27.         printf("Number of hailstones generated: %d\n", i);
  28. }
The program returns some errors:
hailstones.c: In function `hailstones':
hailstones.c:20: error: syntax error before '{' token

Can anybody please tell me what I did wrong and what should I do to fix these problems?
Nov 21 '11 #1
2 3650
johny10151981
1,059 1GB
whenever you use else you must not use any condition.

Expand|Select|Wrap|Line Numbers
  1. //this is wrong
  2. else (n % 2 != 0) {
  3.  
  4. //right would be
  5. else 
  6. {
  7. ...
  8. }
  9.  
in your case you do not need to write the second condition at all. cause possible return is two 1) zero or 2) not zero
Nov 21 '11 #2
Banfa
9,065 Expert Mod 8TB
You could use else if though

Expand|Select|Wrap|Line Numbers
  1. if (n % 2 == 0) {
  2.   ...
  3. }
  4. else if (n % 2 != 0) {
  5.   ...
  6. }
Nov 21 '11 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Max | last post by:
Suppose we have an object hierarchy in XSD. For example, let us take Control, Label, TextBox and LinkLabel windows controls. Each control has some properties which are represented as elements....
1
by: Hemant Shah | last post by:
Folks, I am having problem running select query from a sequence. I get SQL0204N error. I am running UDB 8.2 on AIX 5.2. # oslevel 5.2.0.0 # db2level DB21085I Instance "dba2test" uses...
1
by: Marek Lewczuk | last post by:
Hello, I would like to ask if my problem with sequence is a proper behavior or this is a bug (probably not)... I have a table: CREATE TABLE "testtable" ( "serialfield" SERIAL,...
2
by: yqlu | last post by:
I hava developed a client in C# that is connected to a 3-party XML Web Services developed in Java based on the AXIS 1.1. Most methods call are successful except for one method named "findObjects"...
1
by: leslie_tighe | last post by:
Hello, I have webservice created with Axis 1.2.1 and that I am trying to consuming in .NET (VB) using the Microsoft provided tools. While I am able to consume methods on the service that return...
4
by: leslie_tighe | last post by:
Hello, I have a webservice running on a J2EE server created with Axis 1.2.. I have a client that I am building in .net that needs to consume this webserivce and am having a bit of trouble. I have...
3
by: kevin | last post by:
Is that even possible? I am creating a web service in .NET to expose some already created .NET programs to other groups. One group is writing the client in PERL, and thus wishes the wsdl schema...
0
by: ryan | last post by:
I've been tasked with consuming a Perl web service that was written by a person in a different department of my company. The problem is it's the guy's first attempt at web services and he doesn't...
5
by: Tio | last post by:
I have project in MFC(vc++) . There are files and classes: classes:dialog1,dialog2,aaa,bbb ---------------------- main.cpp --------------------- #include "mainfrm.h" #include "dialog1.h"...
4
by: IsValidUN | last post by:
I have an XSD which has an import to an XSD located on the web which has a DTD within it. (Using VS-2005 2.0 framework) If I load the XSD using the following, it can't find elements defined within...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.