473,396 Members | 1,766 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,396 software developers and data experts.

can anyone help me to invert c++ code to c code....

Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2. int is_prime(int x);
  3. int main(){
  4.     char again;
  5.     int x = 17;
  6.     int count = 0;
  7.     again = 'y';
  8.  
  9.     cout << "The prime numbers less than or equal to "<< x <<" are:" << endl;
  10.                     for(int i=2 ; i<=x ; i++){
  11.                             if(is_prime(i)){
  12.                             cout << "\t\t" <<i << endl;
  13.                             count ++;}
  14.                             }
  15.                     cout << "The total count of prime numbers is: "<< count<<endl << endl << endl;      
  16.  
  17.     while (again == 'y' || again == 'Y')
  18.     {     
  19.           cout << "Enter a 3-digit positive integer: ";
  20.           cin >> x;
  21.           int count = 0 ;
  22.           for(int i=2; i<=x ; i++){
  23.                             if(is_prime(i)){
  24.                             cout << "\t\t" <<i << endl;
  25.                             count++;}
  26.                             }
  27.                     cout << "The total count of prime numbers is: "<< count <<endl << endl << endl;      
  28.           cout << "Would you like enter the number again? (y/n) " ;
  29.           cin >> again;
  30.           cout << endl;
  31.  
  32.           if(again == 'n' || again =='N')
  33.            cout << "<<<<< Program Completed >>>>>" << endl;
  34.           }  
  35.  return 0;   
  36. }
  37.  
  38.  
  39. int is_prime(int x){
  40.      if (x==2)
  41.         return 1;
  42.      for (int i = 2; i < x ; i++){
  43.          if(x%i==0)
  44.              return 0;}
  45.      return 1;
  46.      }
Nov 14 '09 #1
4 1672
Markus
6,050 Expert 4TB
Glancing through your code, the only things that I see that will need changing are the cin and cout statements.

For the C alternatives, see printf() (cout) and scanf() (cin).

Mark.

P.S. Please see the posting guidelines on how to ask a question, specifically the part on using [code] tags.
Nov 14 '09 #2
@Markus
Yep, in c there is no cin and cout, so use printf and scanf.

But theres something else:
* On pure C variables declarations are the first lines of each function.
* this statement:
Expand|Select|Wrap|Line Numbers
  1. for(int i=2 ; i<=x ; i++){ 
isn't allowed in C, you can't declare a variable in the for statement, in the beginning of the function as stated before.
Nov 15 '09 #3
C99 allows that type of declaration:
http://bytes.com/topic/c/answers/698...claration-loop
it depends on with what standard of C you are looking to comply.
Nov 15 '09 #4
Banfa
9,065 Expert Mod 8TB
That's true JonathonS but most C compilers, even if the are C99 compliant*, still default to C89 and so wont support this unless they have been specifically configured to.


* Or at least as compliant as possible since there are no fully C99 compliant compilers yet.
Nov 15 '09 #5

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

Similar topics

10
by: Des Small | last post by:
Lately I have found myself using a pattern to make new dictionaries quite often, by which I mean twice: def invert(d): nd = {} ).append(key) for k, v in d] return nd def count(l): d = {}
5
by: Me Padre | last post by:
Hoping someone can help with this. I know I can do this programatically using a loop but I thought there might be some easier or more effective way. I am trying to invert an array. i.e. ...
3
by: Peter Aitken | last post by:
I want to invert all the bits (all 0s to 1s, all 1s to 0s) in a type ushort. I though I would use the complement operator ~ but the compiler won't buy it. How can I do this? Thanks, --...
4
by: Derek Martin | last post by:
Hi List, I have an arraylist of objects. I have created my own IComparable in the object to return the sort on datetime. This works great! Now, I'd like to invert the sort. Currently, it gives...
41
by: rick | last post by:
Why can't Python have a reverse() function/method like Ruby? Python: x = 'a_string' # Reverse the string print x Ruby: x = 'a_string' # Reverse the string
1
by: mahesh.nimbalkar | last post by:
Hi, I have color as System.Drawing.Color c1 object as background color. Now I would like to get another System.Drawing.Color c2 object which is invert of c1 color to be used as foreground...
0
by: Karthik | last post by:
Hi, I want to record a sound wave from a mic and at the same time invert it and play the inverted wave.My code goes as follows, however nothing is written into the E:\inverted.wav file.Thanks in...
2
by: Stef Mientki | last post by:
hello, from the manual I read that a bitwise inversion should be done by invert. But from some experiments I see that not works equally well. Is this coincidence ? (The disadvantage of invert...
4
by: dave | last post by:
Hello, here is a piece of code I wrote to check the frequency of values and switch them around to keys in a new dictionary. Just to measure how many times a certain key occurs: def...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.