Showing posts with label program. Show all posts
Showing posts with label program. Show all posts

Wednesday, February 18, 2015

Java program to display multiplication table of any number

Java program to display multiplication table of any number

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

for(i=1;i<=10;++i)
System.out.println(n+" * "+i+" = "+(n*i));
}
}
Read more »

C program to perform arithmetic operations using switch case


C program to perform arithmetic operations using switch case

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

void main()
{
int ch;
float a,b,res;
clrscr();
printf("Enter two numbers:");
scanf("%f%f",&a,&b);
printf("
Menu
1.Addition
2.Subtraction
3.Multiplication
4.Division");

printf("
Enter your choice:");

scanf("%d",&ch);

switch(ch)
{
case 1: res=a+b;
break;
case 2: res=a-b;
break;
case 3: res=a*b;
break;
case 4: res=a/b;
break;
default: printf("Wrong choice!!
Press any key...");

getch();
exit(0);
}

printf("
Result=%f",res);

getch();
}
Read more »

Tuesday, February 17, 2015

C program to print following square using character


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

void main()
{
int i,j,n;
clrscr(); //to clear the screen
printf("Enter size of the square:");
scanf("%d",&n);

for(i=0;i<n;++i)
{
printf("
");
for(j=0;j<n;++j)
{
if(i==0||i==n-1||j==0||j==n-1)
printf("*");
else
printf(" ");
}
}
getch(); //to stop the screen
}
Read more »

C Program to convert a lowercase alphabet to uppercase or vice versa

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

void main()
{
clrscr();
char ch;
cout<<"Enter any Alphabet:";
cin>>ch;

if(ch>=a&&ch<=z)
{
cout<<"
You have entered a lowercase alphabet";

ch=ch-32;
cout<<"

The uppercase alphabet is "<<ch;

}
else
{
cout<<"
You have entered an Uppercase alphabet";

ch=ch+32;
cout<<"

The lowercase alphabet is "<<ch;

}
getch();
}
Read more »

Monday, February 16, 2015

C Program to reverse all the strings stored in an array

C++ Program to reverse all the strings stored in an array

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

void main()
{
clrscr();
char a[3][50];
int i,j,k,len;
cout<<"Enter 3 strings:
";


for(i=0;i<3;i++)
{
gets(a[i]);
}
cout<<"
The list of orignal strings:
" ;


for(i=0;i<3;i++)
{
cout<<a[i]<<"
";

}
cout<<"
The list of changed string:
";


for(i=0;i<3;i++)
{
len=strlen(a[i]);
for(j=0,k=len-1;k>=0;j++,k--)
{
cout<<a[i][k];
}
cout<<"
";

}
getch();
}
Read more »

Java Program to Find Union of two Arrays

For example we have two sorted arrays a1[]={2,3,5,11} and a2[]={4,7,9} then union of a1 and a2 will be {2,3,4,5,7,9,11}. A Java program for finding union of two arrays is given below.

Also Read: Java Program to Find Smallest and Largest Element in an Array

Java Program to Find Union of two Arrays

import java.util.Scanner; //import Scanner class in our program

class demo
{
public static void main(String...s)
{
int i,j,n1,n2;
Scanner sc=new Scanner(System.in); //used to read from keyboard

System.out.print("Enter number of elements of first array:");
n1=sc.nextInt();
System.out.print("Enter number of elements of second array:");
n2=sc.nextInt();

int a1[]=new int[n1];
int a2[]=new int[n2];

System.out.print("
Enter elements of first array in ascending order:");

for(i=0;i<n1;++i)
a1[i]=sc.nextInt();

System.out.print("
Enter elements of second array in ascending order:");

for(i=0;i<n2;++i)
a2[i]=sc.nextInt();


i=j=0;
System.out.print("
Union of Arrays: ");

while(i<n1&&j<n2)
{
if(a1[i]<a2[j])
{
System.out.print(a1[i]+" ");
i++;
}
else
if(a2[j]<a1[i])
{
System.out.print(a2[j]+" ");
j++;
}
else
    {
System.out.print(a1[i]+" ");
     i++;
     j++;
    }
}

if(i<n1)
while(i<n1)
{
System.out.print(a1[i]+" ");
i++;
}

if(j<n2)
while(j<n2)
{
System.out.print(a2[j]+" ");
j++;
}

}
}


Java Program to Find Union of two Arrays
Read more »

Sunday, February 15, 2015

C program to swap two numbers using macros

C++ program to swap two numbers using macros

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

#define SWAP(a,b) {int temp; temp=a; a=b; b=temp;}

void main()
{
clrscr();
int x,y;
cout<<"Enter two numbers:";
cin>>x>>y;

cout<<"x="<<x<<" y="<<y;
SWAP(x,y);
cout<<"
x="<<x<<" y="<<y;

getch();
}
Read more »

Saturday, February 14, 2015

C Program to explain Binary search in Array

C++ Program to explain Binary search in Array

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

void main()
{
int search(int [],int,int);
clrscr();
int n,i,a[100],e=-3,res;
cout<<"How Many Elements:";
cin>>n;
cout<<"
Enter Elements of Array in Accending order
";


for(i=0;i<n;++i)
{
cin>>a[i];
}

cout<<"
Enter element to search:";

cin>>e;
res=search(a,n,e);
if(res!=0)
cout<<"
Element is Founded at "<<res+1<<"st position";

else
cout<<"
Element is not found....!!!";

getch();
}

int search(int a[],int n,int e)
{
int f,l,m;
f=0;
l=n-1;
while(f<=l)
{
m=(f+l)/2;
if(e==a[m])
return(m);
else
if(e>a[m])
f=m+1;
else
l=m-1;
}
return 0;
}
Read more »

C program to find factorial of any number using recursion

C program to find factorial of any number using recursion

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

void main()
{
int fac,n;
int factorial(int);
clrscr();

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

fac=factorial(n);
printf("Factorial=%d",fac);
getch();
}

int factorial(int x)
{
int f;
if(x==1||x==0)
return 1;
else
f=x*factorial(x-1);

return f;
}
Read more »

Friday, February 13, 2015

C program to print the following pattern


C++ program to print the following pattern:

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

void main()
{
clrscr(); //to clear the screen
int i,j,k,n;
cout<<"How many lines?";
cin>>n;
n*=2;

for(i=0;i<n;i+=2)
{
cout<<"
";

for(j=n;j>i;j-=2)
cout<<" ";
for(k=0;k<=i;++k)
cout<<"*";
}
getch(); //to stop the screen
}
Read more »

Thursday, February 12, 2015

C program to find greatest number among three numbers


C program to find greatest number among three numbers

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

void main()
{
int x,y,z,max;
clrscr();
printf("Enter The Three Numbers:");
scanf("%d%d%d",&x,&y,&z);

max=x;
if(y>max&&y>z)
max=y;
else
if(z>max)
max=z;
printf("
The Greatest Number among %d %d %d is %d",x,y,z,max);

getch();
}
Read more »

C program to add two numbers using structure

C program to add two numbers using structure

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

struct sum
{
int a;
int b;
};

void main()
{
int sum1;
struct sum s;
clrscr();

printf("Enter two numbers:");
scanf("%d%d",&s.a,&s.b);

sum1=s.a+s.b;
printf("
Sum=%d",sum1);

getch();
}
Read more »

C program to swap two numbers using pointers


C++ program to swap two numbers using pointers

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

void main()
{
clrscr();
int *a,*b,*temp;
cout<<"Enter value of a and b:";
cin>>*a>>*b;

temp=a;
a=b;
b=temp;

cout<<"
After swaping
a="<<*a<<"
b="<<*b;

getch();
}
Read more »

C program to find sum of series 1 1 2 2 1 3 3 1 n n


C++ program to find sum of series 1+1/2^2+1/3^3+.....+1/n^n

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

void main()
{
clrscr();
double sum=0,a;
int n,i;
cout<<"1+1/2^2+1/3^3+.....+1/n^n";
cout<<"
Enter value of n:";

cin>>n;

for(i=1;i<=n;++i)
{
a=1/pow(i,i);
sum+=a;
}

cout<<"Sum="<<sum;
getch();
}
Read more »

Wednesday, February 11, 2015

My First Java Program Print a Message Hello World

My First Java Program: Print a Message "Hello World"

class HelloWorld
{
public static void main(String...s)
{
System.out.println("Hello World");
}
}
Read more »

C Program to perform all arithmetic calculation using switch case

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

void main()
{
clrscr();
float a,b,res;
int ch,q;
cout<<"Arithmetic Operatios";
cout<<"

1.Addition
2.Subtraction
3.Multiplication
4.Division
5.Mode";

cout<<"
 Enter your choice:";

cin>>ch;

switch(ch)
{
case 1:
{
cout<<"

Enter two variables:";

cin>>a>>b;
res=a+b;
cout<<"
 Result="<<res;

}
break;

case 2:
{
cout<<"

Enter two variables:";

cin>>a>>b;
res=a-b;
cout<<"
 Result="<<res;

}
break;

case 3:
{
cout<<"

Enter two variables:";

cin>>a>>b;
res=a*b;
cout<<"
 Result="<<res;

}
break;

case 4:
{
cout<<"

Enter two variables:";

cin>>a>>b;
if(a>=b)
{
res=a/b;
cout<<"
 Result="<<res;

}
else
cout<<"

1st varable should be greater than 2nd.!!!";

}
break;

case 5:
{
cout<<"

Enter two variables:";

cin>>a>>b;
if(a>=b)
{
q=a/b;
res=a-(b*q);
cout<<"
 Result="<<res;

}
else
cout<<"

1st variable should be greater than 2nd..!!!";

}
break;
}

getch();
}
Read more »

C Program to find divisers of a number


#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
long int n,i;
cout<<"Enter the number: ";
cin>>n;
cout<<endl<<"Divisers of "<<n<<" are ";
for(i=1;i<=n;++i)
{
if(n%i==0)
cout<<" "<<i;
}
getch();
}
Read more »

Tuesday, February 10, 2015

Valentine’s Day Special C Program to Print Heart Shape with Happy Valentine’s Day Message inside it

Today’s day is very special for all lovers. So I thought that I should share some programming stuff that show Valentine’s Day felling. A C++ program is given below which prints heart shape with a Happy Valentine’s Day message inside it. If you want, you can change the message by changing the value of string message. I hope that you will like this.

Also Read: Simple program to create a moving car in graphics
Also Read: Simple program to create a circular loading bar using graphics


#include <iostream>
#include <cmath>

using namespace std;

int main()
{
    double x, y, size=10;
    char ch=3;
    string message(" Happy Valentines Day ");
    int print_line = 4;

    if (message.length() % 2 != 0) message += " ";

    for (x=0;x<size;x++)
    {
        for (y=0;y<=4*size;y++)
        {
            double dist1 = sqrt( pow(x-size,2) + pow(y-size,2) );
            double dist2 = sqrt( pow(x-size,2) + pow(y-3*size,2) );

            if (dist1 < size + 0.5 || dist2 < size + 0.5 ) {
                cout << ch;
            }
            else cout << " ";
        }
        cout<<"
";
    }

    for (x=1;x<2*size;x++)
    {
        for(y=0;y<x;y++) cout << " ";

        for (y=0; y<4*size + 1 - 2*x; y++)
        {
            if (x >= print_line - 1 && x <= print_line + 1) {
                int idx = y - (4*size - 2*x - message.length()) / 2;
                if (idx < message.length() && idx >= 0) {
                    if (x == print_line) cout<<message[idx];
                    else cout << " ";
                }
                else cout << ch;
            }
            else cout << ch;
        }
        cout<<endl;
    }
    return 0;
}

Valentine’s Day Special: C++ Program to Print Heart Shape with Happy Valentine’s Day Message inside it
Read more »

C program to find largest number of a list of numbers entered through keyboard


C++ program to find largest number of a list of numbers entered through keyboard


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


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

cout<<"How many numbers?";
cin>>n;
for(i=0;i<n;++i)
{
cout<<"
Enter number "<<i+1<<":";

cin>>x;
if(x>large)
large=x;
}
cout<<"

The largest number is "<<large;

getch();
}
Read more »