10 Function حل
10 Function حل
Faculty of Engineering
2nd Level Dep. of Electronics
Engineering
Programming Sheet # 01
(The Functions)
1) Write a program with functions that takes two integer numbers and returns sum of the numbers from the first
function (sum), product of the numbers from the second function (prod) .
#include<stdio.h>
int sum (int, int);
int prod (int, int);
main()
{
int x,y,s,p;
printf("please enter two integer numbers\n");
scanf("%d%d",&x,&y);
s=sum(x,y);
p=prod(x,y);
printf("SUM = %d\nPRODUCT = %d\n",s,p);
}
int sum (int a, int b)
{
int s = a+b;
return s;
}
int prod (int a, int b)
{
int p = a*b;
return p;
}
2) Write a program with functions that takes two integer parameters and returns sum of all numbers between
them from the function (sumall) .
#include<stdio.h>
int sumall (int, int);
main()
{
int x,y,s;
printf("please enter two integer numbers\n");
scanf("%d%d",&x,&y);
s= sumall(x,y);
printf("SUM OF ALL NUMBER BETWEEN THEM = %d",s);
}
int sumall (int a, int b)
{
int i ,sum=0;
for(i=a;i<=b;i++)
sum=sum+i;
return sum;
}
Sheet #10 - The Functions Eng. Ali Al-Ashwal 1
3) Write a program with function (power) which accepts two integers x and y and returns xy .
#include <stdio.h>
int power(int,int);
void main()
{
int x,y,p;
printf("Please enter the values of x and y\n");
scanf("%d%d",&x,&y);
p=power(x,y);
printf("x^y=%d",p);
}
int power(int a,int b)
{
int i,s=1;
for(i=1;i<=b;i++)
s=s*a;
return s;
}
4) Write a program with a function that produces a table of the numbers from 1 to 10, and their squares, and
their cubes.
#include <stdio.h>
void fun();
void main()
{
fun();
}
void fun()
{
float i;
printf("NUMBER\tSQUARE\tCUBE\n");
printf("------\t------\t------\n");
for(i=1;i<=10;i++)
printf("%.2f\t%.2f\t%.2f\n",i,pow(i,2),pow(i,3));
}