Welcome !!

NICETECHVIDYA

Tutorials

Web Designs

You are welcome to Aptech Designers, designed by Pratik from Nepal.

Creativity is my passion. Logo describes your details about company.

Graphics Design needs vision to create yours unique digital concept.

Responsive and All Browser Compatible Dynamic Web Layouts our target.

Home Python C Language J A V A Our Courses
This WebSite is under Construction....

Friday, 11 March 2022

49.functions - Specification(7-13)

Specification7:

Accept any number and print the sum of individual digits using the function digitsum().

 

Program:

#include<stdio.h>

int digitsum(int);

void main()

{

   int n,s;

   clrscr();

   printf("Enter any integer:");

   scanf("%d",&n);

   s=digitsum(n);

   printf("Sum of all the digits %d",s);

   getch();

}

int digitsum(int n)

{

    int sum;

    for(sum=0;n!=0;n=n/10)

       sum=sum+n%10;

   return sum;

}

 

Execution:

Enter any integer: 6547

Sum of all the digits 22

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Note: A value returned from a function directly printed without storing in to another variable.

 

Specification8:

Accept any integer and print its factorial using the function factorial.

 

Program:

#include<stdio.h>

long int factorial(int n);

void main()

{

int n;

clrscr();

printf("Enter an integer:");

scanf("%d",&n);

printf("Factorial is %ld",factorial(n));

getch();       

}

long int factorial(int n)

{

   int i;

   long int fact;

   for(fact=1,i=1;i<=n;i++)

       fact=fact*i;

   return fact;

}

 

Execution:

Enter an integer: 8

Factorial is 40320

 

Example explained:

An integer is send to the function factorial() which returns its factorial that is directly printed without storing in to another variable.

 

Note: A value returned from a function directly participates in any arithmetic expression.

A function can be called for any number of times.

 

Specification9:

Accept any integer and print whether the number is a strong number or not using the function factorial()

 

Logic: Is sum of factorials of individual digits is equal to the given number then the number is called strong number.

Example: 1, 2, 145 etc.

 

Program:

#include<stdio.h>

long int factorial(int n);

void main()

{

int n,sum,temp;

clrscr();

printf("Enter an integer:");

scanf("%d",&n);

temp=n;

for(sum=0;n!=0;n=n/10)

    sum=sum+factorial(n%10);

if(temp==sum)

   printf("Strong number");

else

  printf("Not a strong number");

getch();       

}

long int factorial(int n)

{

   int i;

   long int fact;

   for(fact=1,i=1;i<=n;i++)

       fact=fact*i;

   return fact;

}

 

Execution1:

Enter an integer: 145

Strong number

 

Execution2:

Enter an integer: 456

Not a strong number

 

Note: A value returned from a function directly participates in relational and logical operations.

 

Specification10:

Accept any integer and print whether the number is a palindrome number or not using the function reverse().

 

 

 

 

 

Program:

#include<stdio.h>

int reverse(int);

void main()

{

   int n;

   clrscr();

   printf("Enter any integer:");

   scanf("%d",&n);

   if(n==reverse(n))

       printf(" Palindrome number");

   else

      printf(" Not a palindrome number");

   getch();

}             

int reverse(int n)

{

   int rev;

   for(rev=0;n!=0;n=n/10)

       rev=rev*10+n%10;

   return rev;

}

 

Execution1:

Enter any integer: 343

Palindrome number

 

Execution2:

Enter any number: 567

Not a palindrome number

 

Note: A function can return either true(1) or false(0) by using which we can take a logical decision.

 

Specification11:

Accept any number and print whether the number is a prime number or not using the function isprime()

 

Program:

#include<stdio.h>

int isprime(int);

void main()

{

    int n;

    clrscr();

    printf("Enter any integer:");

  scanf("%d",&n);

  if(isprime(n))

     printf("Prime number");

 else

     printf("Not a prime number");

getch();

}

int isprime(int n)

{

    int count,i;

    for(count=0,i=1;i<=n;i++)

       if(n%i==0)

          count++;

    if(count==2)

          return 1;

    else

          return 0;

}

 

Execution1:

Enter any integer: 18

Not a prime number

 

Execution2:

Enter any integer: 11

Prime number

 

Note: Any number of functions can be defined within an application.

 

Specification12:

Accept any integer and print the minimum and maximum digits using minimum() and maximum() functions.

 

Program:

#include<stdio.h>

int minimum(int);

int maximum(int);

void main()

{

int n;

clrscr();

printf("Enter any integer:");

scanf("%d",&n);

printf("The minimum digit %d",minimum(n));

printf("\nThe maximum digit %d",maximum(n));

getch();

}

int minimum(int n)

{

   int min;

   for(min=n%10;n!=0;n=n/10)

       if(n%10<min)

            min=n%10;

   return min;

}

int maximum(int n)

{

   int max;

   for(max=n%10;n!=0;n=n/10)

        if(n%10>max)

          max=n%10;

  return max;

}

 

Specification13:

Accept the base, exponent and print the result of expression using the function power().

 

Program:

#include<stdio.h>

int power(int,int);

void main()

{

int base,exp,res;

clrscr();

printf("Base:");

scanf("%d",&base);

printf("Exponent:");

scanf("%d",&exp);

res=power(base,exp);

printf("The result of expression %d",res);

getch();

}

int power(int b,int e)

{

   int r,i;

   for(r=1,i=1;i<=e;i++)

      r=r*b;

   return r;

}

 

Execution:

Base: 3

Exponent: 4

The result of expression: 81

pow(): It is the predefined function used for the same purpose.

Specification14:

Print the Armstrong numbers from 1 to the given limit.

Program:

#include<math.h>

#include<stdio.h>

int isarmstrong(int);

void main()

{

    int n,limit;

    clrscr();

    printf("Enter the limit:");

    scanf("%d",&limit);

    for(n=1;n<=limit;n++)

        if(isarmstrong(n))

           printf("\n%d",n);

    getch();

}

int isarmstrong(int n)

{

    int sum,temp=n;

    for(sum=0;n!=0;n=n/10)

            sum=sum+pow(n%10,3);

     return (temp==sum);                   /* it returns either 1 or 0 */

}

 

Execution:

Enter the limit: 1000

1

153

370

371

407


0 comments:

Post a Comment