AD1

Tuesday, 15 November 2016

Write a recursive function that finds the binary equivalent of an integer 

solution 

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <math.h>
#include <stdbool.h>
void binary(int decimal)
{
if(decimal==0){printf("%d",0);}
if(decimal>0)
{
if(decimal%2==0)
{
printf("%d\n",0);
binary(decimal/=2);
}
else
{
printf("%d\n",1);
binary(decimal/2);
}
}
return;
}
int main()
{
int n;
printf("enter decimal value\n");
scanf("%d",&n);
printf("binary equivalent of %d = \n",n);
binary(n);
return 0;
}
view raw binary.c hosted with ❤ by GitHub

write a recursive function to calculate the sum of the first 25 natural numbers

solution 

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <math.h>
#include <stdbool.h>
int sum25()
{
static int sum=0;
static int t=25;
if(t>=0)
{
sum+=t;
t--;
sum25();
}
return sum;
}
int main()
{
printf("%d",sum25());
return 0;
}
view raw sum25.c hosted with ❤ by GitHub

Write a recursive function to print the first 25 elements of fibonacci series 

fibonacci series is characterized by the fact that every number after the first two is the sum of the two preceding ones
0,\;1,\;1,\;2,\;3,\;5,\;8,\;13,\;21,\;34,\;55,\;89,\;144,\;\ldots \;

solution

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <math.h>
#include <stdbool.h>
void fibonacci25(int start,int current)
{
int next;
static int t=23;
if(t--)
{
next=start+current;
start=current;
current=next;
printf("%d\n",next);
fibonacci25(start,current);
}
return;
}
int main()
{
int start=0; int current=1;
printf("%d\n%d\n",start,current);//print first two terms 0,1
fibonacci25(start,current);//function that print first 25 number of fibonacci series
return 0;
}
view raw fiborecursive.c hosted with ❤ by GitHub

Monday, 14 November 2016

Write a recursive function to calculate the sum of digits of an integer 

write a recursive function that accepts an integer and finds sum of its digits , as example sum(2370) will print 12

solution

#include <stdio.h>
#include <math.h>
int sumofdigits(int n)
{
int sum=0;
if(n>0)
{
sum=(n%10)+sumofdigits(n/10);
}
return(sum);
}
int main()
{
int number;
printf("Enter positive integer\n");
scanf("%d",&number);
if(number>0)
{
printf("sum of digits of %d = %d",number,sumofdigits(number));
}
else
{
printf("Error! you must enter a positive integer");
}
return 0;
}
view raw sumofdigits.c hosted with ❤ by GitHub

Saturday, 12 November 2016

sum,average and standard deviation



Write a function that receives 5 integers and returns the sum, average and standard deviation of these numbers. Call this function from main( ) and print the results in main( ).  

solution 

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <math.h>
#include <stdbool.h>
void sum_avg_dev(int data[5],int *sump,double *avgp,double *devp)// accept numbers as array elements and three pointers treated as function output parameters
{
for(int i=0;i<5;i++)
{
*sump+=data[i];//sum
}
*avgp=*sump/5.0;//average
int sumsqr=0;
for(int i=0;i<5;i++)
{
sumsqr+=data[i]*data[i];
}
*devp=sumsqr/5.0;//std deviation
return ;
}
int main()
{
int sum=0; double avge=0.0; double dev=0.0;int array[5];
printf("enter five integers\n");
for(int i=0;i<=4;i++)
{
scanf("%d",&array[i]);
}
sum_avg_dev(array,&sum,&avge,&dev);
printf("sum= %d\naverage= %lf\nstandard deviation= %lf\n",sum,avge,dev);
return 0;
}

Prime factors

A positive integer is entered through the keyboard. Write a function to obtain the prime factors of this number.For example, prime factors of 24 are 2, 2, 2 and 3, whereas prime factors of 35 are 5 and 7.

solution 

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <math.h>
#include <stdbool.h>
bool is_prime(int n) // afunction to check if the number is prime or not
{
for(int i=2;i<n;i++)
{
if(n%i==0)
{
return false;
}
else
{
continue;
}
}
return true;
}
void prime_factors(unsigned int x) // accept integer x to find its prime factors
{
while(x>0)
{
int n=2;
again:while(x%n==0)
{
x/=n;
printf("%d ",n);
}
back: if(is_prime(++n))
{
goto again;
}
else {goto back;}
}
printf("error! enter positive integer\n");
return;
}
int main()
{
int n;
printf("enter any integer \n");
scanf("%d",&n);
printf("prime factors of %d are \n",n);
prime_factors(n);
return 0;
}
view raw primefactors.c hosted with ❤ by GitHub