Simple C Program is not getting printed - c

I am trying to run this simple code:
#include <stdio.h>
int main()
{
int n = 3;
printf("Enter your number: ");
scanf("%d",&n);
int faculty(int n){
int i = 1;
int res = 1;
for (i = 2;i<= n;i++){
res = res * i;
}
printf("the value is %d\n",res);
return(0);
}
}
But somehow there is no result showing up. Could you please explain me how it should be implemented properly?
Thanks in advance!

Please try this:
#include
int faculty( int n )
{
int i = 1;
int res = 1;
for ( i = 2; i <= n; i++ )
{
res = res * i;
}
printf( "the value is %d\n", res );
getchar( );
return ( 0 );
}
int main( )
{
int n = 3;
printf( "Enter your number: " );
scanf( " %d", &n );
faculty( n );
}

Related

write a program that computes and displays the factorial of a positive integer n entered by the user

Been working on this question for class. Not sure what I'm doing wrong.
Seems like I just don't have the correct format. My professor wants the output to look like "5!=1 * 2* 3* 4* 5=120"
Can someone help me with this? Below is what I have so far:
#include <iostream>
#include <stdio.h>
int main() {
int n, i, fact;
printf("Enter a positive integer: \n");
scanf("%d", &n);
if (n < 0)
printf("Error! Factorial of a negative number doesn't exist.");
else {
for( i = 1; i <= n; ++i) {
fact *= i;
n= n * (n-1);
}
printf("Factorial of %d = ", &n, &fact) ;
}
return 0;
}
remove #include <iostream> if it is the C code
Use functions.
Initialize local variables (you do not and it is undefined behaviour)
Compile C program using C compilers
unsigned long long fact(unsigned val, char *buff)
{
unsigned result = 1;
*buff++ = '1';
*buff = 0;
for(unsigned c = 2; c <= val; c++)
{
buff += sprintf(buff, "x%u", c);
result *= c;
}
return result;
}
int main(void)
{
unsigned x;
char y[1000];
unsigned long long res;
if(scanf("%u", &x) == 1){res = fact(x, y); printf("factoral(%u) = %s = %llu\n", x, y, res);}
else printf("Invalid number!!!\n");
}
or without printing steps
unsigned long long fact(unsigned val)
{
unsigned result;
switch(val)
{
case 0:
case 1:
result = 1;
break;
case 2:
result = 2;
break;
default:
result = 2;
while(val > 2) result *= val--;
break;
}
return result;
}
int main(void)
{
unsigned x;
if(scanf("%u", &x) == 1) printf("factioral(%u) = %llu\n", x, fact(x));
else printf("Invalid number!!!\n");
}
https://godbolt.org/z/Tf5431zco
For starters it does not make a sense to declare the variable n as having a signed integer type. It is better to declare it as having unsigned integer type. For example
unsigned int n, i, fact;
Secondly the variable fact was not initialized. You should initialize it by the value equal to 1.
So the above declaration could look like
unsigned int n, i;
unsigned long long fact = 1;
In this for loop
for( i = 1; i <= n; ++i) {
fact *= i;
n= n * (n-1);
}
the statement
n= n * (n-1);
does not make a sense and shall be removed.
In the printf call you have to use values of variables instead of pointers to the variables like
printf("Factorial of %u = %llu\n", n, fact) ;
Pay attention to that neither declaration from the header <iostream> is used in your program So whether it is a C program or a C++ program nevertheless remove this header.
Here is a demonstration C program.
#include <stdio.h>
int main( void )
{
unsigned int n = 0;
unsigned long long int fact = 1;
printf( "Enter a positive integer: " );
scanf( "%u", &n );
printf( "%u! = ", n );
for ( unsigned int i = 1; i <= n; i++ )
{
fact *= i;
if ( i != 1 ) printf( " * " );
printf( "%u", i );
}
printf( " = %llu\n", fact );
}
The program output might look like
Enter a positive integer: 5
5! = 1 * 2 * 3 * 4 * 5 = 120
If you are allowed to use only variables of the type int as you wrote in a comment to the answer then the program can look like
#include <stdio.h>
int main( void )
{
int n = 0;
int fact = 1;
printf( "Enter a positive integer: " );
scanf( "%d", &n );
if ( n < 0 )
{
puts( "Error! Factorial of a negative number doesn't exist.");
}
else
{
printf( "%d! = ", n );
for ( int i = 1; i <= n; i++ )
{
fact *= i;
if ( i != 1 ) printf( " * " );
printf( "%d", i );
}
printf( " = %d\n", fact );
}
}
To output multipliers in the reverse order like
5! = 5 * 4 * 3 * 2 * 1 = 120
rewrite the for loop the following way
for ( int i = n; i != 0; i-- )
{
fact *= i;
if ( i != n ) printf( " * " );
printf( "%d", i );
}

counting consecutive value and printing in c programming

how to count consecutive two heads and tails in this program please help me, I am really stuck, I tried hard but failed please, write the code for consecutive please, I am very hopeful that StackOverflow can help me
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int flip();
int main()
{
int loop;
int headCount = 0;
int tailCount = 0;
srand( time( NULL ) );
for ( loop = 1; loop <= 100; loop++ ) {
if ( flip() == 0 )
{
tailCount++;
}
else
{
headCount++;
}
if ( loop % 10 == 0 )
{
printf( "\n" );
}
}
printf( "\nThe total number of consecutive Heads was %d\n", headCount );
printf( "The total number ofconsecutive Tails was %d\n", tailCount );
return 0;
}
int flip() {
int HorT = rand() %2;
if ( HorT == 0) {
printf( "Tails " );
}
else
{
printf( "Heads " ); }
return HorT;
}
I don't know how you want to count 4 consecutive runs of a tail (is that 3, or 4 or 2?), and I'm not going to worry too much about it. Mostly posting this to point out some stylistic issues with your code. You can greatly simplify what you're doing with something like:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int flip(void);
int
main(int argc, char **argv)
{
int loop;
int max = argc > 1 ? strtol(argv[1], NULL, 10) : 100;
int count[2] = {0, 0};
int prev = -1;
srand(time(NULL));
for( loop = 0; loop < max; loop++ ) {
int t = flip();
if( t == prev ) {
count[t] += 1;
}
if( loop % 10 == 9 || loop == max - 1 ) {
putchar('\n');
}
prev = t;
}
printf( "\nThe total number of consecutive Heads was %d\n", count[0]);
printf( "The total number of consecutive Tails was %d\n", count[1]);
return EXIT_SUCCESS;
}
int
flip(void)
{
int HorT = rand() % 2;
fputs(HorT ? "Heads " : "Tails ", stdout);
return HorT;
}

Recursive Persistence of a number

I am trying to find the persistence of a number. When you multiply the digits of a number together, eventually you will arrive at a single digit number. Persistence is the number of cycles that takes. I am trying to find that using a recursive Function. Here is my code:
#include <stdio.h>
int persistence(int x);
int main(int argc, char *argv[])
{
int x, per = 0, t;
char c;
while((c = getchar())!= EOF)
{
printf("Enter a number:\n");
scanf("%d", &x);
while(x>10)
{
t = persistence(x);
printf("\n%d", persistence(t));
per++;
}
printf("\n%d\n\n", per);
}
return 0;
}
int persistence(int x)
{
if(x<10)
{
return x;
}
else
{
return (x%10 * persistence(x/10));
}
}
The function persistence itself should return the value of the multiplicative persistence.
It can be defined the following way as it is shown in the demonstrative program.
#include <stdio.h>
size_t persistence( unsigned int x )
{
const unsigned int Base = 10;
if ( ! ( x < Base ) )
{
unsigned int n = 1;
do { n *= x % Base; } while ( x /= Base );
return 1 + persistence( n );
}
else
{
return 0;
}
}
int main(void)
{
unsigned int x = 39;
printf( "persistence( %u ) = %zu\n", x, persistence( x ) );
return 0;
}
The program output is
persistence( 39 ) = 3

How to read only numbers from file until EOF in C

I have this function to find the max and min value of numbers in file with uknown text("ADS 50 d 15"). It works fine with only digits in the file, but when there is a characters it just stops.
{
int n;
int min = INT_MAX, max = INT_MIN;
int flag = 0;
rewind(f);
while (fscanf(f, "%d", &n) != EOF)
{
if (ferror(f))
{
perror("Error:");
}
if (flag == 0)
{
min = n;
max = n;
flag = 1;
}
if (min>n)
min = n;
if (max<n)
max = n;
}
printf("\nMax value: %d\nMin value: %d\n", max, min);
}
fscanf will return EOF after reaching the end of the file. It will return 1 on successful scanning an integer. If the input is not an integer, it will return 0 and the problem input has to be removed.
{
int n;
int min = INT_MAX, max = INT_MIN;
int result = 0;
char skip = 0;
rewind ( f);
while ( ( result = fscanf ( f, "%d", &n)) != EOF)
{
if (result == 0)
{
fscanf ( f, "%c", &skip);//remove a character and try again
}
else
{
if (min>n)
min = n;
if (max<n)
max = n;
}
}
printf("\nMax value: %d\nMin value: %d\n", max, min);
Try the following approach as it is shown in this demonstrative program. You have to use the fscanf instead of scanf used in this program.
#include <stdio.h>
#include <ctype.h>
int main( void )
{
int min, max;
size_t n = 0;
while ( 1 )
{
char c;
int x = 0;
int success = scanf( "%d%c", &x, &c );
if ( success == EOF ) break;
if (success != 2 || !isspace( ( unsigned char )c ) )
{
scanf("%*[^ \t\n]");
clearerr(stdin);
}
else if ( n++ == 0 )
{
min = max = x;
}
else if ( max < x )
{
max = x;
}
else if ( x < min )
{
min = x;
}
}
if ( n )
{
printf( "\nThere were enetered %zu values\nmax value: %d\nMin value: %d\n",
n, max, min );
}
return 0;
}
If the input looks like
1 2 3 4 5a a6 7 b 8
then the output will be
There were enetered 6 values
max value: 8
Min value: 1

C program error - outputting 0

I have been trying to get this to work for days now and I still cannot figure out the error. When I output the code, it prints, but it will not find the amicable pairs (divisor of first == second, and vice versa).
#include <stdio.h>
#include <stdlib.h>
#define _USE_MATH_DEFINES
#include <math.h>
int sumDivisors( int num );
int sumDivisors( int num )
{
int counter, total;
for( counter = 1; counter < num; counter++ )
{
if( num % counter == 0 )
{
total += counter;
}
}
return ( total );
}
int main( void )
{
int higher, lower, lowx, lowy, x, y, numOfPairs = 0;
printf( "This program finds all amicable numbers within a range. \n" );
printf( "Please enter a lower limit: \n" );
scanf( "%d", &lower );
printf( "Please enter a higher limit: \n" );
scanf( "%d", &higher );
for( lowx = lower; lowx <= higher; lowx++ )
{
for( lowy = lower; lowy <= higher; lowy++ )
{
if( sumDivisors( lowx ) == sumDivisors( lowy ) )
{
numOfPairs++;
printf( "Pair #%d: (%d, %d)\n", numOfPairs, lowx, lowy );
}
}
}
printf( "There are %d amicable pairs from %d to %d\n", numOfPairs, lower, higher );
system("pause");
return ( 0 );
}
You are not assigning any value to total in your code:
int sumDivisors( int num )
{
int counter, total;
for( counter = 1; counter < num; counter++ )
{
if( num % counter == 0 )
{
total += counter;
}
}
return ( total );
}
so it contains garbage not predictable value!
it should be like: int counter, total = 0;

Resources