I am trying to make a program to calculate the area of a rectangle
but the function doesn't return any value
This is the code as in cs50 sandbox
#include <stdio.h>
#include <cs50.h>
int calcrectarea(int len,int wid){
return len*wid;
}
int main(){
printf("enter length here:\n");
int x; scanf("%d",&x);
printf("enter width here:\n");
int y; scanf("%d", &y);
calcrectarea(x,y);
}
Actually, the function returns a value. Maybe you want to see it, so just print the result:
#include <stdio.h>
#include <cs50.h>
int calcrectarea(int len,int wid){
return len*wid;
}
int main(){
printf("enter length here:\n");
int x; scanf("%d",&x);
printf("enter width here:\n");
int y; scanf("%d", &y);
printf("area is: %d", calcrectarea(x,y));
}
Related
#include <math.h>
#include <stdio.h>
int main(){
int num, xval;
int sum=0,j=1,p;
printf("Enter number of terms: ");
scanf("%d",num);
printf("Enter value of x: ");
scanf("%d",xval);
for(int i=1;i<=num;i++){
p=pow(xval,j);
j=j+2;
if(i%2==0)
sum=sum-p;
}
else{
sum=sum+p;
}
}
printf("Sum of the series is: %d",sum);
return 0;
}
The question was to find the sum of n terms of the sin series: sin(x) = x - x3 + x5 – x7.
#include <stdio.h>
#include <conio.h>
int sum();
//find the sum of two numbers entered by the user.
int main(){
int n, m;
printf("Enter two numbers: \n");
scanf("%d", &m);
scanf("%d", &n);
int result = sum(m, n);
printf(result);
getch();
return 0;
}
int sum(m, n){
int c;
c = m+n;
return c;
}
i was just writing a simple program with function but i don't know why it is not running it tells me to debug can someone tell me what is the problem with it
Change int sum() ; to int sum(int, int) ;
Change printf(result) to printf("%d", result) ;
Change int sum(m, n) to int sum(int m, int n) ;(https://i.stack.imgur.com/Z07cx.jpg)
Another way of writing above program is
(https://i.stack.imgur.com/VOXRY.jpg)
#include <stdio.h>
#include <conio.h>
int sum(int n, int m);
//find the sum of two numbers entered by the user.
int main(){
int n, m;
printf("Enter two numbers: \n");
scanf("%d", &m);
scanf("%d", &n);
int result = sum(m, n);
printf("%d",result);
getch();
return 0;
}
int sum(int n, int m){
int c;
c = m+n;
return c;
}
I have to do a short assignment for my introductory C class, where I have to ask for a number "N" from the user and calculate it's factorial. The requirements were for me to create a function with the prototype long int factorial(int N). I managed to do it, but I'm confused as to why my code is working with a specific change I made. Here is my code:
#include <stdio.h>
long int factorial(int);
int main(void)
{
int N;
printf("Enter a number: ");
scanf("%d", &N);
printf("The factorial of %d is: %ld", N, factorial(N));
return 0;
}
long int factorial(int N)
{
long int result=1 ;
int i;
for(i=1; i<=N; i++)
result = result * i;
return result;
}
My code at this point didn't work, and would just return the result of N+1 (if I input 5 for example, it would output 6). I was tweaking random things at this point to see what was the problem, and the removal of "void" in my main function fixed it. The problem is, I don't understand why.
#include <stdio.h>
long int factorial(int);
int main()
{
int N;
printf("Enter a number: ");
scanf("%d", &N);
printf("The factorial of %d is: %ld", N, factorial(N));
return 0;
}
long int factorial(int N)
{
long int result=1 ;
int i;
for(i=1; i<=N; i++)
result = result * i;
return result;
}
Could anyone explain why the removal of void in my code fixed this?
This is my solution to the Prime Generator problem on SPOJ(Sphere Online Judge)(http://www.spoj.com/problems/PRIME1/). I have allocated memory using malloc(), but it is still showing a run-time error(SIGSEGV).
#include <stdio.h>
#include <stdlib.h>
int main(){
int n;
scanf("%d", &n);
while(n--){
int a,b;
scanf("%d %d", &a, &b);
int *z =malloc(sizeof(int)*(b-1));
// Filling the array
int i;
for(i=0;i<b-1;i++){
z[i]=i+2;
}
int k,p;
for(k=0;k<=b-2;k++){
if(z[k]){
if(z[k]>=a){
printf("%d\n", z[k]);
}
for(p=k+z[k];p<=b-2;p+=z[k]){
z[p]=0;
}
}
}
free(z);
}
return 0;
}
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int reverse(int a[20],int n);
int main()
{
int a[20];
int n;
printf("enter the number of elements \n");
scanf("%d",&n);
printf("enter the array elements\n");
for(int i=0;i<n;i++)
{
scanf("%d ",&a[i]);
}
reverse(a,n);
return 0;
}
int reverse(int a[20],int n)
{
for(int i=n-1;i>=0;i--)
{
printf("%d ",a[i]);
}
return 0;
}
here if I input n=4 then during runtime i have to take 5 elements and then it reverses.For eg if i take n=4 and then for no of elements i have to take 1,2,3,4,5 and then only output is coming as 4 3 2 1.Why? is my logic wrong? also in this code I am unable to take the number of elements of arrays in a straight line, like 1 2 3 4.When I am entering the number each number is entering in new line .I am a novice programmer in C and thus having these doubts.Please anyone explain...
The problem with your code is the extra space after %d in your scanf line where you accept array elements i.e.
for(int i=0;i<n;i++)
{
scanf("%d ",&a[i]); //should be scanf("%d",&a[i]);
}
Change that and you're good to go.
Here is your entire program refactored to work correctly:
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int reverse(int a[20], int n)
int main()
{
int a[20];
int n;
printf("enter the number of elements \n");
scanf("%d",&n);
printf("enter the array elements\n");
for (int i=0; i<n; i++)
{
scanf("%d",&a[i]);
}
reverse(a,n);
return 0;
}
int reverse(int a[20], int n)
{
int mid = n/2;
for (int i=0; i < mid; ++i)
{
int temp = a[n-i-1];
a[n-i-1] = a[i];
a[i] = temp;
}
return 0;
}