Search This Blog

Tuesday, 16 July 2019

5. C Program to find the factors of nth Fibonacci number.

//5. C Program to find the factors of nth Fibonacci number.
#include<stdio.h>
void main()
{
    int num,f1=0,f2=1,f3=0,i;
    clrscr();
    printf("Enter the value for N:");
    scanf("%d",&num);

    for(i=1;i<num;i++)
    {
        f1=f2;
        f2=f3;
        printf("%d\n",f3); //This statement is for debugging.
        f3=f1+f2;

    }
    printf("\nNth Fibonacci Number = %d  \n",f3);
    printf("Its factors are:\n");
    for(i=1;i<=f3;i++)
    {
        if(f3%i==0)
            printf("%d\n",i);
    }

    getch();
}

4. Program to find nth prime and check whether it is palindrome or not.

//4. Program to find nth prime and check whether it is palindrome or not.
#include<stdio.h>
void main()
{
    int n,i=2,j,k=3,prime,rev=0,rem,temp;
    clrscr();
    printf("Enter nth number:");
    scanf("%d",&n);
    printf("2\n");
    while(i<=n)
    {
        for(j=2;j<=k-1;j++)
        {
            if(k%j==0)
            {
                break;
            }
        }
        if(j==k)
        {
            printf("%d\n",k);
            i++;
            prime=k;
        }
        k++;
    }
    temp=prime;
    while(temp!=0)
    {
        rem = temp%10;
        rev = rev * 10 + rem;
        temp = temp / 10;
    }
    printf("%dth Prime Number is %d \n",n,prime);
    if(prime==rev)
        printf("It is Palindrome. \n");
    else
        printf("It is not Palindrome. \n");

    getch();
}

3. Program to check whether a given number is Armstrong, odd or even, perfect square or cube.

//3. Program to check whether a given number is Armstrong, odd or even, perfect square or cube.
#include<stdio.h>
#include<math.h>
void main()
{
    int number,i,temp,sflag=0,cflag=0;
    double remainder, result = 0,n=0;
    clrscr();
    printf("Enter a number: ");
    scanf("%d", &number);
    temp = number;
   
    while (temp != 0)
    {
    temp =temp / 10;
    ++n;
    //printf("n=%d\n",n); //This is for debugging.
    }
    temp = number;
    while (temp != 0)
    {
    remainder = temp%10;
    result = result + pow(remainder, n);
    temp = temp / 10;
    //fflush(stdin);
    }
    if(result == number)
    printf("It is an Armstrong number.\n");
    else
    printf("It is not an Armstrong number.\n");
    // True if the number is perfectly divisible by 2
    if(number % 2 == 0)
    printf("It is an even number.\n");
    else
    printf("It is an odd number.\n");


    for(i=0;i<number/2;i++)
    {
        if(i*i==number)
        {
            printf("It is a perfect square.\n");
            sflag=1;
        }

        if(i*i*i==number)
        {
            printf("It is a perfect cube.\n");
            cflag=1;
        }

    }
    if(sflag==0)
        printf("It is not a perfect square.\n");
    if(cflag==0)
        printf("It is not a perfect cube.\n");
      getch();
}

1. C Program to find the biggest and smallest among 4 numbers using nested if.

//1. C Program to find the biggest and smallest among 4 numbers using nested if.
#include<stdio.h>
void main()
{
    int a=400,b=300,c=20,d=200,big,small; //Declare variables and initialize them.
    clrscr();    //Clear the screen.
    if((a>b && a>c) && a>d)      //Check whether a is greater than b,c and d.
    {
        big=a;
        if(b<c && b<d)       //Check whether b is lesser than c and d.
            small=b;
        else if(c<d)               //Check whether c is lesser than d.
            small=c;
        else
            small=d;

    }
    else if(b>c && b>d)             //Check whether b is greater than c and d.
    {
        big=b;
        if(a<c && a<d)             //Check whether a is lesser than c and d.
            small = a;
        else if(c<d)               //Check whether c is lesser than d.
            small = c;
        else
            small = d;

    }
    else if(c>d)                       //Check whether c is greater than d.
    {
        big=c;
        if(a<b && a<d)          //Check whether a is lesser than b and d.
            small=a;
        else if(b<d)               //Check whether b is lesser than d.
            small=b;
        else
            small=d;

    }
    else
    {
        big=d;
        if(a<b && a<c)
            small=a;
        else if(b<c)
            small=b;
        else
            small=c;
    }
    printf("Biggest Number=%d \t Smallest Number=%d",big,small);
    getch();
}

Thursday, 24 January 2019

3. Write a java program to find area of geometric figures (atleast 3) using method overloading.


import java.util.*;
import java.io.*;
import java.lang.*;
class geoFig
{
              double area(double r)
              {
              return(3.14*r*r); //Cicle
              }

              float area(float s)
              {
              return(s*s); //Square
              }
            
double area(double b,double h)
    {
           return(0.5*b*h); //Triangle
    }
}
class geoMain
{
        public static  void main(String arg[]) throws IOException
        {
            DataInputStream ob=new DataInputStream(System.in);
             geoFig g = new geoFig();
              System.out.println("enter double value for radius of circle");
             double r=Double.valueOf(ob.readLine()).doubleValue();
             System.out.println("area of circle="+g.area(r));
              System.out.println("enter float value for side of a square");
             float s=Float.valueOf(ob.readLine()).floatValue();
             System.out.println("area of square="+g.area(s));
     System.out.println("enter double value for base & height of triangle");
             double b1=Double.valueOf(ob.readLine()).doubleValue();
             double h=Double.valueOf(ob.readLine()).doubleValue();
             System.out.println("area of triangle="+g.area(b1,h));
    }
}
FREE Hit Counters