Showing posts with label functions. Show all posts
Showing posts with label functions. Show all posts

Saturday, May 21, 2011

C Program to find sum of digits in number using recursion

int sumd(int);

int sumd(int x)
{
if(x==0)
return 0;
else
return (x%10)+sumd(x/10);
}

int main()
{
int a;
printf("Enter a number :");
scanf("%d",&a);
int b=sumd(a);
printf("\nSum of digits= %d",b);
getch();
}

C program with user defined functions

/*Code a program to accept an integer value. Pass this value to
two functions - one to compute square value, and the other to
compute cube value. Print the results from the functions.*/


#include
#include
int square(int);
int cube(int);

int main()
{
int a;
printf("Enter a number :");
scanf("%d",&a);
int b=square(a);
printf("\nSquare= %d",b);
b=cube(a);
printf("\nCube= %d",b);
getch();
}

int square(int x)
{
return x*x;
}

int cube(int x)
{
return x*x*x;
}

C program to reverse digits of number using function


Simple C Program reverse digits of number using function

int reversedigit(int);

int main()
{
int a;
printf("Enter a number :");
scanf("%d",&a);
int b=reversedigit(a);
printf("\nreverse= %d",b);
getch();
}
reversedigit(int x)
{
int y=0;
while(x)
{
y=y*10+x%10;
x=x/10;
}
return y;
}

C program to explain use of function

#include
#include
void print(void);
int main()
{
int i;
for(i=0;i<5;i++)
{
print();
printf("\n");
}

getch();
}
void print()
{
printf("Welcome to c");
}