Ok, so I have a code that stops only at 0. But wanted to make it so that it will stop when it hit's any non-number text. Could anyone help me turning != 0 into non numbers?
The code for this is:
#include <stdio.h>
int main(){
float sum = 1;
float new_number;
scanf("%f", &new_number);
while (new_number != 0){
sum += new_number;
scanf("%f", &new_number);
}
printf("Sum: %f\n", sum);
return 0;
}
Just check the return value of scanf
#include <stdio.h>
int main(){
float sum = 1;
float new_number;
while (scanf("%f", &new_number) > 0) {
sum += new_number;
}
printf("Sum: %f\n", sum);
return 0;
}
Related
I am learning about functions and how to call upon them and use them in class. I don't quite understand where I've gone wrong here. I know that there are some mistakes around the int main part. I have asked my teacher and he is reluctant on giving me an example that would solve my problems or help me out. I think my main problem is at factorial_result = factorial();
#include <stdio.h>
void mystamp(void)
{
printf("My name is John Appleseed\n");
printf("My lab time is 12:30 on Sunday\n");
return;
}
int getnum(void)
{
int local_var;
printf("Please enter an integer: ");
scanf("%d%*c", local_var);
return(local_var);
}
int factorial(void)
{
int x,f=1,local_var;
for(x=1; x <= local_var; x++)
f = f * x;
return(f);
}
int main(void)
{
int result;
int factorial_result;
mystamp();
result = getnum();
factorial_result = factorial();
printf("You typed %d\n", result);
printf("The factorial is %d\n", factorial_result);
return;
}
Declare local_var as a global variable and do:
local_var = getnum();
OR
Change main() to:
int main(void)
{
int result;
int factorial_result;
mystamp();
result = getnum();
factorial_result = factorial(result);
printf("You typed %d\n", result);
printf("The factorial is %d\n", factorial_result);
return;
}
And factorial() to:
int factorial(int n)
{
int x,f=1,local_var=n;
for(x=1; x <= local_var; x++)
f = f * x;
return(f);
}
Your factorial should be calculated based on the input( i.e in your case int result ).
So, your method factorial() should looks as follows :
int factorial( int number )
{
int factorial_value = 1;
while( number > 0 )
{
factorial_value *= number;
number--;
}
return factorial_value;
}
Then, the correct factorial would be returned and printed accordingly ! Regarding the scope of the variables that you have used, see the comments under your question.
#include <stdio.h>
int factorial(int);
int main()
{
int num;
int result;
printf("Enter a number to find it's Factorial: ");
scanf("%d", &num);
if (num < 0)
{
printf("Factorial of negative number not possible\n");
}
else
{
result = factorial(num);
printf("The Factorial of %d is %d.\n", num, result);
}
return 0;
}
int factorial(int num)
{
if (num == 0 || num == 1)
{
return 1;
}
else
{
return(num * factorial(num - 1));
}
}
This is a simple factorial program using recursion calling function !
include
int main()
{
int c, n, fact = 1;
printf("Enter a number to calculate its factorial\n"); scanf("%d", &n);
for (c = 1; c <= n; c++) fact = fact * c;
printf("Factorial of %d = %d\n", n, fact);
return 0;
}
I need some help displaying a float array that's partially filled. Not sure what I'm doing wrong, but here's what I have.
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <conio.h>
#define SIZE 20
int main()
{
float score[SIZE];
float GPA, sum, avg;
int count, ctr, num_of_entries;
sum = 0;
for (count = 0; count < SIZE; count++)
{
printf("Enter a GPA. -1 to stop the data entry: ");
scanf("%f", &GPA);
if (GPA == -1)
break;
score[count] = GPA;
}
printf("Number of GPAs entered = %d", count);
num_of_entries = count;
printf("\n\nContent of the Array:\n=========================\n");
for (ctr = 0; ctr <= num_of_entries; ctr++);
{
printf("%.2f \n", score[ctr]);
}
_getch();
return 0;
}
I'm trying to display the entered GPA values as float variables with 2 decimal places. The printed result is a very large negative number. Any help would be greatly appreciated.
I am having trouble with writing code for a series, I believe it is something with my if statement but I am stumped. The series is supposed to be
but I keep getting the wrong output. This is my code:
#include <stdio.h>
int n,t=1,nextTerm,sum=0,i;
int main() {
printf("Enter an integer number:");
scanf("%d",&n);
for(i=1;i<=n;i++) {
if (t%2 == 0) {
nextTerm = 1;
}
else {
nextTerm = -1;
}
t=nextTerm*(t*t);
sum=sum+t;
}
printf("The value of the series is: %d\n",sum);
return (0);
}
You have to check i+1 instead of i and replace t*t with i*i. You don't need any extra variable like t.
#include <stdio.h>
int main()
{
int n,nextTerm,sum=0,i;
printf("Enter an integer number:");
scanf("%d",&n);
for(i=1;i<=n;i++) {
if ((i+1)%2 == 0)
nextTerm = 1;
else
nextTerm = -1;
sum=sum+(nextTerm*i*i);
}
printf("The value of the series is: %d\n",sum);
return 0;
}
Series be like this:
1, -4, 9, -16, ...
Output:
Enter an integer number:4
The value of the series is: -10
I don't know if I understood it correctly, it should be something like this?:
#include <stdio.h>
int main() {
int n,t=1,firstPart,sum=0,i;
printf("Enter an integer number:");
scanf("%d",&n);
printf("The serie is: \n");
for(i=1;i<=n;i++) {
if (i%2 == 0) {
firstPart = 1;
}
else {
firstPart = -1;
}
t=firstPart*(i*i);
printf(" %d\t ", t);
sum=sum+t;
}
printf("\nThe value of the series is: %d\n",sum);
return (0);
}
for n = 4, the series is:
-1 / 4 / -9 / 16 /
and the sum is 10
Replace (t*t) with (i*i)
#include <stdio.h>
int main() {
int n,i,sum=0,nextTerm,t;
printf("Enter an integer number:");
scanf("%d",&n);
for(i=1;i<=n;i++) {
if (t%2 == 0) {
nextTerm = 1;
}
else {
nextTerm = -1;
}
t=nextTerm*(i*i);
sum=sum+t;
}
printf("The value of the series is: %d\n",sum);
return (0);
}
Output -
Enter an integer number:4
The value of the series is: -10
I in my program, I'm attempting to create two different variables that will look the user's input integers and count how many are over 20 and how many are between 10 and 90. Unfortunately, both functions (counter_20 and between_count) return the same result, and when printed, it is an exorbitantly high and incorrect number. Any help would be greatly appreciated! My code is as follows:
#include <stdio.h>
#include <stdbool.h>
int getNumber(void);
float average_counter(int sum, int numCounter);
int counter_20(int input);
int between_counter(int input);
void print_results(int sum, int average, int numCounter, int sumOfTwenties, int betweenCount);
int main (void){
int a, b, c, e, f;
float d;
bool x = true;
a = 0;
b = 0;
c = 0;
d = 0;
e = 0;
f = 0;
while(x = true){
if(a != 9999){
if(a != 0){
b += a;
c++;
d = average_counter(b, c);
e = counter_20(a);
f = between_counter(a);
}
else{
printf("No input was provided.");
}
}
else{
break;
}
}
print_results(b, d, c, e, f);
}
int getNumber(void){
int input;
printf("Please input an integer. If you would like to stop inputting integers and see the$
scanf(" %d", &input);
return input;
}
float average_counter(int sum, int numCounter){
float average;
average = sum/numCounter;
return average;
}
//One of my functions for counting the number of integers over 20
int counter_20(int input){
int countTwenties;
if(input > 20){
++countTwenties;
}
printf(" %d", countTwenties);
return countTwenties;
}
//my function for counting the integers between 10 1nd 90
int between_counter(int input){
int betweenCount;
if(input < 90){
if(input > 10){
++betweenCount;
}
else{
}
}
return betweenCount;
}
void print_results(int sum, int average, int numCounter, int countTwenties, int betweenCount){
printf("\nThe sum is: %d", sum);
printf("\nThe average is: %d", average);
printf("\nThe number of integers is: %d", numCounter);
printf("\nThe number of integers over 20 is: %d", countTwenties);
printf("\nThe number of integers between 10 and 90 is: %d", betweenCount);
return;
}
int counter_20(int input){
int countTwenties;
if(input > 20){
++countTwenties;
}
printf(" %d", countTwenties);
return countTwenties;
}
countTwenties is uninitialized and using uninitialized variables lead to undefined behavior. Fix it by initializing the variable like
int countTwenties=0;
As pointed out in my comment
while(x = true)
should be
while(x == true)
Normally compiler throws a warning when you have something like this in the code
I need to create a simple program which asks for 10 numbers from user and then shows the sum of those numbers or, if the user gives 0 as input, stops and immediately displays the sum of those numbers, and I need to create it only by using a "for" condition. Here is the code:
#include <stdio.h>
int main(){
int num = 0;
for(num = 0; num < 10; num++){
printf("Input a number: \n");
scanf("%d", &num);
if(num == 0){
printf("Sum: %d\n", num);
}
}
printf("Sum: %d\n", num);
getchar();
getchar();
}
It stops only when the number is greater than "10".Whats wrong?
I think you want to do this kind of work with your code.
#include <stdio.h>
int main(){
int num = 0;
int sum=0;
for(num = 0; num < 10; num++){
int i;
printf("Input a number: \n");
scanf("%d", &i);
sum = sum+i;
if(i == 0){
printf("Sum: %d\n", sum);
getchar();
return 0;
}
}
printf("Sum: %d\n", sum);
getchar();
return 0;
}
You are changing the value of the counter inside the for loop. That's why, when you read a value greater than or equal to 10, it will abandon the for loop, since you have the condition num < 10.
Let me tweak the code for you:
#include <stdio.h>
int main(){
int sum = 0;
int i;
int num;
for(i = 0; i < 10; i++){
printf("Input a number: \n");
scanf("%d", &num);
sum += num;
if(num == 0){
break; //means leave the loop
}
}
printf("Sum: %d\n", sum);
getchar();
return 0;
}
I'm using 3 variables:
sum, which is used to store the overall sum.
i, which is used as the for loop counter.
num, which is used to store the current number given by the user.
First of all, I'm waiting for an input:
printf("Input a number: \n");
scanf("%d", &num);
Now, the input is stored in num, so I'm upgrading the sum, to add the new value:
sum += num;
The I check if the current number is zero; in that case I'll just leave the loop:
if(num == 0){
break;
}
The problem in your code is, you're using the same variable num both as counter and for taking user input which is breaking the logic in for loop.
Use another variable for taking user input.
Also, you've to have a break statement to discontinue the for loop once you've got the breaking criteria.
Note: as I mentioned in my comments, there is no logic for Sum.
Check the below code.
#include <stdio.h>
int main(){
int num = 0;
int sum = 0; //to hold the sum
int input = 0;
for(num = 0; num < 10; num++){
printf("Input a number: \n");
scanf("%d", &input);
sum += input; // yoo-hoo, time to add-up
if(input == 0){
printf("Sum: %d\n", sum);
break; // time to say bye-bye to for loop
}
}
if (num == 10) //only print if not printed previously
printf("Sum: %d\n", sum);
return 0;
}