prowessapps.in Function and Pointers Programs

 C Program to swap the values of two variables.

#include<stdio.h>
void swap(int, int);  //prototype
int main( ){
 int x,y;
 printf("Enter X: ");
 scanf("%d",&x);
 printf("Enter Y: ");
 scanf("%d",&y);
 printf("BEFORE SWAP:\n");
 printf("X=%d and Y=%d",x,y);
 swap(x,y);   //calling
 return 0;
}
//definition
void swap(int x, int y){  
 int temp = x;
 x = y;
 y = temp;
 printf("AFTER SWAP : \n");
 printf("X=%d and Y=%d",x,y);
}
OUTPUT :
Enter X: 15
Enter Y: 28

BEFORE SWAP:
X=15 and Y=28

AFTER SWAP:
X=28 and Y=15

 C Program to Calculate Factorial of a given Number.

#include<stdio.h>
int fact(int); 

int main(){
 int n,res;
 printf("Enter number: ");
 scanf("%d",&n);
 res = fact(n);
 printf("Factorial : %d",res);
 return 0;
}


int fact(int x){
 int i, f=1;
 for(i=1; i<=x; i++){
  f = f*i;
 }
 return f;
}
OUTPUT :
Enter a Number : 5
Factorial = 120

 C Program to find out ncr factor by using a user defined function for factorial.
  FORMULA : ncr = n! / ((n-r)! * r!)

#include<<stdio.h>
int fact(int); 

int main(){
 int n,r, res;
 printf("Enter n: ");
 scanf("%d",&n);
 printf("Enter r: ");
 scanf("%d",&r);
 res = fact(n)/(fact(n-r)*fact(r));
 printf("Result nCr Is: %d",res);
}

int fact(int x){
 int i, f=1;
 for(i=1; i<=x; i++){
  f = f*i;
 }
 return f;
}
OUTPUT :
Enter n: 5 
Enter r: 3
Result nCr Is : 10

 C Program to Calculate Factorial of a given Number using Recursion.

#include<stdio.h>
int fact(int); 

int main(){
 int n,res;
 printf("Enter number: ");
 scanf("%d",&n);
 res = fact(n);
 printf("Factorial : %d",res);
 return 0;
}


int fact(int x){
 if(x==0 || x==1){
  return 1;
 }
 else{
  return x*fact(x-1);
 }
OUTPUT :
Enter a Number : 5
Factorial = 120

 C Program to Calculate HCF and LCM of two Number

#include<stdio.h>
int lcm(int , int);
int hcf(int , int);
int main( ) {
 int a,b;
 printf("Enter two Number : ");
 scanf("%d%d",&a,&b);
 int lc=lcm(a,b);
 int hc =(a>b)?hcf(a,b):hcf(b,a);
 printf("LCM = %d\n",lc);
 printf("HCF = %d\n",hc);
return 0;
}
int lcm(int a, int b) {
 int res = a*b/hcf(a,b);
 return res;
}
int hcf(int a, int b) {
 int rem;
 while( low != 0) {
   rem = a%b;
   a = b;
   b = rem;
 }
 return gre;
}
OUTPUT :
Enter two Number : 15 50
LCM = 150
HCF = 5

 C Program to calculate 1!+2!+3!+4!+5!.

#include<stdio.h>
int fact(int);
int main( ){
 int i, sum=0;
 for(i=1; i<=7; i++){
  sum = sum+fact(i);
 }
 printf("SUM = %d",sum);
 return 0;
} 
int fact(int x){
 if(x==0 || x==1){
  return 1;
 }
 else{
  return x*fact(x-1);
 }
OUTPUT :
SUM = 153


 C Program to calculate 1/1!+2/2!+3^2/3!+4^3/4!+5^4/5!+6^5/6!

#include<stdio.h>
int fact(int);
float power(int,int);
int main( ){
 int i;
 float sum;
 for(i=0; i<=6; i++){
   sum = sum+power(i,i-1)/fact(i);
 }
 printf("Sum = %f\n",sum);
 
return 0;

}


int fact(int x){
 if(x==0 || x==1){
  return 1;
 }
 else{
  return x*fact(x-1);
 }
}
float power(int x, int y){
 int j, result=1;
 for(j=1; j<=y; j++){
  result = x*result;
 }
 return result;
}
OUTPUT :
Sum = 22.174999

 C Program to compute the cosine series using function. =1-x2/2!+x4/4!-x6/6!----

#include<stdio.h>
#include< math.h >
float cal_cos(int, int);
int fact(int);
int main( ) 
{

 int x, n;
 printf("Enter the value of x : ");
 scanf("%d",&x);
 printf("Enter the value of n : ");
 scanf("%d",&n);
 float r = cal_cos(x, n);
 printf("Result = %f ",r);
 return 0;
}
float cal_cos(int a, int n) {
 int j =2 ;
 float sum = 1;
 for(i=1; i<=n; i++)
 {
  if(i%2 ==0)
  {
   sum = sum+pow(a,j)/(float)fact(j);
 }
 else
  {
   sum = sum-pow(a,j)/fact(j);
  }
 }
 return sum;
}

int fact(int x){
 int i, f=1;
 for(i=1; i<=x; i++){
  f = f*i;
 }
 return f;
}
OUTPUT :
Enter the Value of x : 3
 Enter the Value of n : 3
 Result = -1.250000

 C Program to calculate sum of two number using Pointer

#include<stdio.h>
int main( ) {
 int a, b, *p, *q, sum;
 printf("Enter two number : ");
 scanf("%d%d", &a, &b);
 p = &a;
 q = &b;
 sum = *p + *q;
 printf("SUM = %d\n",sum);
 return 0;
}
OUTPUT :
Enter two number : 10 20
SUM = 30

 C Program to swap two Number using Pointer

#include<stdio.h >
int main( ) {
 int x, y, *a, *b, temp;
 printf("Enter the value of X and Y : ");
 scanf("%d%d", &x, &y);
 printf("Before Swapping\nX = %d\nY = %d\n", x, y);
 a = &x;
 b = &y;
 temp = *b;
 *b = *a;
 *a = temp;
 printf("After Swapping\nX = %d\nY = %d\n", x, y);
 return 0;
}
OUTPUT :
Enter the value of X and Y : 10 20
Before Swapping
X = 10
Y = 20
After Swapping
X = 20
Y = 10

 C Program to find the length of String using Pointer

#include<stdio.h>
int main( ) {
 char str[20];
 int length = 0;
 printf("\nEnter any String : ");
 gets(str);
 char* p = str;
 while (*p != '\0') {
  length++;
  p++;
  }
  printf("Length of  %s is : %d", str, length);
  return 0;
}
OUTPUT :
Enter any String : prowess
Length of prowess is : 7

 C Program to Access Array Elements Using Pointer

#include<stdio.h>
int main( ) {
 int arr[5], i;
 printf("Enter Elements of Array:");
 for(i=0;i<5;i++){
  scanf("%d",arr+i);
 }
  printf("You have entered : ");
 for(i=0;i<5;i++){
   printf("%d ",*(arr+i));
 }
 return 0;
}
OUTPUT :
Enter Elements of Array : 1 4 6 7 8
You have entered : 1 4 6 7 8

 C Program to convert binary to decimal number using user define function.

#include<stdio.h>
#include<math.h>
long binToDec(long n);
int main() {
 long bi;
 printf("Enter a binary no. :");
 scanf("%ld", &bi);
 printf("BINARY : %ld \n",bi);
 printf("DECIMAL: %ld",binToDec(bi));
 return 0;
}

long binToDec(long n) {
 int rem;
 long dec = 0, i=0;
 while(n != 0) {
  remainder = n%10;
  n = n/10;
  dec = dec + (rem*pow(2,i));
  ++i;
 }
return dec;
}
OUTPUT :
Enter a binary no. :11011
 BINARY  : 11001
 DECIMAL : 27

 C Program to convert decimal to binary number using user define function.

#include<stdio.h>
#include<math.h>
long decToBin(long n);
int main() {
 long n;
 printf("Enter a decimal no. :");
 scanf("%ld", &n);
 printf("DECIMAL: %ld", n);
 printf("BINARY : %ld",decToBin(n));
 return 0;
}

long decToBin(long n) {
 int rem;
 long binary = 0, i = 1;
 while(n != 0) {
  rem = n%2;
  n = n/2;
  binary= binary + (rem*i);
  i = i*10;
 }
 return binary;
}
OUTPUT :
Enter a decimal no. : 27
 DECIMAL : 27
 BINARY  : 11001

 C Program to reverse of a number using recursion.

#include<stdio.h>
#include<math.h>
int rev(int, int);
int main() {
 int num, res;
 int length = 0, temp;
 printf("Enter a number: ");
 scanf("%d", &num);
 temp = num;
 while (temp != 0) {
  length++;
  temp = temp / 10;
 }
 res = rev(num, length);
 printf("Reverse = %d\n",res);
 return 0;
}

int rev(int n, int len) {
 int x;
 if (len == 1) {
  return n;
 }
 else {
  x = ((n % 10) * pow(10, len-1));
  return ( x + rev(n / 10, --len));
 }
}
OUTPUT :
Enter a number: 1234
Reverse = 4321




CONTACT DETAILS

info@prowessapps.in
(8AM to 10PM):

+91-8527238801 , +91-9451396824

© 2017, prowessapps.in, All rights reserved