Newbie math checker for C. Why won't it work? - c

#include <stdio.h>
int main(void)
{
printf("What is the sum of 5 + 5?\n");
GetInt();
if int == 10;
printf("Correct.\n");
}

#include <stdio.h>
int main(void)
{
printf("What is the sum of 5 + 5?\n");
GetInt();
if(int == 10){
printf("Correct.\n");
}
return 0;
}
GetInt() is undefined, you also have some very basic syntax errors(that I've corrected) for example if int == 10; int is a data type you can't compare it to a number and even if you could by putting a ; at the end you basically putting and empty conditional, it won't do anything.
#include <stdio.h>
int main(void)
{
int a;
printf("What is the sum of 5 + 5?\n");
scanf("%d", &a);
if(a == 10){
printf("Correct.\n");
}
return 0;
}
This is what you want, now you should really read back the basics of C.

I never use ; with if statement but If I write this code I will write like this
int main(void)
{
int res;
printf("What is the sum of 5 + 5?\n");
scanf("%d",&res)
if (res == 10)
{
printf("Correct.\n");
}
return 0;
}

Related

I wanted to print factorial of no. using recursion but its not working in C

#include <stdio.h>
int factorial(int b)
{
return (b * factorial(b - 1));
}
int main()
{
int num;
printf("Enter a no:\n");
scanf("%d", &num);
printf("factorial is %d\n", factorial(num));
return 0;
}
You are missing a base case. Also, you don't want negative numbers to be input, so make the function arguments and return type to be of unsigned int.
A base case is a condition to end the recursion. Since it wasn't present, the program was executing even for negative values of b.
#include <stdio.h>
unsigned int factorial(unsigned int b)
{
if (b <= 1)
{
return b;
}
return (b * factorial(b - 1));
}
int main()
{
int num;
printf("Enter a no:\n");
scanf("%d", &num);
printf("factorial is %u\n", factorial(num));
return 0;
}

How do I use Do While to stop a loop?

I am writing a simple program that takes an input and adds it to a sum and then prints it, but then asks for another input and also adds that to the sum. However when 0 is in the input, the program should stop. That part is not working, here is what I have tried.
#include <stdio.h>
int main(){
int n, summa, t;
summa = 0;
t=1;
do{
scanf("%d", &n);
if(n==0){t=0;
}
summa = n + summa;
printf("%d\n", summa);
}
while(t == 0);{return 0;}
return 0;}
I believe you intended your conditional to be t != 0.
Here's a reformatted version of your code with the new conditional, see if that functions as you expected.
#include <stdio.h>
int main() {
int n, summa, t;
summa = 0;
t = 1;
do {
scanf("%d", &n);
if (n == 0) {
t = 0;
}
summa = n + summa;
printf("%d\n", summa);
}
while(t != 0);
return 0;
}

the code here works perfectly in codeblocks but in codechef IDE it gives SIGTSTP (runtime error and shows a lot of 0s in the output)

LINK TO QUESTION : https://www.codechef.com/problems/LUCKFOUR
#include <stdio.h>
#include<stdlib.h>
int main() {
int T,ans;
scanf("%d",&T);
while(T) {
int num,count = 0;
scanf("%d",&num);
while(num) {
ans = num % 10;
num = num/10;
if( ans == 4) {
count++;
}
}
printf("%d\n",count);
T--;
}
return 0;
}
Runs normally for codeblocks:it takes T first and then takes input from the user and prints the no. of 4s in that input and repeats the process of taking inputs and printing out the no. of 4s T times
I did a little modification in your solution, and it passed. Just pay attention to the constraints, because sometimes int can cause an overflow. Take a look:
#include <stdio.h>
#include <stdlib.h>
int main() {
long long int T;
scanf("%lld", &T);
while(T--) {
long long int num, count=0;
int ans;
scanf("%lld",&num);
while(num) {
ans = num % 10;
num = num/10;
if( ans == 4) {
count++;
}
}
printf("%d\n",count);
}
return 0;
}
Submission image

the if statement is not getting executed in this program to find if an int is palindrome or not

This is a program to find whether a given number is a palindrome or not. In this case the condition in the if statement even though it is true isnt getting executed and the program is terminationg.
#include <stdio.h>
#include <math.h>
int count(int n)
{
int counts;
counts = 0;
do{
n = n/10;
counts++;
}while(n!=0);
return counts;
}
int main()
{
int i=0;
int numb;
printf("Enter a number: \n");
scanf("%d",&numb);
int num1=numb;
int num2=numb;
int c;
int power;
int n1,n2;
int c_num = count(numb);
do{
c = count(num1);
if((num1/pow(10,c-1))==(num2%10)){
power = pow(10,c-1);
n1 = num1 % power;
n2 = num2/10;
num1 = n1;
num2 = n2;
i++;}
else{
num1=0;
num2=0;
}
}while(num1!=0);
if (c_num==i){
printf("It is a Palindrome number.");
}
else{
printf("It is not a Palindrome number.");
}
return 0;
}
Answering exactly what you asked for:
pow() returns a double and you are comparing that to an int, that is the problem.
Simple Solution:
You missed the cast to int, do this:
if ((num1 / (int)pow(10, c - 1)) == (num2 % 10))

Factorial calculator using functions in C

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;
}

Resources