6. Write a function that receives a positive integer as input and returns the leading digit in its decimal representation
// For example : the leading digit of 4567 is 4 #include<stdio.h> #include<conio.h> // Linux user - Remove this int leading_digit(int n) { // Declaring variable d=digit to store the digits extracted from number int d; // Performing digit extraction while(n) { d=n%10; n=n/10; } // Returning leading digit return (d); } int main() { // Declaring variable "n" to input a number int n; // Inputting number printf("Enter any positve integer number : "); scanf("%d",&n); // Calling function and printing result printf("Leading digit of number : %d = %d ",n, leading_digit(n));
getch(); // Linux user - Remove this return 0; }
7. Implement a function that receives an integer value and returns the number of prime factors of that number. If the number is itself a prime number then return 0
/* For example : for number 21, the function should return 2 as the prime factors of 21 is 3, 7 for number 3, 71 the function should return 0 as 3, 71 are prime numbers */ #include<stdio.h> #include<conio.h> // Linux user - Remove this int prime_factors(int n) { // Decaring variable "i", "j" to iterate loop, c=counter int i, j, c; // Declaring variable cp=count prime to count the number of prime numbers int cp = 0; // Determining number of prime factors for(i=2; i<=n/2; i++) { c=0; if(n%i==0) { for(j=1; j<=i/2; j++) { if(i%j==0) { c++; } } if(c<=1) { cp++; } } } // Returning number of prime factors return (cp); } int main() { // Declaring variable "n" to input a number int n; // Inputting number printf("Enter any positve integer number : "); scanf("%d",&n); // Calling funtion and printing result printf("Number of prime factors of %d = %d ",n, prime_factors(n)); getch(); // Linux user - Remove this return 0; }
8. Program to find maximum number between three numbers
#include<stdio.h> #include<conio.h> // Linux user - Remove this // Function prototype int maximum(int x, int y, int z); int main() { // Declaring variable n1, n2, n3 to take the input of three numbers int n1, n2, n3; // Inputting value printf("Enter first number : "); scanf("%d",&n1); printf("Enter second number : "); scanf("%d",&n2); printf("Enter third number : "); scanf("%d",&n3); // Calling function maximum() and printing the returned maximum value printf("Maximum between three numbers : %d ", maximum(n1, n2, n3)); getch(); // Linux user - Remove this return 0; } int maximum(int x, int y, int z) { // Decalring variable max to store max value int max = x; // Assuming x to be maximum if(max<y) { max=y; } if(max<z) max=z; // Returning maximum value return (max); }
9. Program to accept a number and print the sum of given and Reverse number using function
# include <stdio.h> # include <conio.h> // Linux user - Remove this int main( ) { // Declaring variable n=to store the entered number by user int n; /* Declaring variable r=reverse to store the reverse of a number, s=sum to store the sum of given and reverse number */ int r, s; printf("Enter a number : "); scanf("%d",&n); // Calling funciton rev and storing the result in "r" r=rev(n); // Displaying reverse number printf("Reverse of a number = %d",r); // Calling function add s=add(n,r); // Displaying result or sum printf("\nSum of a given and reverse number = %d",s); getch( ); // Linux user - Remove this return 0; } int rev( int n) { /* Declaring variable d=digit to store the extracted digit, rev=reverse to store reversed number */ int d,rev=0; while(n>0) { d=n%10; rev=rev*10+d; n=n/10; } // Returning reversed number return rev; } int add(int n, int r) { // Returning sum of given and reverse number return n+r; }