Showing posts with label number. Show all posts
Showing posts with label number. Show all posts

Wednesday, February 18, 2015

Java program to check whether a number is prime or not

Java program to check whether a number is prime or not

class prime
{
public static void main(String...s)
{
int n,i;
n=Integer.parseInt(s[0]);

for(i=2;i<n;++i)
{
if(n%i==0)
break;
}

if(i==n)
System.out.println("
Number is prime");

else
System.out.println("
Number is not prime");

}
}
Read more »

Monday, February 16, 2015

C program to reverse a number

C++ program to reverse a number

#include<iostream.h>
#include<conio.h>

void main()
{
clrscr(); //to clear the screen
long n,rev=0,d;
cout<<"Enter the number:";
cin>>n;

while(n!=0)
{
d=n%10;
rev=(rev*10)+d;
n=n/10;
}

cout<<"The reversed number is "<<rev;
getch(); //to stop the screen
}
Read more »

C Program to print table of any number


#include<iostream.h>
#include<conio.h>


void main()
{
clrscr();
int i,n;
cout<<"Enter a ny number:";
cin>>n;
cout<<"

";



for(i=1;i<=10;++i)
cout<<" "<<n<<"*"<<i<<"="<<n*i<<"
";

getch();
}
Read more »

Saturday, February 14, 2015

C program to count number of words in a string


C program to count number of words in a string

#include<stdio.h>
#include<conio.h>

void main()
{
int i,words=1;
char str[100];
clrscr();
printf("Enter a string:");
gets(str);
for(i=0;str[i]!=
Read more »

Friday, February 13, 2015

C Program to find Greatest number among three numbers

#include<iostream.h>
#include<conio.h>

void main()
{
int x,y,z,max;
clrscr();
cout<<"Enter The Three Numbers ";
cin>>x>>y>>z;

max=x;
if (y>max)
max=y;
if (z>max)
max=z;

cout<<"
"<<"The Greatest Number among "<<x<<" "<<y<<" "<<z<<" is "<<max;

getch();
}

Read more »

Thursday, February 12, 2015

C program to input number of weeks day 1 7 and translate to its equivalent name of the day of the week


#include<stdio.h>
#include<conio.h>


void main()
{
int ch;
clrscr(); //to clear the screen
printf("Enter number of weeks day(1-7):");
scanf("%d",&ch);

switch(ch)
{
case 1: printf("
Sunday");

break;
case 2: printf("
Monday");

break;
case 3: printf("
Tuesday");

break;
case 4: printf("
Wednesday");

break;
case 5: printf("
Thursday");

break;
case 6: printf("
Friday");

break;
case 7: printf("
Saturday");

break;
}
getch(); //to stop the screen
}
Read more »

Wednesday, February 11, 2015

C program to print table of a given number


#include<stdio.h>
#include<conio.h>

void main()
{
int i,n;
clrscr(); //to clear the screen

printf("Enter any number:");
scanf("%d",&n);
printf("Table of %d is:
",n);


for(i=1;i<=10;++i)
printf("
%d*%d=%d",n,i,n*i);

getch(); //to stop the screen
}
Read more »

C program to raise any number x to a positive power n

C program to raise any number x to a positive power n

#include<stdio.h>
#include<conio.h>
#include<math.h>

void main()
{
int x,n,result;
clrscr(); //to clear the scrren
printf("Enter value of x and n:");
scanf("%d%d",&x,&n);
result=pow(x,n);
printf("
Result=%d",result);

getch(); //to stop the screen
}
Read more »

Tuesday, February 10, 2015

Prime Number Checking in java

public class PrimeTest {
public static void main(String[] args) {
// TODO Auto-generated method stub

for(int number = 2; number<=20; number++){
         //print prime numbers only
boolean r=false;
         for(int i=2; i<number; i++){
             if(number%i == 0){
             r=true;
             System.out.println(number+" is not prime");
             break;
             }
             else
             {
             r=false;
             }
          }
         if(r==false){
         System.out.println(number+" is prime");
         }
     }
}
---------------------------------------------------------------------------
Output:
----------------------------------------------------------------------------

Read more »