Implemet Stack in python

 

class Stack:
def __init__(self, data ):
self.stack = []
if(data):
self.stack.append(data)
def push(self, data ):
self.stack.append(data)
def pop(self):
popped_elemnt = self.stack.pop()
def show(self):
for i in self.stack:
print(i)
s = Stack(45)
s.push(23)
s.push(29)
s.push(695)
s.show()
s.pop()
s.show()

Arrays In Javascript.

Consider this small code sample in Javascript.


var arrayOfNumbers=[1,2,3,4,5,6,78];
for(var index in arrayOfNumbers){
    console.log(index+1);
};

Output of this snippet will be

01
11
21
31
41
51
61

Now this is because Arrays are treated as Objects in Javascript, so index is iterating over keys in an object. In this case the object will be represented as follows

arrayOfNumbers={ "0":1,
                 "1":2,
                 "2":3,
                 "3":4,
                 "4":5,
                 "5":6,
                 "6":78}

Write a program to check whether two strings are anagram or not.


import java.util.*;
public class anagrams {
    public static void main(String[] args) {
        int count=0,i;
        Scanner sc= new Scanner(System.in);
        System.out.println("enter first string");
        String a=sc.nextLine();
        System.out.println("enter second string");
        String b=sc.nextLine();
        a=a.toLowerCase();
        b=b.toLowerCase();
        char a1[]=a.toCharArray();
        char a2[]=b.toCharArray();
        Arrays.sort(a1);
        Arrays.sort(a2);
        if(a1.length!=a2.length)
            count++;
        if(count==0)
        for(i=0;i<a1.length;i++)
            if(a1[i]!=a2[i])

                count++;

        if(count==0)
            System.out.println("anagrams");
        else
            System.out.println("not anagrams");
    }
}

Solution for SAMER08F problem in SPOJ.

problem link-  
http://www.spoj.com/problems/SAMER08F/

#include<stdio.h>
#include<math.h>
int main()
{
int n,i,sum=0;
while(1)
{
scanf("%d",&n);
if(n==0)
break;
for(i=1;i<=n;i++)
sum=sum+ pow(i, 2);
printf("%d\n",sum);
sum=0;
}
return 0;
}


this problem can be done without  using loop. 

wap to print numbers from 1 to 100 without the use of loop.

#include<stdio.h>
main()
{
static int i=100;
static int a=1;
if(--i)
main();
printf("%d\n",a++);
}

wap to check whether an integer is a power of 2..



#include<stdio.h>
main()
{
int n,i;
printf("\nenter the number\n");
scanf("%d",&n);
i=n-1;
if((n&i)==0)
   printf("\ninteger is a power of 2\n");
else
   printf("\ninteger is not a power of 2\n");
}




wap in c to find the total number of divisors of a given number.

#include<stdio.h>
#include<conio.h>
#include<math.h>
int arr[100000];
int prime(m,n)
{
int count;
long int i,j,t=1,a=1;
while(t--)
{
 if(m==1||m==2)
 arr[0]=2;
 if(m%2==0)
 m=m+1;
 for(i=m;i<=n;i=i+2)
   {
   count=0;
   for(j=3;j<=sqrt(i);j=j+2)
   if(i%j==0)
   {count++;break;}
  if(count==0&&i!=1)
   {arr[a]=i;
   a++;}
   }
}
return(0);
}
main()
{
int divisors=1,count=0,n,i=0,j;
printf("enter the number\n");
scanf("%d",&n);
prime(1,n);
while(n>1)
{
   {for(j=0;;j++)
    if(n%arr[i]==0)
     {n=n/arr[i];
      count++;}
    else
    {i++;break;}
     }
divisors=divisors*(count+1);
count=0;
}
printf("\nthe no of divisors is %d",divisors);
getch();
}

output 


wap in c to print the prime numbers between two numbers using sieve method.

To know about sieve method go to --http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes

#include<stdio.h>
#include<math.h>
int main()
{
int count;
long int n,m,i,j,t=1;
while(t--)
{
printf("enter the numbers between you want to print the prime numbers");
scanf("%d %d",&m,&n);
if(m==1||m==2)
    printf("%d\n",2);
if(m%2==0)
m=m+1;
for(i=m;i<=n;i=i+2)
{
count=0;
for(j=3;j<=sqrt(i);j=j+2)
if(i%j==0)
{count++;break;}
if(count==0&&i!=1)
printf("%d\n",i);
}
printf("\n");
}
return(0);
}





wap in c to implement newton's forward interpolation formula.

Question-- using newton forward interpolation formula,find the area of a circle of diameter 82 metre                                from the given table of diameter and area of circle.

Diameter(metre)      80          85           90          95            100
Area(metre^2)      5026        5674      6362     7088         7854


code to solve the above question.

#include<stdio.h>
#include<math.h>
main()
{
int i,j,a=0,n;
float x[10],fx[10],delfx[10],result=0,point,f,h,p,fact=1.000;
printf("\nthe point at which u want to calculate the value\n");
scanf("%f",&point);
printf("\nenter the no of elements in x\n");
scanf("%d",&n);

printf("enter the elements of x\n");
for(i=0;i<n;i++)
scanf("%f",&x[i]);

h=fabs(x[0]-x[1]);
p=(point-x[0])/h;
printf("\nthe value of p is %f\n",p);
printf("enter the elements of f(x)\n");
for(i=0;i<n;i++)
scanf("%f",&fx[i]);
f=fx[0];
for(j=n;j>1;j--)
{
for(i=0;i<j-1;i++)
{
fx[i]=fx[i+1]-fx[i];
}
delfx[a]=fx[0];
a++;}
for(i=0;i<a;i++)
{
if(i!=0)
p=p*(p-i);
fact=fact*(i+1);
result=result+(p/fact)*delfx[i];
}
result=result+f;
printf("the result is %f",result);
}


output

wap to implement newton raphson method to find the root of x*sin(x)+cos(x).

#define f(x) x*sin(x)+cos(x)
#define df(x) sin(x)+x*cos(x)-sin(x)
#include<stdio.h>
# include<math.h>
 main()
{
float i,a,b,c;
for(i=0;;i++)
{
if(f(i)>0&&f(i+1)<0)
{
a=i;b=i+1;break;}
if((f(i)<0)&&(f(i+1)>0))
{
a=i+1;b=i;break;}
}
for(i=0;;i++)
{
if(f(b)>0&&df(b)<0)
c=b+(f(b)/fabs(df(b)));
if(f(b)<0&&df(b)>0)
c=b+(fabs(f(b)/df(b)));
if((f(b)>0&&df(b)>0)||(f(b)<0&&df(b)<0))
c=b-(fabs(f(b))/fabs(df(b)));
if(fabs(c-b)<.00001)
break;
b=c;
}
printf("the root is %f",c);
}


wap to store data of sparse matrix efficiently.

#include<stdio.h>
main()
{
int arr[100][100],arr1[100][3],i,j,m,n,a=1,b=0;
printf("enter the no of columns and rows\n");
scanf("%d%d",&m,&n);
arr1[0][0]=m;arr1[0][1]=n;
printf("enter the elements of the matrix\n");
for(i=0;i<m;i++)
for(j=0;j<n;j++)
{
scanf("%d",&arr[i][j]);
if(arr[i][j]!=0)
{
arr1[a][b]=i;
arr1[a][b+1]=j;
arr1[a][b+2]=arr[i][j];
a++;
}
}
arr1[0][2]=a-1;
printf("the sparse matrix is as follows\n");
for(i=0;i<a;i++){
for(j=0;j<3;j++)
printf("%d\t",arr1[i][j]);
printf("\n");}
}

wap to find the second largest element from an array.

#include<stdio.h>
main()
{
int i,n,arr[100],a=0,b=0;
printf("enter the no of elements in the array\n");
scanf("%d",&n);
printf("enter the elements of the array\n");
for(i=0;i<n;i++)
scanf("%d",&arr[i]);
for(i=1;i<n;i++)
if(arr[a]<arr[i])
{
b=arr[a];
a=i;
}
else
if(b<arr[i])
b=arr[i];
printf("\n the second largest number is %d ",b);
}

wap to implement bisection method to find the root of (X)*log10(X)-1.2.



#define f(x) (x)*log10(x)-1.2
#include<stdio.h>
# include<math.h>
 main()
{
float i,a,b,c,t=0;
for(i=0;;i++)
{
if(f(i)>0&&f(i+1)<0)
{
a=i;b=i+1;break;}
if((f(i)<0)&&(f(i+1)>0))
{
a=i+1;b=i;break;}
}
for(i=0;;i++)
{
c=(a+b)/2;
if(f(c)>0)
a=c;
else
b=c;
if(fabs(c-t)<=.00005)
break;
t=c;
}
printf("\nthe root is %f",c);
}

write a c program that returns the position of the largest element in an array.


#include<stdio.h>
void main()
{
int array[100],j=0,n,i;
printf("enter the no of elemnts in the array\n");
scanf("%d",&n);
printf("enter the elements of the array\n");
for(i=0;i<n;i++)
scanf("%d",&array[i]);
for(i=1;i<n;i++)
{
if(array[j]<array[i])
j=i;
}
printf("\nthe position of largets element is= %d",j+1);
}

define a macro that does not make use of a bitwise XOR operator to swap the contents of two variables.


#include<stdio.h>
#define swap(a,b) {a=a+b;b=a-b;a=a-b;printf("x=%d y=%d",a,b);}
void main()
{
    int x,y;
    printf("enter the two numbers\n");
    scanf("%d%d",&x,&y);
    printf("the numbers before swapping are\n x=%d y=%d\n",x,y);
    printf("the numbers after swapping are\n");
    swap(x,y);
}

NOTE-  i have compiled this program in codeblocks if u do it in turbo c++  use  conio.h  header file in the beginning  and getch()  a the  last.

output image


wap to find whether a number entered by user is automorphic or not.

if u want to know what is automorphic number then go to-http://en.wikipedia.org/wiki/Automorphic_number

#include<stdio.h>
void main()
{
    long int count=1,n,a,m,div=1,i;
    printf("enter the number \n");
    scanf("%d",&n);
    a=n;
    while((n/10)!=0)
    {
    n=n/10;
    count++;
    }
    m=a*a;
    for(i=1;i<=count;i++)
        div=div*10;
    if(m%div==a)

    printf("\nit is an automorphic number");
        else
    printf("\nit is not an automorphic number");


}

NOTE-  i have compiled this program in codeblocks if u do it in turbo c++  use  conio.h  header file in the beginning  and getch()  a the  last.


output window (click to enlarge).



define a macro to find the factors of a number.

#include<stdio.h>
#define factor(num) {do{if(num%a==0){num=(num/a);printf("\n%d",a);}else a++;}while(num!=1);}
void main()
{
    int a=2,num;
    printf("enter the number\n");
    scanf("%d",&num);
    printf("%d",1);
    factor(num);
}


NOTE-  i have compiled this program in codeblocks if u do it in turbo c++  use  conio.h  header file in the beginning  and getch()  a the  last.


output image  (click to enlarge).


wap to find the lcm of three numbers.


#include<stdio.h>
void main()
{

    int num,num2,num3,a=2,lcm=1;
    printf("enter the three numbers\n");
    scanf("%d%d%d",&num,&num2,&num3);
    while((num!=1)||(num2!=1)||(num3!=1))
        if((num%a==0)||(num2%a==0)||(num3%a==0))
        {
        if(num%a==0)num=num/a;
    if(num2%a==0)num2=num2/a;
    if(num3%a==0)num3=num3/a;
    lcm=lcm*a;
        }
        else
            a++;
        printf("\nthe lcm of the three numbers is  %d",lcm);
}


NOTE-  i have compiled this program in codeblocks if u do it in turbo c++  use  conio.h  header file in the beginning  and getch()  a the  last.


here is the proof of the code running..........(click on the image to enlarge)




wap to find the factors of a number.


#include<stdio.h>
void main()
{
    int a=2,num;
    printf("enter the number");
    scanf("%d",&num);
    printf("the factors of the number are\n%d",1);
    do
    {
        if(num%a==0)
        {
            num=(num/a);
        printf("\n%d",a);
        }
        else
            a++;
    }while(num!=1);

}

NOTE-  i have compiled this program in codeblocks if u do it in turbo c++  use  conio.h  header file in the beginning  and getch()  a the  last.

Implemet Stack in python

  class Stack : def __init__ ( self , data ): self . stack = [] if ( data ): self . stack . append ( da...