473,405 Members | 2,262 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,405 software developers and data experts.

Assignment: Moving values between files and integer to digit

1. write a program that tests the value (between 1 and 12 ) of an int variable num1 (take the value from the a file). If the value is 10, square num1.If it is 9, read a new value into num1. If it is 2 or 3, multiply num1 by 99 and print out the result. In all the other cases, print ++num1. (take the value from a file and write the results into a file)

2. Write a program that prompts the user to input an integer and then outputs both the individual digits of the number and the sum of the digits. For example, it should output the individual digits of 3456 as 3 4 5 6, output the individual digits of -2345 as 2 3 4 5.

I NEED HELP ><!!!
Oct 23 '06 #1
5 2520
Banfa
9,065 Expert Mod 8TB
What don't you know how to do.
Oct 23 '06 #2
What don't you know how to do.
in the first one, i don't know how to take the value from a file and put the value to a file.
in the second one, how to get each digit of the integer...
Oct 23 '06 #3
Banfa
9,065 Expert Mod 8TB
Here is a sample for 1

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. int main(int argc, char **argv)
  5. {
  6.     FILE *fin;
  7.     FILE *fout;
  8.  
  9.     fin = fopen("input.txt", "r");
  10.  
  11.     if (fin != NULL)
  12.     {
  13.         char line[250];
  14.         int value;
  15.  
  16.         fgets(line, sizeof line, fin);
  17.  
  18.         value = atoi(line);
  19.  
  20.         fclose(fin);
  21.  
  22.         fout = fopen("output.txt", "w");
  23.  
  24.         if (fout != NULL)
  25.         {
  26.             fprintf(fout, "%d\n", value);
  27.             fclose(fout);
  28.         }
  29.         else
  30.         {
  31.             puts("Failed to open output file");
  32.         }
  33.     }
  34.     else
  35.     {
  36.         puts("Failed to open input file");
  37.     }
  38.  
  39.     return 0;
  40. }
  41.  
It reads 1 number from the first line of a file.
Oct 23 '06 #4
Banfa
9,065 Expert Mod 8TB
and here is an example for the second

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <limits.h>
  3.  
  4. void InterativePrintDigits(int value);
  5. void RecursivePrintDigits(int value);
  6.  
  7. int main(int argc, char **argv)
  8. {
  9.     int value = 24807635;  /* This is a random number */
  10.  
  11.     InterativePrintDigits(value);
  12.     RecursivePrintDigits(value);
  13.     printf("\n");
  14.  
  15.     return 0;
  16. }
  17.  
  18. void InterativePrintDigits(int value)
  19. {
  20.     int mask = 1000000000;  /* You could calculate this value */
  21.     int firstNonZeroFound = 0;
  22.     int digit;
  23.  
  24.     /* get the maximum digit */
  25.     while(mask)
  26.     {
  27.         digit = value / mask;
  28.  
  29.         if (firstNonZeroFound || digit)
  30.         {
  31.             printf("%d ", digit);
  32.             firstNonZeroFound = 1;
  33.         }
  34.  
  35.         value %= mask;
  36.         mask /= 10;
  37.     }
  38.  
  39.     printf("\n");
  40. }
  41.  
  42. void RecursivePrintDigits(int value)
  43. {
  44.     if (value == 0)
  45.     {
  46.         return;
  47.     }
  48.  
  49.     RecursivePrintDigits(value/10);
  50.  
  51.     printf("%d ", value%10);
  52. }
  53.  
Oct 23 '06 #5
What don't you know how to do.

I don't know this program
Oct 3 '07 #6

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

Similar topics

5
by: Haoyu Zhang | last post by:
Dear Friends, Python assignment is a reference assignment. However, I really can't explain the difference in the following example. When the object is a list, the assignment seems to be a...
16
by: Edward Diener | last post by:
Is there a way to override the default processing of the assignment operator for one's own __value types ? I realize I can program my own Assign method, and provide that for end-users of my class,...
4
by: Dawn Minnis | last post by:
Hi When I compile my files I get the following: driver.c: In function `main': driver.c:49: warning: assignment makes integer from pointer without a cast driver.c:50: warning: assignment...
8
by: Candace | last post by:
I am using the following code to pick off each digit of a number, from right to left. The number I am working with is 84357. So for the first iteration it should return the number 7 and for the...
6
by: ocean | last post by:
Write a program that prompts the user to input an integer and then outputs both the individual digits of the number and the sum of the digits. For example, it should output the individual digits...
61
by: warint | last post by:
My lecturer gave us an assignment. He has a very "mature" way of teaching in that he doesn't care whether people show up, whether they do the assignments, or whether they copy other people's work....
5
by: adarshyam | last post by:
Hi friends, I have an interesting problem in vb.net. And I am struggling to get a solution for this..m trying for the past 3days.. It’s to calculate moving average for the inputs given by the...
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
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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.