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();
}
This blog will cover all important aspects of 'C' 'C++', 'Data Structures in C' and other Technical stuffs. In this blog you will find good C Interview Questions Answers. I will be posting both multiple choice and subjective type C interview questions and answers. Tutorials will be posted from time to time that will focus on problem solving.
Showing posts with label functions. Show all posts
Showing posts with label functions. Show all posts
Saturday, May 21, 2011
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;
}
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 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");
}
#include
void print(void);
int main()
{
int i;
for(i=0;i<5;i++)
{
print();
printf("\n");
}
getch();
}
void print()
{
printf("Welcome to c");
}
Subscribe to:
Posts (Atom)