Help me in these programs(C programing)

theserpent

Firecracker to the moon
I need help in these programs i can't figure out how to do it :(
Fuctions:With Arguements and with returns IN C PROGRAMING
1)Sum of digits of a number untill the resulting value is a single digit no
2)Find the frequency of a digit in a given number
3)To find the no of odd and even in a given Number

I know these are easy programs, but how to do them using functions, I don't know

So the 1st program
#include<stdio.h>
void sum(int,int)
void main()
{
int p,sum,n,c,d;
enter the number(n)
if(n==0)
printf("Not possible");
else
p=sum(n)
print p

3)
#include<stdio.h>
void sum(int,int)
main()
{
int p,sum,n,c;
scanf("%d",&n)
if(n==0)
printf("Not possible");
else
p=sum(n)
}
int sum(int n,int c)
{
int s,e=0,o=0;
s=n%10;
if(n%2==0)
e=e+1;
else
o=o+1;
n=n/10;
Now what????????
 

krishnandu.sarkar

Simply a DIGITian
Staff member
Well I'll only show you the procedure if you don't know. But surely not write the programs for you...

So here's an example for the 1st program...do the rest by following the example...

Code:
#include<stdio.h>

int sum(long);

int main()
{
    long n, result;

    printf("Enter a Number : ");
    scanf("%ld",&n);

    result = sum(n);
    printf("Final Single Digit Sum = %ld",result);

    return 0;
}

int sum(long t)
{
    long sum;
    int r;

    while(t>9)
    {
        sum=0;
        do
        {
            r=t%10;
            sum=sum+r;
            t=t/10;
        }while (t!=0);
        t=sum;
    }

    return t;
}
 
OP
theserpent

theserpent

Firecracker to the moon
Ok thanks :) that cleared some of my doubts
Is my 3rd program right?

#include<stdio.h>
void sum(int)
main()
{
int p,sum,n,c;
scanf("%d",&n)
if(n==0)
printf("Not possible");
else
p=sum(n)
}
int sum(int a)
{
int s,e=0,o=0;
s=n%10;
if(n%2==0)
e=e+1;
else
o=o+1;
n=n/10;
Now what???????
2)
Code:
#include<stdio.h>
void sum(int,int)
void main()
{
int n,c,p;
scanf("%d",&n);
scanf("%d",&c); <------Number to find.
p=sum(n,c);
printf("%d",p);
}
int main(int     ) [THIS part is difficulty]
{
int s=0,d;
while(n!=0)
{
d=n%10;
if(d==c)
s=c+1;
n=n/10;
return s;
}

Is this correct?
 

krishnandu.sarkar

Simply a DIGITian
Staff member
3)To find the no of odd and even in a given Number

I really don't understand what you mean by this? Can you please simplify? Do you mean No of Odd and Even Number in a Given Range?

3)
#include<stdio.h>
void sum(int,int)
main()
{
int p,sum,n,c;
scanf("%d",&n)
if(n==0)
printf("Not possible");
else
p=sum(n)
}
int sum(int n,int c)
{
int s,e=0,o=0;
s=n%10;
if(n%2==0)
e=e+1;
else
o=o+1;
n=n/10;
Now what????????

Why are you doing the line s=n%10? you are not even using the s after that line. So what's the point of doing that line?
 
OP
theserpent

theserpent

Firecracker to the moon
^ what i mean is
Say i entered 232
The no of odd is 1
and even is 2

I really can't understand what to do in the 2nd part.. though i know the program :|
 

krishnandu.sarkar

Simply a DIGITian
Staff member
^ what i mean is
Say i entered 232
The no of odd is 1
and even is 2

I really can't understand what to do in the 2nd part.. though i know the program :|

Code:
#include<stdio.h>

int a = 0, b = 0;
void odd_even(int);

int main()
{
    int n;
    printf("Enter a Number : ");
    scanf("%d", &n);

    odd_even(n);

    printf("The Number of Even occurence is %d and Number of Odd occurence is %d", a, b);

    return 0;
}

void odd_even(int i)
{
    while (i)
    {
        if ((i%10) % 2 == 0)
            a++;
        else
            b++;
        i = i/10;
    }
}
 
OP
theserpent

theserpent

Firecracker to the moon
We dint learn to use it directly like odd_even.
If possible tell me what did i do wrong in the program i wrote?
 
OP
theserpent

theserpent

Firecracker to the moon
Umm what i mean is

odd_even(n);

Declaring or storing the variable like this way wasn't thought to us.
Anyways thanks and is the program i wrote correct? whats wrong in it?

And is this one correct???
#include<stdio.h>
void sum(int,int)
void main()
{
int n,c,p;
scanf("%d",&n);
scanf("%d",&c); <------Number to find.
p=sum(n,c);
printf("%d",p);
}
int main(int a) [THIS part is difficulty]
{
int s=0,d;
while(a!=0)
{
d=a%10;
if(d==c)
s=c+1;
a=a/10;
return s;
}

And is this one correct???
#include<stdio.h>
void sum(int,int)
void main()
{
int n,c,p;
scanf("%d",&n);
scanf("%d",&c); <------Number to find.
p=sum(n,c);
printf("%d",p);
}
int main(int a) [THIS part is difficulty]
{
int s=0,d;
while(a!=0)
{
d=a%10;
if(d==c)
s=c+1;
a=a/10;
return s;
}
 

krishnandu.sarkar

Simply a DIGITian
Staff member
I can't understand what you are trying to do??

1. You have declared a function sum() but not defining it anywhere. It'll give you error.

2. You have defined two main() function which is not possible at all.

3. You are not using function at all.

Well you are saying they didn't taught you using functions like odd_even(n) but then how are you supposed to do the programs using functions? In that case ask your teachers how are you supposed to do the programs using functions if you haven't taught us functions yet.

If you mean they didn't taught you functions with parameters / arguments, then make n global then you don't need to pass n as parameter.

In that case use...

Code:
#include<stdio.h>

int i, a = 0, b = 0;
void odd_even(void);

int main()
{
    //int n; You don't need this line anymore
    printf("Enter a Number : ");
    scanf("%d", &i);

    odd_even();

    printf("The Number of Even occurence is %d and Number of Odd occurence is %d", a, b);

    return 0;
}

void odd_even()
{
    while (i)
    {
        if ((i%10) % 2 == 0)
            a++;
        else
            b++;
        i = i/10;
    }
}
 
OP
theserpent

theserpent

Firecracker to the moon
I'm totally confused :(.
Keep that odd and even program out for now(I'll clarify with my friend Too)
Is the 2nd program correct
 

krishnandu.sarkar

Simply a DIGITian
Staff member
I'm totally confused :(.
Keep that odd and even program out for now(I'll clarify with my friend Too)
Is the 2nd program correct

The function for second program would be like

Code:
#include<stdio.h>

int check(int, int);

int main()
{
    int n, a, res;
    printf("Enter a Number : ");
    scanf("%d", &n);

    printf("Enter The Number To Search For Occurence : ");
    scanf("%d", &a);

    res = check(n, a);

    printf("The Number of Occurence of %d in %d is %d", a, n, res);

    return 0;
}

int check(int num, int che)
{
    int rem, occ=0, quo=num;
    while(quo>0)
    {
        rem=quo%10;
        quo=quo/10;
        if(che==rem)
            occ++;
    }
    return occ;
}

Looks like all of your programs have been done. Now just copy paste and submit the assignment :p

But if you really want to learn C and understand what's going on post it here, many members are there to help you out.
 

krishnandu.sarkar

Simply a DIGITian
Staff member
^^What type of explanation? You have all the three programs. Please read and tell us exact position which you are not getting.

PS : Hope you know basics of C (variables, printf, scanf, functions etc.) ?
 
OP
theserpent

theserpent

Firecracker to the moon
Well as i said before it's the 2nd part of every program i cant understand i.e
int check(int num, int che) <----------- this part
 

krishnandu.sarkar

Simply a DIGITian
Staff member
Well, you can pass values to functions by parameters.

So a function...

int sum();

means...

return type : Integer
function name : sum;
parameters it takes : none i.e. void.

Let me define a statement here, in C you don't need to mention void explicitly if there's nothing, but in C++ you need to. So you get the thing more comfortable in C++

so int sum() actually means int sum(void)

So lets say a program..

Code:
int a = 10; b = 20;
int sum();

int main()
{
	int c = sum();
}

int sum()
{
	return a + b;
}

Adds two values in the variables a and b.

But this program is static. Isn't it?

Because whenever you'll run the program you'll get 30 as answer.

If you want to add other values you need change the source code. Also the variables are global. You don't use global variables for this type of things. This is not recommended.

(For now forget scanf() to get the user input)

Now comes function parameters. You can pass the values to the function as parameters upon which it should perform it's task.

This makes the function re-usable. In the above program the function will only add a and b even if you manage to take input using scanf().

What if you want a sum function which can add any values(variables) that you wish to.

So think yourself, what should you NEED NOT DO??

Answer is simple, you don't add that a and b always, instead do something like the function should add the values which you'd like at realtime, dynamically...

So here comes function parameters... Let's take this as example...

Code:
void sum(int, int);

int main()
{
	int a = 10; b = 20;
	int c = sum(a, b);
	
	int m = 50; n = 100;
	int o = sum(m, n);
	
	int i = 200; j = 300;
	int k = sum(i, j);
}

int sum(int x, int y)
{
	return x + y;
}

So the line void sum(int, int); says, it's a function declaration of function named sum, whose return type is int and which takes two parameters of type int.

Code:
int sum(int x, int y)
{
	return x + y;
}

is the function definition, which means, whatever values you pass in the function, it'll store them in x and y and add them and return them the addition result.

So in first line int c = sum(a, b); it passes 10 to x and 20 to y

In 2nd line int o = sum(m, n); it passes 50 to x and 100 to y

In 3rd line int k = sum(i, j); it passes 200 to x and 300 to y.

So see, the function became resuable...

But in first program...if we call the sum function 100 of times, all the times the result will be 30.

One more difference is there...which seems logical...see you are re-using the function and are not writing same code 100's of times for doing same thing...

Now lets compare the first program with this... What if you want to do the same in first function...??

Code:
int a = 10; b = 20;
int m = 50; n = 100;
int i = 200; j = 300;
void sumab();
void summn();
void sumij();

int main()
{
	int c = sumab();
	
	int o = summn();
	
	int k = sumij();
}

int sumab()
{
	return a + b;
}

int summn()
{
	return m + n;
}

int sumij()
{
	return i + j;
}

See, if you are not using function parameters... you need to writing same code to add diff. variables.

So it makes the program more prone to bugs and anyone who'll look at it will think it's a bad program...even including you. Why you should write same code for doing same things?

It's for 3...what about if you are adding 100 variables?? You are going to write the function 100 time with diff name??

Of course not...right?

Hope that helps.
 

desiJATT

Away from Forums, Again!
Another example of bad teachers in colleges, they didn't teach properly hence you are confused. Krishnandu gave a brief explanation to functions, but that will be of no use if you don't pick up your book and actually read about it. First clear out everything about functions from your teacher, then move on to real programming.
 
OP
theserpent

theserpent

Firecracker to the moon
Thanks Krishna :)

Well the doubt

int check(int num, int che)
{
int rem, occ=0, quo=num;
while(quo>0)
{
rem=quo%10;
quo=quo/10;
if(che==rem)
occ++;
}
return occ;
}
Say i entered n= 5667 and number to search 6
So, what's stored in int num and int che here --> int check(int num, int che)
So in int num i have 5667 and che = 6??
 
Top Bottom