Search This Blog

Thursday 12 September 2019

10. C Program to generate n prime numbers and print them in pyramid pattern.

//10. C Program to generate n prime numbers and print them in pyramid pattern.

#include <stdio.h>
#include <conio.h>
void main()
{
    int space, n, prime[100];     //Declare variables.
    int count=0, i=2, j, k=2;
    clrscr();
    printf("Enter nth number:");
    scanf("%d",&n);
    while(i<=n+1)
    {
    for(j=2;j<=k-1;j++)
    {
        if(k%j==0)             //Check whether k is divisible by any other number.
        {
        break;
        }
    }
    if(j==k)
    {
       // printf("%d\n",k);    //For debugging.
        prime[count++]=k;
        i++;
    }
    k++;
    }
    count=0;
    space=n;
    for (i=0;i<=n/2+1;i++)
    {
    for (k=0;k<space;k++)
    {
        printf("   "); //Provide 2 spaces
    }
    for (j=0;j<2*i-1;j++)
    {
        if(count<n)
        printf("%d  ",prime[count++]); //Provide 2 spaces after %d
    }
    space--;
    printf("\n");
    }
    getch();
   
}

Tuesday 16 July 2019

9. C program to count the number of vowels, consonants and special characters in a string

/*9. C program to count the number of vowels, consonants and special characters in a string.*/
#include <stdio.h>

void main()
{
    char   str[100];
    int i, digit, vowel, consonant, splchar, spaces;
    clrscr();

    digit = vowel = consonant = splchar = spaces = 0;

    printf("Enter a string: ");
    gets(str);

    for(i=0;str[i]!=NULL;i++)
    {

    if(str[i]>='0' && str[i]<='9')
        digit++;
    else if(str[i]==' ')
        spaces++;
    else if(str[i]=='A' || str[i]=='E' || str[i]=='I' || str[i]== 'O' || str[i]=='U' ||
    str[i]=='a' || str[i]=='e' || str[i]=='i' || str[i]=='o' || str[i]=='u')

        vowel++;
    else if((str[i]>='A' && str[i]<='Z')||(str[i]>='a' && str[i]<='z'))
        consonant++;
    else
        splchar++;
    }

    printf("\nDigits: %d \nVowels: %d \nSpaces: %d \nConsonants: %d \nSpecial Characters: %d",digit,vowel,spaces, consonant,splchar);

    getch();
}

8. Program to find e to the power x using n terms of the exponential series.

// 8. Program to find e to the power x using n terms of the exponential series.
#include<stdio.h>
#include<conio.h>
#include<math.h>
      //Correction is required
void main()
{
    int i,n,j;
    float x,t, exp , sum=0;
    clrscr();

    printf("Enter the value for x:");
    scanf("%f", &x);

    printf("\nEnter the value for n:");
    scanf("%d", &n);
    if(n<=1)
    {
    printf("1\n");
    getch();
    exit(0);
    }
    printf("\nThe exponential series is:\n");
    
    for(i=1;i<n;i++)
    {
       if(i==1)
      printf("%d + ",i);
       t=i;
       for(j=i;j>1;j--)
       {
          t=t*(j-1);
       }

       exp=pow(x,i)/t;
       sum=sum+exp;
       printf("%.3f + ",exp);
    }
    printf("\nThe Exponential Value = %f",sum+1);
    getch();
}

7. Program to generate n terms of the series 1,-2,6,-24,120.....

//7. Program to generate n terms of the series 1,-2,6,-24,120.....
#include<stdio.h>
void main()
{
    int n=10,i,j=-2,k=1;
    clrscr();
    printf("Enter the limit:");
    scanf("%d",&n);
    printf("The Series is:\n");
    for(i=0;i<n;i++)
    {
           printf("%d\n",k);
           k=k*(j);
           j--;
    }
    getch();
}

6. Program to convert decimal number into binary.

//6. Program to convert decimal number into binary.
#include<stdio.h>
void main()
{
    int dec,bin=0,rem,i=1;
    clrscr();
    printf("Enter a decimal number:");
    scanf("%d",&dec);
    do
    {
        rem = dec%2;
        bin = bin + rem * i;
        dec = dec /2;
        i=i*10;
    }while(dec>0);
    printf("Binary Equivalent = %d",bin);
    getch();
}

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));
    }
}

2. Write a Java program to create a vector, add elements at the end, at specified location onto the vector and display the elements. Write an option driven program using switch…case.


import java.util.*;
import java.io.*;
import java.lang.String;

class VectorPro
{
    public static void main(String args[])
    {
        try
        {
       
            int length = args.length;
            int i,ch;
            String str1,str2;

            Vector list = new Vector();

            for( i=0;i<length;i++)
            {
                list.addElement(args[i]);
            }

            DataInputStream in= new DataInputStream(System.in);
           
            for(;;)
            {
                System.out.println("1.Add element at end");
                System.out.println("2.Add element at specific location");
                System.out.println("3.Display");
                System.out.println("4.Exit");       
                System.out.println("Enter your choice:");
   
                str1=in.readLine();
                ch=Integer.parseInt(str1);

                switch(ch)
                {
                   
             case 1: System.out.print("Enter an element to insert at end:");
                            str2=in.readLine();
                            list.addElement(str2);
                            length++;   
                            break;
                       
             case 2: System.out.print("Enter an element & position:");
                            str1=in.readLine();
                            str2=in.readLine();
                            int pos;
                            pos=Integer.parseInt(str2);
                            if(pos<=length && pos>0)
                            {
                                list.insertElementAt(str1,pos-1);
                                length++;
                                break;
                            }
                            else
                            {
                                System.out.println("Invalid Position!");
                            }
                   
            case 3:
                            System.out.println("List of elements:");
                            for(i=0;i<length;i++)
                            System.out.println(list.elementAt(i));
                            break;
                   
            default:       System.exit(0);
   
                }
                              
            }
        }
        catch(Exception e){}
    }
}

1. Java program to generate first odd numbers then pick and display prime numbers among them.



import java.io.*;

public class OddNumber

{

     public static void main(String args[]) throws IOException

     {

           int limit;

           int b=0;

           limit = Integer.parseInt(args[0]);

           int a[]= new int[50];

           System.out.println("Odd numbers between 1 and " + limit);

           for(int i=1; i <= limit; i++)

           {

                boolean isPrime = true;

                           for(int j=2; j < i ; j++)

                           {                       

                                 if(i % j == 0)

                                 {

                                      isPrime = false;

                                      break;

                                 }

                           }

                //if the number is not divisible by 2 then it is odd

                           if( i % 2 != 0)

                           {

                           System.out.print(i + " ");

                           }

                           if(isPrime)

                           {

                                 if(i>2)   //1 is not prime and 2 is even.

                                 {

                                      a[b] =i;

                                      b++;

                                 }

                           }

           }

           System.out.println("\nPrime numbers are as follows:");

                for(int k=0;k<=b-1;k++)

                           System.out.println(a[k]);

     }

}
FREE Hit Counters