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 ><!!!
5 2520 Banfa 9,065
Expert Mod 8TB
What don't you know how to do.
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...
Banfa 9,065
Expert Mod 8TB
Here is a sample for 1 -
#include <stdio.h>
-
#include <stdlib.h>
-
-
int main(int argc, char **argv)
-
{
-
FILE *fin;
-
FILE *fout;
-
-
fin = fopen("input.txt", "r");
-
-
if (fin != NULL)
-
{
-
char line[250];
-
int value;
-
-
fgets(line, sizeof line, fin);
-
-
value = atoi(line);
-
-
fclose(fin);
-
-
fout = fopen("output.txt", "w");
-
-
if (fout != NULL)
-
{
-
fprintf(fout, "%d\n", value);
-
fclose(fout);
-
}
-
else
-
{
-
puts("Failed to open output file");
-
}
-
}
-
else
-
{
-
puts("Failed to open input file");
-
}
-
-
return 0;
-
}
-
It reads 1 number from the first line of a file.
Banfa 9,065
Expert Mod 8TB
and here is an example for the second -
#include <stdio.h>
-
#include <limits.h>
-
-
void InterativePrintDigits(int value);
-
void RecursivePrintDigits(int value);
-
-
int main(int argc, char **argv)
-
{
-
int value = 24807635; /* This is a random number */
-
-
InterativePrintDigits(value);
-
RecursivePrintDigits(value);
-
printf("\n");
-
-
return 0;
-
}
-
-
void InterativePrintDigits(int value)
-
{
-
int mask = 1000000000; /* You could calculate this value */
-
int firstNonZeroFound = 0;
-
int digit;
-
-
/* get the maximum digit */
-
while(mask)
-
{
-
digit = value / mask;
-
-
if (firstNonZeroFound || digit)
-
{
-
printf("%d ", digit);
-
firstNonZeroFound = 1;
-
}
-
-
value %= mask;
-
mask /= 10;
-
}
-
-
printf("\n");
-
}
-
-
void RecursivePrintDigits(int value)
-
{
-
if (value == 0)
-
{
-
return;
-
}
-
-
RecursivePrintDigits(value/10);
-
-
printf("%d ", value%10);
-
}
-
What don't you know how to do.
I don't know this program
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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,...
|
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...
|
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...
|
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...
|
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....
|
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...
|
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
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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: 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,...
|
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...
|
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...
|
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...
|
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,...
| |