Friday, 2 October 2015

C program to print natural numbers from 1 to 10 in Reverse

#include <stdio.h>

#include <conio.h>

void main()

{

int i;

clrscr();

for(i=10;i>=1;i--)

{

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

getch();

}

OUTPUT:

10
9
8
7
6
5
4
3
2
1

Wednesday, 15 July 2015

C program to check leap year

#include <stdio.h>
 
int main()
{
  int year;
 
  printf("Enter a year to check if it is a leap year\n");
  scanf("%d", &year);
 
  if ( year%400 == 0)
    printf("%d is a leap year.\n", year);
  else if ( year%100 == 0)
    printf("%d is not a leap year.\n", year);
  else if ( year%4 == 0 )
    printf("%d is a leap year.\n", year);
  else
    printf("%d is not a leap year.\n", year);  
 
  return 0;
}
 
OUTPUT:
Enter a year to check if it is a leap year
2012
2012 is a leap year
 
 
 

Tuesday, 7 July 2015

C program to find the Armstrong number

#include <stdio.h>
 
int power(int, int);
 
int main()
{
   int n, sum = 0, temp, remainder, digits = 0;
 
   printf("Input an integer\n");
   scanf("%d", &n);
 
   temp = n;
   // Count number of digits
   while (temp != 0) {
      digits++;
      temp = temp/10;
   }
 
   temp = n;
 
   while (temp != 0) {
      remainder = temp%10;
      sum = sum + power(remainder, digits);
      temp = temp/10;
   }
 
   if (n == sum)
      printf("%d is an Armstrong number.\n", n);
   else
      printf("%d is not an Armstrong number.\n", n);
 
   return 0;
}
 
int power(int n, int r) {
   int c, p = 1;
 
   for (c = 1; c <= r; c++) 
      p = p*n;
 
   return p;   
}
 
OUTPUT: 

Input an Integer
9926315
9926315 is an Integer

Thursday, 18 June 2015

C program to check whether input alphabet is a vowel or not

#include <stdio.h>
 
int main()
{
  char ch;
 
  printf("\nEnter a character:");
  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;
}
 
Output:
 
 Enter a character : e
 e is a vowel 
 
 Enter a character : w
 w is not a vowel 
 
 

Wednesday, 17 June 2015

C++ Program to Reverse a Number

#include<iostream>
using namespace std;

int main() {
int number, reverse = 0;
cout<<"Input a Number to Reverse and press Enter: ";
 cin>> number;     // Taking Input Number in variable number

   for( ; number!= 0 ; )
   {
      reverse = reverse * 10;
      reverse = reverse + number%10;
      number = number/10;
   }
   cout<<"New Reversed Number is:  "<<reverse;
   return 0;

}


Output: 

  Input a Number to Reverse and press Enter:12345
  New Reversed Number is:54321

Friday, 12 June 2015

C++ Program to enter two integers and find their Sum and Average

#include <iostream.h> 
#include <conio.h>
void main() 
clrscr(); 
int x,y,sum; 
float average; 
cout << "Enter 2 integers : " << endl; 
cin>>x>>y; 
sum=x+y; 
average=sum/2; 
cout << "The sum of " << x << " and " << y << " is " << sum 
     << "." << endl; 
cout << "The average of " << x <<  " and " << y << " is " << average 
     << "." << endl; 
getch(); 
}
 
Output:
 
The sum of 8 and 6 is 14
The average of 8 and 6 is 7

Tuesday, 9 June 2015

C program to Find the Prime Number


#include<stdio.h>
 
int 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;
}
 
Output:
 
 Enter the number of prime numbers require
  5
 First 10 Prime Numbers are:
  2
  3
  5
  7 
  11 
 

Tuesday, 5 May 2015

C program to verify Arithmetic Operator & Operation

#include<stdio.h>
#include<conio.h>
int main()
{
int num1,num2;
int sum,sub,mult,div,mod;
printf("\nEnter the First Number:");
scanf("%d",&num1);
printf("\nEnter the Second Number:");
scanf("%d",&num2);

sum=num1 + num2;
printf("\nAddition is:%d",sum);

sub=num1 - num2;
printf("\nSubtraction is:%d",sub);

mult=num1 * num2;
printf("\nMultiplication is:%d",mult);

div=num1 / num2;
printf("\nDivision is:%d",div);

mod=num1 % num2;
printf("\nModulus is:%d",mod);

return(0);
getch();
}

Output:
   Enter the First Number: 10
   Enter the Second Number: 5
   Addition is: 15
   Subtraction is: 5
   Multiplication is: 50
   Division is: 2
   Modulus is: 0

Tuesday, 28 April 2015

C Program to Find Area and Circumference of Circle

#include<stdio.h>
#include<conio.h>
int main()
{
int rad;
float PI= 3.14, area, ci;
printf("\nEnter the radius of Circle :");
scanf("%d",&rad);
area= PI * rad * rad;
printf("\nArea of Circle : %f ",area);
ci= 2 * PI *  rad;
printf("\nCircumference : %f ", ci);
return(0);
getch();
}

Output:
Enter the radius of Circle: 1
Area of Circle: 3.14
Circumference: 6.28