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

Monday, February 16, 2015

Java program to convert given number of days into months and days

Java program to convert given number of days into months and days

class Days
{
public static void main(String...s)
{

int n,m,d;
n=Integer.parseInt(s[0]);

m=n/30;
d=n%30;

System.out.println(n+" days"+" = "+m+" months "+"and "+d+" days");
}
}
Read more »

C program to find cube of a number using macros


C++ program to find cube of a number using macros

Also Read: C++ program to swap two numbers using macros
Also Read: C++ Program to find cube of a number using function

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

#define CUBE(x) (x*x*x)

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

cube=CUBE(n);
cout<<"Cube="<<cube;
getch();
}
Read more »

Sunday, February 15, 2015

C C Program to Print Following Pattern

ABCDEFGFEDCBA
ABCDEF  FEDCBA
ABCDE     EDCBA
ABCD         DCBA
ABC             CBA
AB                 BA
A                     A

C Program

#include<stdio.h>

int main()
{
int i,j,k,l,m;
for(i=0;i<=6;i++)
{
for(k=65;k<=71-i;k++)
printf("%c",k);

for(j=1;j<=i*2-1;j++)
printf(" ");

for(l=71-i;l>=65;l--)
if(l!=71)
printf("%c",l);

printf("
");
}

return 0;
}

C++ Program

#include<iostream>

using namespace std;

int main()
{
int i,j,k,l,m;
for(i=0;i<=6;i++)
{
for(k=65;k<=71-i;k++)
cout<<(char)k; //typecasting integer to character

for(j=1;j<=i*2-1;j++)
cout<<" ";

for(l=71-i;l>=65;l--)
if(l!=71)
cout<<(char)l;
cout<<"
";
}

return 0;
}

C/C++ Program to Print Following Pattern ABCDEFGFEDCBA ABCDEF  FEDCBA ABCDE     EDCBA ABCD         DCBA ABC             CBA AB                 BA A                     A

Comment below if you find any difficulty in understanding the above programs.
Read more »

Saturday, February 14, 2015

C program to print numbers from 1 to 10 using for loop


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

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

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

}

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

Friday, February 13, 2015

C Program to sort an Array by using Bubble sort

C++ Program to sort an Array by using Bubble sort

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

void main()
{
clrscr();
int a[50],n,i,j,temp;
cout<<"Enter the size of array: ";
cin>>n;
cout<<"Enter the array elements: ";

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

for(i=0;i<n;++i)
for(j=0;j<(n-i);++j)
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}

cout<<"Array after bubble sort: ";
for(i=0;i<n;++i)
cout<<a[i]<<" ";
getch();
}
Read more »

C Program to concatenate two strings

C++ Program to concatenate two strings

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

void main()
{
clrscr();
char str1[30],str2[30],str3[60];
int i,j;
cout<<"Enter first string:";
gets(str1);
cout<<"
Enter second string:";

gets(str2);

for(i=0;str1[i]!=
Read more »

C Program and Algorithm for Evaluation of a Postfix Expression


In postfix or reverse polish notation, every operator follows all of its operands. For example 5 3 2 * +.

Algorithm for Evaluation of Postfix Expression

Create an empty stack and start scanning the postfix expression from left to right.

– If the element is an operand, push it into the stack.
– If the element is an operator O, pop twice and get A and B respectively. Calculate BOA and push it back to the stack.
– When the expression is ended, the value in the stack is the final answer.

Also Read: C Program for Array Representation of Stack [Push, Pop and Display]
Also Read: Menu Driven C Program to Perform Insert, Display and Delete Operations on a Singly Linked List (SLL)

Evaluation of a postfix expression using a stack is explained in below example:

Evaluation of a postfix expression using a stack

C Program for Evaluation of a Postfix Expression

//Assumption -- primary operators -,+,*,/,% operand -- a single digit

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

#define MAX 20

typedef struct stack
{
    int data[MAX];
    int top;
}stack;

void init(stack *);
int empty(stack *);
int full(stack *);
int pop(stack *);
void push(stack *,int);
int evaluate(char x,int op1,int op2);

void main()
{
    stack s;
    char x;
    int op1,op2,val;
    init(&s);
    printf("Enter the expression(eg: 59+3*)
Single digit operand and operators only:");

    while((x=getchar())!=
)
    {
        if(isdigit(x))
                push(&s,x-48);      //x-48 for removing the effect of ASCII
        else
        {
            op2=pop(&s);
            op1=pop(&s);
            val=evaluate(x,op1,op2);
            push(&s,val);
        }
    }

    val=pop(&s);
    printf("
Value of expression=%d",val);
}

int evaluate(char x,int op1,int op2)
{
    if(x==+)
        return(op1+op2);
    if(x==-)
        return(op1-op2);
    if(x==*)
        return(op1*op2);
    if(x==/)
            return(op1/op2);
    if(x==%)
            return(op1%op2);
}

void init(stack *s)
{
    s->top=-1;
}

int empty(stack *s)
{
    if(s->top==-1)
        return(1);
    return(0);
}

int full(stack *s)
{
    if(s->top==MAX-1)
        return(1);
    return(0);
}

void push(stack *s,int x)
{
    s->top=s->top+1;
    s->data[s->top]=x;
}

int pop(stack *s)
{
    int x;
    x=s->data[s->top];
    s->top=s->top-1;
    return(x);
}

C Program for Evaluation of a Postfix Expression

Image Credit: http://cis.stvincent.edu/html/tutorials/swd/stacks/stacks.html
Read more »

C program to find sum of two numbers

C program to find sum of two numbers

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


void main()
{
int a,b,sum;
clrscr(); //to clear the screen
printf("Enter first no:");
scanf("%d",&a);
printf("Enter second no:");
scanf("%d",&b);
sum=a+b;
printf("
Sum=%d",sum);
getch(); //to stop the screen
}
Read more »

Wednesday, February 11, 2015

C C Program for Fibonacci Series Using Recursion

Series 0, 1, 1, 2, 3, 5, 8, 13, 21 . . . . . . . is a Fibonacci series. In Fibonacci series, each term is the sum of the two preceding terms. The C and C++ program for Fibonacci series is given below.

C Program


#include<stdio.h>

int fibonacci(int n)
{
if((n==1)||(n==0))
{
return(n);
}
else
{
return(fibonacci(n-1)+fibonacci(n-2));
}
}

int main()
{
int n,i=0;
printf("Input the number of terms for Fibonacci Series:");
scanf("%d",&n);
printf("
Fibonnaci Series is as follows
");
while(i<n)
{
printf("%d ",fibonacci(i));
i++;
}

return 0;
}

C++ Program


#include<iostream>

using namespace std;

int fibonacci(int n)
{
if((n==1)||(n==0))
{
return(n);
}
else
{
return(fibonacci(n-1)+fibonacci(n-2));
}
}

int main()
{
int n,i=0;
cout<<"Input the number of terms for Fibonacci Series:";
cin>>n;
cout<<"
Fibonnaci Series is as follows
";

while(i<n)
{
cout<<" "<<fibonacci(i);
i++;
}

return 0;
}

C/C++ Program for Fibonacci Series Using Recursion
Read more »

Tuesday, February 10, 2015

Simple program to create a circular loading bar using graphics


Simple program to create a circular loading bar using graphics

#include<graphics.h>
#include<dos.h>

void main()
{
int gd=DETECT,gm,i;
initgraph(&gd,&gm,"c:\turboc3\bgi");
for(i=0;i<=360;++i)
{
circle(300,200,80);
pieslice(300,200,0,i,80);
outtextxy(200,320,"Loading....Please Wait!");
delay(20);
}
closegraph();
}
Read more »