Friday, July 5, 2019

CALCULATOR PROGRAM USING TKINTER IN PYTHON


""" ***********************************************
programming Language :Python
Tool(IDE) used :Anaconda -> Spider
started date:03-07-2019
latest modified date:06-07-2018
version:1
created by:ANIL DURGAM

*****************************************************"""


from tkinter import *


# globally declare the expression variable

expression = ""


# Function to update expressiom

# in the text entry box

def press(num):

 # point out the global expression variable

 global expression

 # concatenation of string

 expression = expression + str(num)
 # update the expression by using set method

 equation.set(expression)
# Function to evaluate the final expression

def equalpress():

 # Try and except statement is used

 # for handling the errors like zero

 # division error etc.

 # Put that code inside the try block

 # which may generate the error

 try:

  global expression

  # eval function evaluate the expression

  # and str function convert the result

  # into string

  total = str(eval(expression))

  equation.set(total)
  # initialze the expression variable

  # by empty string

  expression = ""

 # if error is generate then handle

 # by the except block

 except:

  equation.set(" error ")

  expression = ""

# Function to clear the contents

# of text entry box

def clear():

 global expression

 expression = ""

 equation.set("")

# Driver code

if __name__ == "__main__":

 # create a GUI window
 gui = Tk()
 # set the background colour of GUI window

 gui.configure(background="light green")
 # set the title of GUI window

 gui.title("Calculator v1.0")
 # set the configuration of GUI window

 #gui.geometry("265x125")
 gui.geometry("360x180")

 # StringVar() is the variable class

 # we create an instance of this class

 equation = StringVar()
 # create the text entry box for

 # showing the expression .

 expression_field = Entry(gui, textvariable=equation)
 # grid method is used for placing

 # the widgets at respective positions

 # in table like structure .

 expression_field.grid(columnspan=6, ipadx=140)#ipadx=80

 equation.set('enter your expression')

 # create a Buttons and place at a particular

 # location inside the root window .

 # when user press the button, the command or

 # function affiliated to that button is executed .

 button1 = Button(gui, text=' 1 ', fg='black', bg='white',

     command=lambda: press(1), height=2, width=7)

 button1.grid(row=2, column=0)



 button2 = Button(gui, text=' 2 ', fg='black', bg='white',

     command=lambda: press(2), height=2, width=7)

 button2.grid(row=2, column=1)



 button3 = Button(gui, text=' 3 ', fg='black', bg='white',

     command=lambda: press(3), height=2, width=7)

 button3.grid(row=2, column=2)



 button4 = Button(gui, text=' 4 ', fg='black', bg='white',

     command=lambda: press(4), height=2, width=7)

 button4.grid(row=3, column=0)



 button5 = Button(gui, text=' 5 ', fg='black', bg='white',

     command=lambda: press(5), height=2, width=7)

 button5.grid(row=3, column=1)



 button6 = Button(gui, text=' 6 ', fg='black', bg='white',

     command=lambda: press(6), height=2, width=7)

 button6.grid(row=3, column=2)



 button7 = Button(gui, text=' 7 ', fg='black', bg='white',

     command=lambda: press(7), height=2, width=7)

 button7.grid(row=4, column=0)



 button8 = Button(gui, text=' 8 ', fg='black', bg='white',

     command=lambda: press(8), height=2, width=7)

 button8.grid(row=4, column=1)



 button9 = Button(gui, text=' 9 ', fg='black', bg='white',

     command=lambda: press(9), height=2, width=7)

 button9.grid(row=4, column=2)



 button0 = Button(gui, text=' 0 ', fg='black', bg='white',

     command=lambda: press(0), height=2, width=7)

 button0.grid(row=5, column=0)



 plus = Button(gui, text=' + ', fg='black', bg='white',

    command=lambda: press("+"), height=2, width=7)

 plus.grid(row=2, column=3)



 minus = Button(gui, text=' - ', fg='black', bg='white',

    command=lambda: press("-"), height=2, width=7)

 minus.grid(row=3, column=3)



 multiply = Button(gui, text=' * ', fg='black', bg='white',

     command=lambda: press("*"), height=2, width=7)

 multiply.grid(row=4, column=3)



 divide = Button(gui, text=' / ', fg='black', bg='white',

     command=lambda: press("/"), height=2, width=7)

 divide.grid(row=5, column=3)



 equal = Button(gui, text=' = ', fg='black', bg='white',

    command=equalpress, height=2, width=7)

 equal.grid(row=5, column=2)



 clear = Button(gui, text='Clear', fg='black', bg='white',

    command=clear, height=2, width=7)

 clear.grid(row=5, column='1')



 # start the GUI

 gui.mainloop()



led blinking program for pic16f877A

#include <htc.h>
#include

#define _XTAL_FREQ 8000000

void main()
{
  TRISB=0X00;
  PORTB=0X00;
  while(1)
  {
    PORTB=0XFF;
    __delay_ms(1000);
    PORTB=0X00;
    __delay_ms(1000);
  }
}

Thursday, July 4, 2019

print the pascal triangle in c language


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

long factorial(int);
long comb(int,int);

int main()
{
    int i,j,k;
    printf ("Enter number of rows for pascal triangles\n");
    scanf ("%d", &k) ;
    for(i=0;i<k;i++)
    {
        for(j=0;j<=i;j++)
        printf("%5ld",comb(i,j));
        printf("\n") ;
    }
}
long comb(int n,int r)
{

    long c;
    c=factorial(n)/(factorial(r)*factorial(n-r));
    return c;
}
long factorial (int k)
{
    long fact=1;
    while(k>0)
    {
        fact*=k;
        k--;
    }
    return fact;
}


output:
Enter number of rows for pascal triangles
5
    1
    1    1
    1    2    1
    1    3    3    1
    1    4    6    4    1

convert binary octal to decimal in c language

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

int main()
{
    int num,base,result;
    char choice;
    printf("enter b for binary and o for octal\n");\
    scanf("%c",&choice);
    printf("enter the number\n");
    scanf("%d",&num);

  if(choice=='b')
  {
    base=2;

  }
  else
    base=8;
 //result=fun(num,base);
 printf("Decimal number is %d\n",fun(num,base));
    return 0;
}

fun(int n,int base)
{
    int rem,d,j=1,dec=0;
    while(n>0)
    {
        rem=n%10; /* taking last digit */
        d=rem*j;
        dec+=d;
        j*=base;
        n/=10; /* skipping last digit */
    }

return dec;
}

float power Calculation

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


float power (float a, int n);
void main()
{
    float a, p;
    int n;
    printf("Enter a and n\n") ;
    scanf("%f%d",&a,&n) ;
    p=power(a,n) ;
    printf("%f raised. to power %d is %f\n",a,n,p);
}
float power(float a, int n)
{

 if(n==0)
    return(1);
 else
    return(a*power(a,n-1));
}

output::
Enter a and n
5
2

5.000000 raised. to power 2 is 25.000000

static variable in c language

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

void func(void);
void main()
{
    func();
    func();
    func();
}


void func(void){
int a=10;
static int b=10;
printf("a=%d b=%d\n",a,b);
a++;
b++;

}

output::
a=10 b=10
a=10 b=11
a=10 b=12

global variables in c language



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


void funcl(void) ;
void func2(void) ;
int a,b=6;

void main()
{
    printf("Inside main()a=%d b=%d\n",a,b);
funcl();
func2();
}

void funcl(void)
{
   printf("\nInside funcl() a=%d,b=%d\n",a,b);
}

void func2 (void)
{
    int a=8;
    printf("\nInside func2() a=%d, b=%d\n",a,b);

}


output::
Inside main()a=0 b=6

Inside funcl() a=0,b=6

Inside func2() a=8, b=6

write a program to raise floating point number to an integer power in c language


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

void main()
{
    float a;
    int n;
    float power (float a, int n);
    printf("Enter base");
    scanf("%f",&a);
    printf("Enter exponent");
    scanf("%d",&n);
    printf("%f raised to power::%d is %f\n",a,n,power(a,n));
}
float power (float a, int n)
{
    int i;
    float p=1;
    if(n==0)
        return 1;
    else
        for(i=1;i<=abs(n) ;i++)
        p=p*a;
    if (n>0)
        return p;
    else
    return 1/p;
}


output::
Enter base12
Enter exponent2
12.000000 raised to power::2 is 144.000000

palindrome or not....in c language

#include<stdio.h>
int reverse(int n);
main()
{
    int num;
    printf("Enter a number\n");
    scanf("%d",&num) ;
    printf("Reverse of::%d is ::%d\n", num, reverse(num));
    if(num==reverse(num))
        printf("number is palindrome\n");
    else
        printf("Number is not a palindrome\n");
}
int reverse(int n)
{
int rem, rev=0;
    while(n>0)
    {
        rem=n%10;
        rev=rev*10 +rem;
        n/=10;
    }
return rev;
}

no arguments and no return values in c language


#include<stdio.h>

void dispmenu(void);

void main()
{
    int choice;
    dispmenu();
    printf("Enter your choice :");
    scanf("%d",&choice);
}
void dispmenu (void)
{
    printf (" 1. Create database\n");
    printf("2.Insert new record\n");
    printf("3.Modifya record\n");
    //Functions
    printf("4.Delete a record\n");
    printf("5.Display all records\n");
    printf("6.Exit\n");
}

number of characters occarance in a given string in c language

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

int main()
{
   char string[100];
   int c = 0, count[26] = {0}, x;

   printf("Enter a string\n");
   gets(string);

   while (string[c] != '\0') {
   /** Considering characters from 'a' to 'z' only and ignoring others. */

      if (string[c] >= 'a' && string[c] <= 'z') {
         x = string[c] - 'a';
         count[x]++;
      }

      c++;
   }

   for (c = 0; c < 26; c++)
         printf("%c occurs %d times in the string.\n", c + 'a', count[c]);

   return 0;
}

python class topic video