MY mENU


Sunday 20 May 2012

program to generate and print armstrong numbers

program to generate and print armstrong numbers
armstrong number in c: This program prints armstrong number. In our program we ask the user to enter a number and then we use a loop from one to the entered number and check if it is an armstrong number and if it is then the number is printed on the screen. Remember a number is armstrong if the sum of cubes of individual digits of a number is equal to the number itself. For example 371 is an armstrong number as 33 + 73 + 13 = 371. Some other armstrong numbers are 0, 1, 153, 370, 407.

C code
#include 
#include 
 main() {
 int r;
 long number = 0, c, sum = 0, temp;
 printf("Enter the maximum range upto which you want to find armstrong numbers ");
 scanf("%ld",&number);
 printf("Following armstrong numbers are found from 1 to %ld\n",number);
 for( c = 1 ; c <= number ; c++ ) { 
 temp = c;
 while( temp != 0 ) {
 r = temp%10;
 sum = sum + r*r*r;
 temp = temp/10; 
 }
 if ( c == sum )
 printf("%ld\n", c);
 sum = 0;
 }
 getch();
 return 0; 
}
Output of program

armstrong number

armstrong number c program
  c programming code to check whether a number is armstrong or not. A number is armstrong if the sum of cubes of individual digits of a number is equal to the number itself. For example 371 is an armstrong number as 33 + 73 + 13 = 371. Some other armstrong numbers are: 0, 1, 153, 370, 407.

C programming code
#include  
 main() {
 int number, sum = 0, temp, remainder;
 printf("Enter a number\n");
 scanf("%d",&number);
 temp = number;
 while( temp != 0 ) { 
 remainder = temp%10;
 sum = sum + remainder*remainder*remainder; 
 temp = temp/10;
 }
 if ( number == sum )
 printf("Entered number is an armstrong number."); 
    else 
     printf("Entered number is not an armstrong number."); 
 return 0; 
}

Output of program:

Prime number program

Prime number program in c language

#include 
   main() {
  int n, i = 3, count, c;
  printf("Enter the number of prime numbers required\n");
 scanf("%d",&n);  
if ( n >= 1 ) {
  printf("First %d prime numbers are :\n",n);
 printf("2\n");
 }
 for ( count = 2 ; count <= n ; ) {
 for ( c = 2 ; c <= i - 1 ; c++ ) {
 if ( i%c == 0 )
 break;
 }
 if ( c == i ) { 
 printf("%d\n",i);
 count++; 
 }
 i++;
 }
 return 0; 
}

There are many logic to check prime numbers, one given below is more efficient then above method.
for ( c = 2 ; c <= (int)sqrt(n) ; c++ )
//only checking from 2 to square root of number is sufficient.


C program for prime number or not
#include
 main() { 
 int n, c = 2; 
 printf("Enter a number to check if it is prime\n"); 
 scanf("%d",&n);
 for ( c = 2 ; c <= n - 1 ; c++ ) {
 if ( n%c == 0 ) {
 printf("%d is not prime.\n", n);
 break;
 }
 }
 if ( c == n ) 
 printf("%d is prime.\n", n); 
 return 0; 
}
C program for prime number using function
#include
 int check_prime(int);
 main() {
 int n, result;
 printf("Enter an integer to check whether it is prime or not.\n");
 scanf("%d",&n); 
 result = check_prime(n);
 if ( result == 1 )
 printf("%d is prime.\n", n);
 else
 printf("%d is not prime.\n", n);
 return 0;
 }
 int check_prime(int a) {
 int c;
 for ( c = 2 ; c <= a - 1 ; c++ ) {
 if ( a%c == 0 )
 return 0;
 }
 if ( c == a )
 return 1;
 }

to print diamond pattern

c program to print diamond pattern

Diamond pattern in c: This code print diamond pattern of stars. Diamond shape is as follows:

     * 
   *** 
 ***** 
   ***
     * 

#include  
 int main() {
 int n, c, k, space = 1;
 printf("Enter number of rows\n");
 scanf("%d", &n);
 space = n - 1; 
 for (k = 1; k <= n; k++) { 
   for (c = 1; c <= space; c++) 
      printf(" "); 
       space--; 
 for (c = 1; c <= 2*k-1; c++)
   printf("*");
    printf("\n"); 
     }
 space = 1; 
      for (k = 1; k <= n - 1; k++) { 
       for (c = 1; c <= space; c++)
         printf(" ");
           space++;
               for (c = 1 ; c <= 2*(n-k)-1; c++)
                printf("*");
                 printf("\n");
                } 
                 return 0;
  }

To print Pattern

#include 
 main() {
 int row, c, n, temp;
 printf("Enter the number of rows in pyramid of stars you wish to see ");
 scanf("%d",&n); 
 temp = n;
 for ( row = 1 ; row <= n ; row++ ) {
 for ( c = 1 ; c < temp ; c++ ) 
     printf(" ");
           temp--;
 for ( c = 1 ; c <= 2*row - 1 ; c++ )
 printf("*");
 printf("\n"); 
 } 
 return 0;
 }

Output:



Consider the pattern
*
**
***
****
*****

to print above pattern see the code below:

#include
 main() { 
 int n, c, k;
 printf("Enter number of rows\n");
 scanf("%d",&n); 
 for ( c = 1 ; c <= n ; c++ ) { 
 for( k = 1 ; k <= c ; k++ ) 
 printf("*");
 printf("\n");
 }
 return 0;
 }

Palindrome number

#include

 main() {
 int n, reverse = 0, temp; 
 printf("Enter a number to check if it is a palindrome or not\n");
 scanf("%d",&n);
 temp = n; 
 while( temp != 0 ) {
 reverse = reverse * 10;
 reverse = reverse + temp%10; 
 temp = temp/10;
 }
 if ( n == reverse ) 
 printf("%d is a palindrome number.\n", n); 
    else
       printf("%d is not a palindrome number.\n", n); 
 return 0;
 }

Reverse a Number

#include
 main() {
 int n, reverse = 0;
 printf("Enter a number to reverse\n");
 scanf("%d",&n);
 while (n != 0) {
 reverse = reverse * 10;
 reverse = reverse + n%10;
 n = n/10;
 }
 printf("Reverse of entered number is = %d\n", reverse);
 return 0;
 }

Output of program:

Swapping of two number

Swapping of two numbers in c

#include

 int main() {
 int x, y, temp;
 printf("Enter the value of x and y\n");
 scanf("%d%d", &x, &y);
 printf("Before Swapping\nx = %d\ny = %d\n",x,y);
 temp = x; x = y; y = temp;
 printf("After Swapping\nx = %d\ny = %d\n",x,y);
 return 0;
 }

Swapping of two numbers without third variable: You can also swap two numbers without using temp or temporary or third variable. In that case c program will be as shown :-

#include  
 int main() { 
 int a, b; 
 printf("Enter two integers to swap\n");
 scanf("%d%d", &a, &b);
 a = a + b; 
 b = a - b;
 a = a - b;
 printf("a = %d\nb = %d\n",a,b); 
 return 0; 
}

Swap two numbers using pointers

#include  
 int main() { 
 int x, y, *a, *b, temp;
 printf("Enter the value of x and y\n"); 
 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;
 }

Swapping numbers using call by reference
#include  
 void swap(int*, int*);
 int main() { 
 int x, y;
 printf("Enter the value of x and y\n"); 
 scanf("%d%d",&x,&y);
 printf("Before Swapping\nx = %d\ny = %d\n", x, y);
 swap(&x, &y); 
 printf("After Swapping\nx = %d\ny = %d\n", x, y);
 return 0; 
 void swap(int *a, int *b) { 
 int temp; 
 temp = *b; 
 *b = *a;
 *a = temp; 
 }

C programming code to swap using bitwise XOR
#include  
 int main() { 
 int x, y;
 scanf("%d%d", &x, &y);
 printf("x = %d\ny = %d\n", x, y);
 x = x ^ y; 
 y = x ^ y;
 x = x ^ y;
 printf("x = %d\ny = %d\n", x, y);
 return 0; 
}

Output of code:

to Add n Numbers

#include  
 int main() { 
 int n, sum = 0, c, value;
 printf("Enter the number of integers you want to add\n");
 scanf("%d", &n); 

 printf("Enter %d integers\n",n);
     for (c = 1; c <= n; c++)
     { 
       scanf("%d",&value); 
         sum = sum + value; 
      }
 printf("Sum of entered integers = %d\n",sum); 
 return 0;
 }

Output of program:
C programming code using array
#include  
 int main() { 
 int n, sum = 0, c, array[100];
 scanf("%d", &n); 

 for (c = 0; c < n; c++) {
 scanf("%d", &array[c]);
 sum = sum + array[c];
 }
 printf("Sum = %d\n",sum); 
 return 0;
 }

add digits of number

#include
 main() {
 int n, sum = 0, remainder;
 printf("Enter an integer\n");
 scanf("%d",&n); 
 while(n != 0) {
 remainder = n % 10;
 sum = sum + remainder;
 n = n / 10;
 }
 printf("Sum of digits of entered number = %d\n",sum); 
 return 0; 
}
Add digits using recursion
#include  
 int add_digits(int);
 int main() { 
 int n, result;
 scanf("%d", &n); 
 result = add_digits(n);
 printf("%d\n", result);
 return 0; 
}
 int add_digits(int n) { 
 static int sum = 0;
 if (n == 0) 
{
 return 0;
 } 
 sum = n%10 + add_digits(n/10);
 return sum;
 }

Factorial

Factorial program in c using for loop


#include
#include
main()
{
int c, n, fact = 1;
printf("Enter a number to calculate it's factorial\n");
scanf("%d",&n);

for( c = 1 ; c <= n ; c++ )
fact = fact*c;

printf("Factorial of %d = %d\n",n,fact);
getch();
return 0;
}


Factorial program in c using function

#include
long factorial(int);
main()
{
int number;
long fact = 1;

printf("Enter a number to calculate it's factorial\n");
scanf("%d",&number);
printf("%d! = %ld\n", number, factorial(number));
return 0;
}

long factorial(int n)
{
int c;
long result = 1;
for( c = 1 ; c <= n ; c++ )

result = result*c;

return ( result );

}

Factorial program in c using recursion

#include

long factorial(int);
main()
{
int num;
long f;
printf("ENTER A NUMBER TO FIND FACTORIAL :");
scanf("%d",&num);

if(num<0)
printf("NEGATIVE NUMBERS ARE NOT ALLOWED");
else
{
f = factorial(num);
printf("%d!=%ld",num,f);
}
return(0);
}

long factorial(int n)

{
if(n==0)
return(1);
else
return(n*factorial(n-1));
}

Vowel or Not


#include

main()
{

char ch;
printf("Enter a character\n");
scanf("%c", &ch);

if (ch == 'a' || ch == 'A'  || ch == 'e'  || ch == 'E'  || ch == 'i' || ch == 'I' || ch =='o' || ch=='O'  || ch == 'u'  ||  ch == 'U')

printf("%c is a vowel.\n", ch);

else

printf("%c is not a vowel.\n", ch);
return 0;
}

Check vowel using switch:

#include
main()
{
char ch;
printf("Enter a character\n");
scanf("%c", &ch);

switch(ch)
{
case 'a':
case 'A':
case 'e':
case 'E':
case 'i':
case 'I':
case 'o':
case 'O':
case 'u':
case 'U':

printf("%c is a vowel.\n", ch);
break;

default: printf("%c is not a vowel.\n", ch);

}
return 0;
}

Function to check vowel

int check_vowel(char a)
{

 if (a >= 'A' && a <= 'Z')
a = a + 'a' - 'A'; /* Converting to lower case or use a = a + 32 */

if (a == 'a' || a == 'e' || a == 'i' || a == 'o' || a == 'u')

return 1;
return 0;
}

program to check whether input alphabet is a vowel or not

check out odd or even


C program to check odd or even using modulus operator:

#include

main()
{

int n;
printf("Enter an integer\n");
scanf("%d",&n);

if ( n%2 == 0 )

printf("Even\n");
else
printf("Odd\n");

return 0;
}

C program to check odd or even using bitwise operator

#include

main()

{

int n;

printf("Enter an integer\n");

scanf("%d",&n);

 
if ( n & 1 == 1 )

 
printf("Odd\n");
else
 printf("Even\n");
return 0;
}


C program to check odd or even without using bitwise or modulus operator


#include

main()

{

int n;



printf("Enter an integer\n");

scanf("%d",&n);

if ( (n/2)*2 == n )

 
printf("Even\n");

else

printf("Odd\n");

 
return 0;

}


Find odd or even using conditional operator


#include

main()

{

int n;


printf("Enter an integer\n");

scanf("%d",&n);



n%2 == 0 ? printf("Even number\n") : printf("Odd number\n");
return 0;
}