Welcome To Uday Satya Blog

61 - 64 PROGRAMS



/* **********************************************
         61.To find the sum of n numbers using recursion
   ********************************************** */
#include<*stdio.h>
#include<*conio.h>

int sum(int);
void main()
{
int n,s;
 clrscr();
 printf("Enter the limit\n");
 scanf("%d",&n);
 s=sum(n);
 printf("The sum of the first %d numbers is %d ",n,s);

 getch();
 }
int sum(int n)
{
if(n==0)
 return(0);
 else
 return(n+sum(n-1));
 }

OUTPUT
Enter the limit
6
The sum of the first 6 numbers is 21

/* ***************************************************
       62.To find the sum of digits of a number using recursion
  *************************************************** */
#include
#include

int digi(int);
void main()
{
int n,s;
 clrscr();
 printf("Enter the number to find the sum of the digits\n");
 scanf("%d",&n);
 s=digi(n);
 printf("The sum of the digits of %d is %d ",n,s);

 getch();
 }
int digi(int n)
{
if(n==0)
 return(0);
 else
 return(n%10+digi(n/10));
 }

OUTPUT
Enter the number to find the sum of the digits
3456
The sum of the digits of 3456 is 18

/* ******************************************** 
         63.To swap two numbers using pointers
   ******************************************** */
#include<*stdio.h>
#include<*conio.h>
void swap(int *, int*);

void main()
{
int a,b;
 clrscr();
 printf("Enter the two numbers\n");
 scanf("%d %d",&a,&b);
 printf("Before Swapping\nA=%d\nB=%d",a,b);
 swap(&a,&b);
 getch();
}
void swap(int *p,int*q)
{
int temp;
 temp=*p;
  *p=*q;
  *q=temp;
 printf("\nAfter Swapping\nA=%d\nB=%d",*p,*q);
  }

OUTPUT
Enter the two numbers
5
10
Before Swapping
A=5
B=10
After Swapping
A=10
B=5

/* *****************************************************
       64.To find the sum of elements of a 1D array using pointers
   **************************************************** */
#include<*stdio.h>
#include<*conio.h>

void main()
{
int arr[20],i,n,s=0;
 clrscr();
 printf("Enter the length of the array\n");
 scanf("%d",&n);

 printf("Enter the array elements\n");
 for(i=0;i
 {
scanf("%d",(arr+i));
  s=s+*(arr+i);
 }
 printf("The sum is : %d",s);
 getch();
 }

OUTPUT
Enter the length of the array
5
Enter the array elements
1 2 3 4 5
The sum is : 15

0 comments:

Post a Comment