DIGIT-ARC read-only archive · snapshot: July 23, 2026

Help me in these programs(C programing)

28 posts · originally from https://geek.digit.in/community/threads/help-me-in-these-programs-c-programing.166071/

theserpent
theserpent Original post #1
Nov 12, 2012 at 3:45 PM
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
Nov 12, 2012 at 3:49 PM
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;
}
theserpent
Nov 12, 2012 at 4:09 PM
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
Nov 12, 2012 at 5:06 PM
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?
theserpent
Nov 12, 2012 at 5:16 PM
^ 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 :|
P
Nov 12, 2012 at 5:18 PM
Ok, I get it, you want to check the frequency of odd and even digits that the entered number consists of!
theserpent
Nov 12, 2012 at 5:27 PM
^^ Yeah so is the program i wrote correct?
krishnandu.sarkar
Nov 12, 2012 at 5:31 PM
^ 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;
    }
}
theserpent
Nov 12, 2012 at 5:40 PM
We dint learn to use it directly like odd_even.
If possible tell me what did i do wrong in the program i wrote?
P
Nov 12, 2012 at 5:44 PM
We dint learn to use it directly like odd_even.
If possible tell me what did i do wrong in the program i wrote?

What do you mean by that? What did you not understand in the programs that krishnandu.sarkar wrote?
theserpent
Nov 12, 2012 at 5:54 PM
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
Nov 12, 2012 at 6:05 PM
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;
    }
}
theserpent
Nov 12, 2012 at 6:10 PM
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
Nov 12, 2012 at 7:39 PM
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.
theserpent
Nov 12, 2012 at 7:42 PM
Yeah thanks :)
Now can anyone explain me?
Sorry for troubling all of you
krishnandu.sarkar
Nov 12, 2012 at 8:01 PM
^^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.) ?
theserpent
Nov 12, 2012 at 8:05 PM
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
Nov 12, 2012 at 10:23 PM
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
desiJATT #19
Nov 13, 2012 at 5:22 PM
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.
theserpent
Nov 18, 2012 at 10:40 AM
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??
krishnandu.sarkar
Nov 18, 2012 at 11:29 AM
^^ Right.

n = num = quo = 5667
che = 6
theserpent
Nov 18, 2012 at 12:22 PM
So how is to accessed her? so in the 2nd part they call it up?

Now it kinda makes sense
Thanks
krishnandu.sarkar
Nov 18, 2012 at 12:55 PM
Let me tell you line by line if that makes more sense

#include<stdio.h> //Needs No Explanation

int check(int, int); //The Function Declaration

int main() //Main
{
int n, a, res;
printf("Enter a Number : ");
scanf("%d", &n); //Get a number from user and store it in n. Say user entered 12345.

printf("Enter The Number To Search For Occurrence : ");
scanf("%d", &a); //Get the number to search from n (i.e. 12345). Say user entered 3.

res = check(n, a); //Call the Check function. Check() returns the number of occurrence in integer so we store it (the return value / result) in res which is a integer declared above. Check() takes two integer as parameters... 1. The number, 2. The Number to Search from the first number. So it's actually res = check(12345, 3);

//At this point when the function gets called, the control jumps to the function definition point. i.e. where the function body is defined.

printf("The Number of Occurrence of %d in %d is %d", a, n, res); //The result is shown to the user.

return 0;
}

int check(int num, int che) //Control jumps here on calling the function and passes the values to the variables (parameters) mentioned in the () to hold the value. So now num = 12345, che = 3. NOTE : This num and che is local to this function. Outside this function body these variables have no existence.
{
int rem, occ=0, quo=num; //initializing quo as num. So quo = 12345.
while(quo>0) //if quo is > 0 i.e. if 12345 > 0
{ //The whole logic is implemented here to find the times of occurrence of 3 in 12345.
rem=quo%10;
quo=quo/10;
if(che==rem)
occ++;
} //After the whole calculation the result is stored in occ. So we return occ in the next line
return occ; //NOTE : This return value again must be hold somewhere. Right? To hold this value we wrote res = check(n, a). So this occ gets stored in res above in main().
}
theserpent
Nov 18, 2012 at 3:05 PM
Thanks a lot Bro
krishnandu.sarkar
Nov 18, 2012 at 6:19 PM
You are welcome..!! Hope that helps. Do not hesitate to ask questions if you are still not clear.

In the mean time, if you get time, follow Head First C to learn the Language along with your suggested book by College (Probably Kaneetkar) to only pass the semester exam :)
theserpent
Nov 24, 2012 at 5:38 PM
Here some more basic programs, are they right?
1)Lower case to upper case(Without using functions)
Code:
#include<stdio.h>
#include<string.h>
main()
{
char a[100];
int i;
gets(a);
for(i=0;a[i]!='\0';i++)
{
for(a[i]>=97&&a[i]<=122)
{
a[i]=a[i]-32; [like say a is 97, so 97-32=65, where 65=A]
}
}
puts(a);
}

2)Upper to Lower

#include<stdio.h>
#include<string.h>
main()
{
char a[100];
int i;
gets(a);
for(i=0;a!='\0';i++)
{
for(a>=65&&a<=90)
{
a=a+32;
}
}
puts(a);
}

3)Check if a word is plaindrome or not using inbuilt functions
EG: ANA,MADAM
Code:
#include<stdio.h>
#include<string.h>
main()
{
char a[100],b[100];
int c;
gets(a);
strcopy(b,a);  [Copying a to b, like i can reverse b)
strrev(b);
c=strcomp(a,b);
if(c==0)
printf("Palindrome");
else
printf("Not a Palindrome");
}

4) Palindrome without functions

How do i do this??

#include<stdio.h>
#include<string.h>
main()
{
int a,b,c;
gets(a);
for(i=0;a!='\o';i++)
{

All the programs are not executing :( , i tried compling them in a online compiler
krishnandu.sarkar
Nov 24, 2012 at 6:32 PM
Here some more basic programs, are they right?
1)Lower case to upper case(Without using functions)
Code:
#include<stdio.h>
#include<string.h>
main()
{
char a[100];
int i;
gets(a);
for(i=0;a[i]!='\0';i++)
{
for(a[i]>=97&&a[i]<=122)
{
a[i]=a[i]-32; [like say a is 97, so 97-32=65, where 65=A]
}
}
puts(a);
}

This one is perfectly fine. But just one mistake. The nested for will be if. IDK why did you write for. Aren't you using your Logic? Why for? You are checking the value is whether between 97 and 122. So it should be if

Code:
#include<stdio.h>
#include<string.h>
main()
{
char a[100];
int i;
gets(a);
for(i=0;a[i]!='\0';i++)
{
    if(a[i]>=97&&a[i]<=122)
    {
        a[i]=a[i]-32; //[like say a is 97, so 97-32=65, where 65=A]
    }
}
puts(a);
}

^^Just corrected your code.

2)Upper to Lower

#include<stdio.h>
#include<string.h>
main()
{
char a[100];
int i;
gets(a);
for(i=0;a!='\0';i++)
{
for(a>=65&&a<=90)
{
a=a+32;
}
}
puts(a);
}



Same fault again. The nested for will be if.

Code:
#include<stdio.h>
#include<string.h>
main()
{
char a[100];
int i;
gets(a);
for(i=0;a[i]!='\0';i++)
{
    if(a[i]>=65&&a[i]<=90)
    {
    a[i]=a[i]+32;
    }
}
puts(a);
}

I don't think you do the debugging part ever. Next time no help will be provided if you don't debug your program with imaginary values.

3)Check if a word is plaindrome or not using inbuilt functions
EG: ANA,MADAM
Code:
#include<stdio.h>
#include<string.h>
main()
{
char a[100],b[100];
int c;
gets(a);
strcopy(b,a);  [Copying a to b, like i can reverse b)
strrev(b);
c=strcomp(a,b);
if(c==0)
printf("Palindrome");
else
printf("Not a Palindrome");
}

From where did you copied this? Do you ever read what error compiler throws? The function names you used are strcopy and strcomp but they are actually strcpy() and strcmp(). Just change that and everything will work fine.

4) Palindrome without functions

How do i do this??

#include<stdio.h>
#include<string.h>
main()
{
int a,b,c;
gets(a);
for(i=0;a!='\o';i++)
{

All the programs are not executing :( , i tried compling them in a online compiler


I'd just say you the logic.
  • Take a String
  • Reverse it using for loop and store it in another string
  • Now check both the char array one by one

The for loop will run from back of the string... i.e. for (i=length_of_string - 1; i >= 0; i--)

(length_of_string - 1) because final character is NULL (\0)

There's another way...
Run a for loop to length_of_string/2 i.e. half and match 1st char with last char then with 2nd char with last -1 char in this way untill the char keeps matching.

If you find mismatch discard the loop and display not palindrome.

Eg.

MADAM
12345

For Loop will run 5/2 = 2
So 1 - 5 (M-M)
2 - 4 (A - A)
Display Palindrome

Hello
12345

For loop will run 5/2 = 2
Match 1 - 5 (H - O)
didn't matched...so no necessary to keep matching. Once unmatched discard the loop and display Non Palindrome.

Code:
#include <stdio.h>
#include <string.h>
int main()
{
char string1[20];
int i, length;
int flag = 0;
printf("Enter a string: ");
scanf("%s", string1);
length = strlen(string1);

for(i=0;i < length ;i++)
{
    if(string1[i] != string1[length-i-1])
        {
        flag = 1;
        break;
        }
    }

if (flag)
{
    printf("%s is not a palindrome ", string1);
}
else
{
    printf("%s is a palindrome ", string1);
}
return 0;
}

^^The program for 2nd Logic. If you want 1st Logic, try it yourself.
theserpent
Nov 24, 2012 at 9:40 PM
Oh crap, forgot its strcpy :/
thanks , Yeah my brain wasn't working so by mistake i wrote for here

From where did you copied this? Do you ever read what error compiler throws? The function names you used are strcopy and strcomp but they are actually strcpy() and strcmp(). Just change that and everything will work fine.

Ahh well, wont happen again, thanks for pointing it out