0% found this document useful (0 votes)
50 views4 pages

ADA Lab File Work

The document contains code snippets and explanations for three programming problems: 1) A C program to calculate the sum of integers input by the user using a for loop. It prompts the user for the number of integers, reads the integers in a loop, and calculates the running sum. 2) A recursive C function to calculate the sum of integers from 1 to n. It uses recursion by calling itself with decreasing values of n until the base case of n = 0 is reached. 3) A C program to calculate the factorial of a given number using an iterative approach with a while loop. It initializes the factorial to 1 and multiples it by integers from 1 to the given number.

Uploaded by

Wrestling Worm
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
50 views4 pages

ADA Lab File Work

The document contains code snippets and explanations for three programming problems: 1) A C program to calculate the sum of integers input by the user using a for loop. It prompts the user for the number of integers, reads the integers in a loop, and calculates the running sum. 2) A recursive C function to calculate the sum of integers from 1 to n. It uses recursion by calling itself with decreasing values of n until the base case of n = 0 is reached. 3) A C program to calculate the factorial of a given number using an iterative approach with a while loop. It initializes the factorial to 1 and multiples it by integers from 1 to the given number.

Uploaded by

Wrestling Worm
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

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