Professional Documents
Culture Documents
PPS Imp
PPS Imp
ARMSTRONG NUMBER
.
1
Call by value in C
2. include<stdio.h>
#
3. intmain() include<stdio.h>
#
4. { intmain() {
In call by value method, the value of
#
5. intn1=0,n2=1,n3,i,number;
the actual parameters is copied into
6. printf("Enter the number of .
1 intn,r,sum=0,temp;
the formal parameters. In other words,
elements:"); 2. printf("enter the number=");
we can say that the value of the
7. scanf("%d",&number); 3. scanf("%d",&n);
variable is used in the function call in
8. printf("\n%d %d",n1,n2);//printing 0 4. temp=n;
the call by value method. #In call by
and 1 5. while(n>0)
value method, we can not modify the
9. for(i=2;i<number;++i)//loop starts 6. {
value of the actual parameter by the
from 2 because 0 and 1 are already 7. r=n%10;
formal parameter.
printed 8. sum=sum+(r*r*r);
#In call by value, different memory is
10. { 9. n=n/10;
allocated for actual and formal
11. n3=n1+n2; 10. }
parameters since the value of the
12. printf(" %d",n3); 11. if(temp==sum)
actual parameter is copied into the
13. n1=n2; 12. printf("armstrong number ");
formal parameter. #The
14. n2=n3; 13. else
actual parameter is the argument
15. } 14. printf("not armstrong
which is used in the function call
16. return0; number");
whereas formal parameter is the
17. } 15. return0;
argument which is used in the function
Enter the number of elements:15
definition. 6. }
1 Recursion in C
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377
ecursion is the process which comes into
R
existence when a function callsacopyofitselfto
PRIME NUMBER #include<stdio.h> work on a smaller problem. Any function which
calls itself is called recursive function, and such
.
1 include<stdio.h>
# function calls arecalledrecursivecalls.Recursion
.
1 voidchange(intnum) {
2. intmain(){ 17. involves several numbers of recursive calls.
2. printf("Before adding
3. intn,i,m=0,flag=0; However, it is important to impose a termination
value inside function PALINDROME PROGRAM
4. printf("Enter the number to check condition of recursion. Recursion code is shorter
num=%d \n",num);
.
1 include<stdio.h>
# than iterative code however it is difficult to
prime:"); 3. num=num+100;
2. intmain() understand.Recursioncannotbeappliedtoallthe
5. scanf("%d",&n); 4. printf("After adding value
3. { problem, but it is more useful for the tasks that
6. m=n/2; inside function num=%d
4. intn,r,sum=0,temp; can be defined in terms of similar subtasks. For
7. for(i=2;i<=m;i++) \n", num);
5. printf("enter the number="); Example, recursion may be applied to sorting,
8. { 5. }
6. scanf("%d",&n); searching, and traversal problems.
9. if(n%i==0) 6. intmain() {
7. temp=n;
10. { 7. intx=100;
8. while(n>0)
11. printf("Number is not prime"); 8. printf("Before function
9. {
12. flag=1; call x=%d \n", x);
10. r=n%10;
RecursiveFunction:- Arecursivefunction
13. break; 9. change(x);//passing erforms the tasks by dividing it into the
p
11. sum=(sum*10)+r;
14. } value in function subtasks. Thereisaterminationconditiondefined
12. n=n/10;
15. } 10. printf("After function call in the functionwhichissatisfiedbysomespecific
13. }
16. if(flag==0) x=%d \n", x); subtask. After this, the recursion stops and the
14. if(temp==sum)
17. printf("Number is prime"); 1. return0;
1 final result is returned from the function. The
15. printf("palindrome number ");
18. return0; 12. } case at which the function doesn't recur iscalled
16. else
19. } the base case whereas the instances where the
Before function call x=100 17. printf("not palindrome");
Enter the number to check prime:56 function keeps calling itself toperformasubtask,
efore adding value inside function
B 18. return0;
Number is not prime is called the recursive case. All the recursive
num=100 19. }
functions can be written using this format.
nter the number to check prime:23
E fter adding value inside function
A
Number is prime num=200
After function call x=100 .
1 include<stdio.h>
#
Sum of digits 2. intfibonacci(int);
3. voidmain ()
.
1 #include<stdio.h> Call By Reference in C 4. {
2. intmain() 5. intn,f;
#In call by reference, the address of ACTORIAL OF NUMBER
F
3. { 6. printf("Enter the value of n?");
4. intn,sum=0,m; the variable ispassedintothefunction #include<stdio.h> 7. scanf("%d",&n);
5. printf("Enter a number:"); call as the actual parameter. .
1 intmain() 8. f = fibonacci(n);
6. scanf("%d",&n); 2. { 9. printf("%d",f);
#The value of the actual parameters
7. while(n>0) 3. inti,fact=1,number; 10.
}
8. { can be modified by changing the 11.
intfibonacci (intn)
4. printf("Enter a number: ");
9. m=n%10; formal parameters since the address 5. scanf("%d",&number); 12.
{
10. sum=sum+m; of the actual parameters is passed. 6. for(i=1;i<=number;i++){ 13. if(n==0)
11. n=n/10; 7. fact=fact*i; 14. {
12. } #In call by reference, the memory 15. return0;
8. }
13. printf("Sum is=%d",sum); allocation is similar for both formal 9. printf("Factorial of %d is: 16. }
14. return0; parameters and actual parameters.All %d",number,fact); 17. elseif(n == 1)
15. } 10. return0; 18. {
the operations in the function are
Enter a number:654 1. }
1 19. return1;
Sum is=15 performed on the value stored at the 20. }
address of the actual parameters, and 21. else
nter a number:123
E Output: 22. {
the modified value gets stored at the
Sum is=6 23. return
same address. Enter a number: 5 fibonacci(n-1)+fibonacci(n-2);
24. }
Factorial of 5 is: 120 5. }
2