0% found this document useful (0 votes)
14 views5 pages

C Programs

C program

Uploaded by

lakshmism46
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)
14 views5 pages

C Programs

C program

Uploaded by

lakshmism46
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
You are on page 1/ 5

1. Write a program in C to add two numbers using pointers.

#include <stdio.h>
int main()
{
int a, b, *p, *q, sum;

printf("\n\n Pointer : Add two numbers :\n");


printf("--------------------------------\n");

printf(" Input the first number : ");


scanf("%d", &a);
printf(" Input the second number : ");
scanf("%d", &b);

p = &a;
q = &b;

sum = *p + *q;

printf(" The sum of the entered numbers is : %d\n\n",sum);

return 0;
}

2. Write a program in C to add numbers using call by reference.


#include <stdio.h>
int addTwoNumbers(int *, int *);

int main()
{
int fno, sno, sum;

printf(" Input the first number : ");


scanf("%d", &fno);
printf(" Input the second number : ");
scanf("%d", &sno);
sum = addTwoNumbers(&fno, &sno);
printf(" The sum of %d and %d is %d\n\n", fno, sno, sum);
return 0;
}
int addTwoNumbers(int *n1, int *n2)
{
long sum;
sum = *n1 + *n2;
return sum;
}
3. Write a program in C to find the maximum number between two numbers using a
pointer.
#include <stdio.h>
void main()
{
int fno,sno,*ptr1=&fno,*ptr2=&sno;

printf(" Input the first number : ");


scanf("%d", ptr1);
printf(" Input the second number : ");
scanf("%d", ptr2);

if(*ptr1>*ptr2)
{
printf("\n\n %d is the maximum number.\n\n",*ptr1);
}
else
{
printf("\n\n %d is the maximum number.\n\n",*ptr2);
}

4. Write a program in C to store n elements in an array and print the elements using
pointer.
#include <stdio.h>
int main()
{
int arr1[25], i,n,*ptr;
printf(" Input the number of elements to store in the array :");
scanf("%d",&n);

printf(" Input %d number of elements in the array :\n",n);


for(i=0;i<n;i++)
{
scanf("%d",arr1[i]);

ptr=arr1;

printf(" The elements you entered are : \n");


for(i=0;i<n;i++)
{
printf("%d \t",*ptr);
ptr++;
}
return 0;
}
5. Write a program in C to store n elements in an array and find the sum array elements
using pointer.
#include <stdio.h>
void main()
{
int arr1[25], i,n,*ptr;
printf(" Input the number of elements to store in the array :");
scanf("%d",&n);

printf(" Input %d number of elements in the array :\n",n);


ptr=arr1;
for(i=0;i<n;i++)
{
scanf("%d",arr1[i]);
sum=sum+*ptr;
ptr++;
}
printf(" The sum of array elements is :%d\n",sum);
}

6. Write a program in C to find the factorial of a given number using pointers (call by
reference)

#include <stdio.h>
void findFact(int,int*);
int main()
{
int fact;
int num1;
printf(" Input a number : ");
scanf("%d",&num1);

findFact(num1,&fact);
printf(" The Factorial of %d is : %d \n\n",num1,fact);
}

void findFact(int n,int *f)


{
int i;

*f =1;
for(i=1;i<=n;i++)
*f=*f*i;
}
7. Write a program in C to sort an array using Pointer.
#include <stdio.h>
void main()
{
int a[20],i,j,tmp,n,*p;
printf(" Enter number of elements\n");
scanf("%d",&n);

printf(" Enter %d number of elements: \n",n);


for(i=0;i<n;i++)
{
scanf("%d",a+i);
}
p=a; // p store the base address of array a
for(i=0;i<n;i++)
{
if( *p > *(p+1))
{
tmp = *p;
*p = *(p+1);
*(p+1) = tmp;
}
}
printf("\n The elements in the array after sorting : \n");
p=a; // p store the base address of array a
for(i=0;i<n;i++)
{
printf("%d \t",*p);
p++;
}
}

8. Write a program in C to print the elements of an array in reverse order.

#include <stdio.h>
int main()
{
int arr1[25], i,n,*ptr;
printf(" Input the number of elements to store in the array :");
scanf("%d",&n);

printf(" Input %d number of elements in the array :\n",n);


for(i=0;i<n;i++)
{
scanf("%d",arr1[i]);
}
ptr=arr1[n-1];
printf(" The elements you entered are : \n");
for(i=0;i<n;i++)
{
printf("%d \t",*ptr);
ptr--;
}
return 0;
}

9. C Program to Add Two Complex Numbers by Passing Structure to a Function


#include <stdio.h>

struct complex {

float real;
float imag;
} ;

int main() {
struct complex n1, n2, n3;

printf("Enter1st complex number real and imaginary parts \n");


scanf("%f %f", &n1.real, &n1.imag);
printf("\nEnter 2nd complex number real and imaginary parts \n");
scanf("%f %f", &n2.real, &n2.imag);

n3.real = n1.real + n2.real;


n3.imag = n1.imag + n2.imag;

printf("Sum = %.1f + %.1fi", n3.real, n3.imag);


return 0;
}

You might also like