Showing posts with label Codes. Show all posts
Showing posts with label Codes. Show all posts

Monday, 11 August 2014

C language program to find out the factorial of an integer by calling a function.

#include<stdio.h>
void main()
{
          long int n;
          long int fact(long int n);
          clrscr();
          printf(" Enter a number: ");
          scanf("%ld",&n);
          printf(" Factorial of %ld is %ld",n,fact(n));
          getch();
}
long int fact(long int i)
{
          int fct=1;
          for(;i>=1;i--)
          fct*=i;
          return fct;

}

Wednesday, 6 August 2014

C language program to find out the factorial of an integer by using "while" loop.

#include<stdio.h>
main()
{
          double n,ans=1;
          clrscr();
          printf(" Enter a number\n to find factorial of it : ");
          scanf("%lf",&n);
          while (n>1.)
          {
          ans*=n;
          --n;
          }
          printf(" answer = %.2lf",ans);
          getch();

}

C language program to find Prime Number.

#include<stdio.h>
#include<math.h>
void main()
{
          int num,i,is_prime=0;
          clrscr();
          printf(" Give a number : ");
          scanf("%d",&num);
          for (i=2;i<=sqrt(num);i++)
          {
          if (num%i==0)
          {
          is_prime=1;
          break;
          }
          }
          if (is_prime==1)
          printf(" Not prime number");
          else
          printf(" Prime number");
          getch();
}

Monday, 4 August 2014

A C language program to print Triangle of asterisks (*).


#include<stdio.h>
void draw_j(void);
void main()
{
          clrscr();
          draw_j();
          getch();
}
void draw_j(void)
{
          int a,s;
          for(a=5;a>=1;a--)
          {
          s=1;
          while (s<=a)
          {
          printf("*");
          s++;
          }
          printf("\n");
          }
}

OUTPUT:
********
*******
******
*****
****
***
**

*

Friday, 25 July 2014

A C language program that promotes the user to enter a number and then reverse it.


For example, if the user enters 5143, the function would reverse it so that it becomes 3415.

#include<stdio.h>
void main ()
{
          int num=0;
          clrscr();
          printf(" Enter a number to reverse : ");
          scanf("%d",&num);
          printf(" Reverse of %d is %d\n",num,reverse(num));
          getch();
}
int reverse(int num)
{
          int res=0,temp=num,k,a=0,i;
          /* finds the number or digits*/
          for(;temp/10!=0;temp/=10)
          a++;
          for(;num/10!=0;)
          {
          k=num%10;
          for(i=0;i<a;i++)
          {
          k*=10;
          }
          num/=10;
          a--;
          res+=k;
          if (num<10)
          res+=num;
          }
          return res;

}

Examples of loop with output in java code

class Loops
{
                public static void main(String args[])
                {
                                int i;
                                for(i=0; i<10; i += 2)
                                System.out.print(" " + i);
                                System.out.println();

                                for(i=100; i>=0; i -= 7)
                                System.out.print(" " + i);
                                System.out.println();

                                for(i=1; i<=10; i += 1)
                                System.out.print("*" + ++i);
                                System.out.println();

                                for(i=2; i<100; i *= 2)
                                System.out.print(" " + i);
                }
}
Output:
0 2 4 6 8
100 93 86 79 72 65 58 51 44 37 30 23 16 9 2
*2*4*6*8*10

2 4 8 16 32 64

Thursday, 24 July 2014

Calculate square of odd, cube of even , quotient for multiple of 7 with java code.

import javax.swing.*;
class Cal
{
                public static void main(String args[])
                {
                                int n;
                                n = Integer.parseInt(JOptionPane.showInputDialog("enter a value: "));
                                if (n%2==0)
                                {
                                System.out.println("cube of even value = " + n*n*n);
                                JOptionPane.showMessageDialog(null, "cube of even value = " + (n*n*n));
                                }
                                else if (n%2!=0)
                                {
                                System.out.println("square of odd value = " + n*n);
                                JOptionPane.showMessageDialog(null, "square of odd value = " + (n*n));
                                }
                                if (n%7 == 0)
                                {
                                System.out.println("quotient for 7 is " + n/7);
                                JOptionPane.showMessageDialog(null,"quotient for 7 is " + (n/7));
                                }
                                System.exit(0);
                }

}

Wednesday, 23 July 2014

Show first 10 Fibonacci numbers in message dialog box with java code.

import javax.swing.*;
class Fibo
{
                public static void main(String[] args){
                int n1 = 0,n2 = 1,n3;
                String st;
                st = n1 + "\n" + n2 + "\n";
                for(int i = 1; i<10; i++){
                                n3 = n1 + n2;
                                st += ( n3 + "\n");
                                n1 = n2;
                                n2 = n3;
                }
                JOptionPane.showMessageDialog(null, st);
                System.exit(0);
                }                             

}