You are on page 1of 4

PRACTICAL-1(A)

AIM: WAP TO FIND THE SUM OF A NO.S


PSEUDO CODE:
SumToN()
Begin
Read: n;
Set sum = 0;
for i = 1 to n by 1 do
Set sum = sum + i;
endfor
Print: sum;
End
Source code:
#include <stdio.h>
int main()
{ int n, sum = 0, c, value;
printf("How many numbers you want to add?\n");
scanf("%d", &n);
printf("Enter %d integers\n", n);
for (c = 1; c <= n; c++)
{ scanf("%d", &value);
sum = sum + value;}
printf("Sum of the integers = %d\n", sum);
return 0;}
OUTPUT:
PRACTICAL-1(B)
AIM: WAP TO FIND THE SUM OF A NO.S USING RECURSION
PSEUDO CODE:
double sum( int n )
{
if ( n ==0 ) // base case
return 0;
// recursive case
return n + sum( n - 1 );
}
Source code:
#include <stdio.h>
void display(int);
int main()
{ int num, result;
printf("Enter the Nth number: ");
scanf("%d", &num);
display(num);
return 0;}
void display(int num)
{ static int i = 1;
if (num == i)
{ printf("%d \n", num);
return; }
else
{ printf("%d ", i);
i++;
display(num); } }
OUTPUT:
PRACTICAL-2(A)
AIM: WAP TO FIND THE FACTORIAL OF A NO. USING ITERATION
PSEUDO CODE:
Read number
Fact = 1
i=1
WHILE i<=number
Fact=Fact*i
i=i+1
ENDWHILE
WRITE Fact

Source code:
#include <stdio.h>
int main()
{ int c, n, fact = 1;
printf("Enter a number to calculate its factorial\n");
scanf("%d", &n);
for (c = 1; c <= n; c++)
{fact = fact * c;
printf("Factorial of %d = %d\n", n, fact);
return 0;}
getch();}
OUTPUT:

You might also like