C Tutor...

C Tutor...


C Program to convert infix to postfix expression

Posted: 02 Jun 2014 09:49 AM PDT

C Program to convert infix to postfix expression 





#include
#include
char infix[100],post[100];
int top=0,stack[10];
void push(int);
char pop();
void postfix();
main()
{
char ch;
clrscr();
printf("\n Infix Expression ");
gets(infix);
postfix();
getch();
}


void postfix()
{
int i=0,j=0;
for(i=0;infix[i]!='\0';i++)
{
switch(infix[i])
{
case '+': while(stack[top]>=1)
post[j++]=pop();
push(1);
break;
case '-': while(stack[top]>=1)
post[j++]=pop();
push(2);
break;
case '*': while(stack[top]>=3)
post[j++]=pop();
push(3);
break;
case '/': while(stack[top]>=3)
post[j++]=pop();
push(4);
break;
case '^': while(stack[top]>=4)
post[j++]=pop();
push(5);
break;
case '(': push(0);
break;
top--;
break;
default: post[j++]=infix[i];
}
while(top>0)
post[j++]=pop();
printf("\n The postfix expression is %s\n",post);
}


void push(int element )
{
top++;
stack[top]=element;
}
char pop()
{
int ele;
char ex;
ele=stack[top];
top--;
switch(ele)
{
case 1: ex='+';
break
case 2: ex='-';
break;
case 3: ex='*';
break;
case 4: ex='/';
break;
case 5= ex='^';
break;
}
return ex;
}




C Program to check whether a given matrix is symmetric or not

Posted: 02 Jun 2014 09:48 AM PDT

C Program to check whether a given matrix is symmetric or not 


A matrix is symmetric if its transpose is same as the matrix itself check the martix by using C Logic.





#include
#include

//write matrix
void write_mat(int a[][10],int n)
{
int i,j;
{
for(i=0;i<n;i++)
for(j=0;j<n;j++)
printf("%10d",a[i][j]);
printf("\n");
}
}

//read matrix
void read_mat(int a[][10],int n)
{
int i,j;
printf("Enter %d X %d matrix below:\n",n,n);
for(i=0;i<n;i++)
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
}

//main function
void main()
{
int a[10][10],i,j,n;

//accept matrix
printf("Enter size of square matrix");
scanf("%d",&n);
read_mat(a,n);

//check if the matrix is symmetric or not
for(i=0;i<n;i++)
for(j=i+1;j<n;j++)
if(a[i][j]!=a[j][i])
{
printf("Not symmetric");
return;
}
printf("Symmetric");
getch();
}




Java Program to find nth term in a TRIBONACCI series

Posted: 02 Jun 2014 09:45 AM PDT

Java Program to find nth term in a TRIBONACCI series






import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Tribonacci {

/**
* @param args
*/
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
BufferedReader reader = new BufferedReader(new InputStreamReader(
System.in));
System.out.println(" Enter the number ");
int num = Integer.parseInt(reader.readLine());
int first = 0, second = 1, third = 1, nthTerm = 0;
int num1 = num;
if (num == 1)
nthTerm = 0;

if (num == 2 || num == 3)
nthTerm = 1;

while (num > 3) {
nthTerm = first + second + third;
// System.out.print(d +"\t");
num--;
first = second;
second = third;
third = nthTerm;
}
System.out.print(num1 + "th term of the Tribonacci is " + nthTerm);
}
}




C Program to count the number of ones in a 32 bit number.

Posted: 02 Jun 2014 09:44 AM PDT

C Program to count the number of ones in a 32 bit number. 



#include

int bitcount(unsigned int n)
{
int count=0;
while(n)
{
count+=n& 0x1u;
n>>=1;
}
return count;
}

void main()
{
unsigned int g; int n;
printf(" \n Enter the number : \n");
scanf("%u",&g);
n=bitcount(g);
printf("It has %d ones",n);
}








Post a Comment