Showing posts with label Number. Show all posts
Showing posts with label Number. Show all posts

Find out the perfect number using c program

Write a C Program to Find out the perfect number using c program?


#include<stdio.h>
int main(){
  int n,i=1,sum=0;
  printf("\nEnter a number:-");
  scanf("%d",&n);
  while(i<n){
      if(n%i==0)
           sum=sum+i;
          i++;
  }
  if(sum==n)
      printf("\nThe no %d is a perfect number",i);
  else
      printf("\nThe no %d is not a perfect number",i);
  return 0;
}

Write a c program to find out H.C.F. of two numbers.

How To Write a c program to find out H.C.F. of two numbers in C Program.


#include<stdio.h>
int main(){
int x,y,m,i;
printf("Insert any two number: ");
scanf("%d%d",&x,&y);
m=x
for(i=m;i>=1;i--){
if(x%i==0&&y%i==0){
printf("\nHCF of two number is : %d",i) ;
break;
}
}
return 0;
}

Check the given number is armstrong number or not using c program.

How To Check the given number is armstrong number or not using c program.

#include<stdio.h>
int main(){
int num,r,sum=0,temp;
printf("\nEnter a number:-");
scanf("%d",&num);
temp=num;
while(num!=0){
r=num%10;
num=num/10;
sum=sum+(r*r*r);
}
if(sum==temp)
printf("\nThe number %d is an armstrong number",temp);
else
printf("\nThe number %d is not an armstrong number",temp);
return 0;
}

FIND OUT GENERIC ROOT OF A NUMBER - C PROGRAM.

How to FIND OUT GENERIC ROOT OF A NUMBER BY C PROGRAM.


#include<stdio.h>
int main(){
long int num,sum,r;
printf("\nEnter a number:-");
scanf("%ld",&num);
while(num>10){
sum=0;
while(num){
r=num%10;
num=num/10;
sum+=r;
}
if(sum>10)
num=sum;
else
break;
}
printf("\nSum of the digits in single digit is: %ld",sum);
return 0;
}

FIND PRIME FACTORS OF A NUMBER USING C PROGRAM

How To FIND PRIME FACTORS OF A NUMBER USING C PROGRAM.


#include<stdio.h>
int main(){
  int num,i=1,j,k;
  printf("\nEnter a number:");
  scanf("%d",&num);
  while(i<=num){
      k=0;
      if(num%i==0){
         j=1;
          while(j<=i){
            if(i%j==0)
                 k++;
             j++;
          }
          if(k==2)
             printf("\n%d is a prime factor",i);
      }
      i++;
   }
   return 0;
}

How To Write a C program that generates two random numbers ?

Write a C program that generates two random numbers.


Print both numbers that are generated.

Then use a ternary (or conditional) operator to identify the largest of the 2 numbers and print the result.

Seed the random number generator so the numbers generated are not the same every time.

Submit the .c file only.

Sample Run:

Random 1:8809
Random 2:27141
The max of 8809 and 27141 is 27141.


  1. #include <stdio.h>
  2. #include <stdlib.h> // void srand(unsigned int); - int rand(); - NULL - RAND_MAX,
  3. #include <time.h> // time_t time(time_t*) - NULL
  4.  
  5. int main()
  6. {
  7.     srand((unsigned int)time(NULL)); // seeds srand with the number of seconds since January 1st 1970
  8.     int rand1 = rand(); //init the vars to random
  9.     int rand2 = rand(); //init the vars to random
  10.     int max = rand1 > rand2 ? rand1 : rand2; //Ternary (three) conditional operator which sets max = rand1 if true and max = rand2 if false
  11.     printf("Random 1: %d \n", rand1);
  12.     printf("Random 1: %d \n", rand2);
  13.     printf("The max of %d and %d is %d \n", rand1, rand2, max);
  14.     return 0;
  15. }

Write a C program to find maximum or equal between two numbers ?

How to Write a C program to find maximum or equal between two numbers ?


  1. /*Write a C program to find maximum or equal between two numbers */
  2.  
  3.  
  4. #include <stdio.h>
  5.  
  6. int main ()
  7.  
  8. {
  9.     int a,b;
  10.     printf("Find the maximum between two number: ");
  11.     scanf("%d %d", &a, &b);
  12.  
  13.     if ( a > b ){
  14.         printf("Minimum Number: %d\n",a);
  15.     }
  16.  
  17.     if ( b > a ){
  18.         printf("Maximum Number: %d\n",b);
  19.     }
  20.  
  21.     if ( a == b ){
  22.         printf("Number is equal");
  23.     }
  24.  
  25.     return 0;
  26. }

How to Write a C program to find maximum between two numbers ?

Write a C program to find maximum between two numbers.



  1. /*Write a C program to find maximum between two numbers */
  2. #include <stdio.h>
  3. int main ()
  4. {
  5.     int a,b;
  6.     printf("Find the maximum between two number: ");
  7.     scanf("%d %d", &a, &b);
  8.     if ( a < b ){
  9.         printf("Minimum Number:%d\n",a);
  10.     }
  11.     if ( a > b ){
  12.         printf("Maximum Number%d\n",b);
  13.     }
  14.     return 0;
  15. }

Write a C program to perform math operations on two input whole numbers. The operations are:

Write a C program to perform math operations on two input whole numbers. The operations are:


a) Compute square root of n1
b) Compute square root of n2
c) Raise n1 to the power of n2
d) Inverse n1
e) Inverse n2


  1. //Write a C program to perform math operations on two input whole numbers. The operations are:
  2. //a) Compute square root of n1
  3. //b) Compute square root of n2
  4. //c) Raise n1 to the power of n2
  5. //d) Inverse n1
  6. //e) Inverse n2
  7. #include<iostream>
  8. #include<math.h>
  9. using namespace std;
  10. int main()
  11. {
  12.     int n1,n2;
  13.     cin>>n1>>n2;
  14.     cout<<sqrt(n1)<<endl;
  15.     cout<<sqrt(n2)<<endl;
  16.     int aux=n1;
  17.     for(int i=1;i<n2;i++)
  18.         aux=aux*n1;
  19.     cout<<aux<<endl;
  20.     aux=0;
  21.     while(n1>0)
  22.     {
  23.         aux=aux*10+n1%10;
  24.         n1=n1/10;
  25.     }
  26.     cout<<aux<<endl;
  27.     aux=0;
  28.     while(n2>0)
  29.     {
  30.         aux=aux*10+n2%10;
  31.         n2=n2/10;
  32.     }
  33.     cout<<aux<<endl;
  34. }