Facebook

adsense

Saturday 28 February 2015

Small but really Interesting Program "INVERT NUMBER"

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

int main()
{
    int a;
    printf("enter a three digit code\n");
               scanf("%d",&a);
    printf("%d\t%d\t%d\n",a%10,a%100/10,a%1000/100);
   
  system("PAUSE");
  return 0;
}

AGE CALCULATOR IN JAVA CODE

import java.text.*;
import java.util.*;
public class CalculateAge
{
public static void main(String args[])
{
Scanner input=new Scanner(System.in);
  System.out.print("Enter your Date of Birth in format MM-dd-yyyy: ");
  String dateOfBirth=input.nextLine() ;
  System.out.print("Enter Current date in format MM-dd-yyyy: ");
  String currentDate=input.nextLine() ;
try {
      Calendar cal1 = new GregorianCalendar();
      Calendar cal2 = new GregorianCalendar();
      int age = 0;
      int factor = 0;
      Date date1 = new SimpleDateFormat("MM-dd-yyyy").parse(dateOfBirth);
      Date date2 = new SimpleDateFormat("MM-dd-yyyy").parse(currentDate);
      cal1.setTime(date1);
      cal2.setTime(date2);
      if(cal2.get(Calendar.DAY_OF_YEAR) < cal1.get(Calendar.DAY_OF_YEAR))
{
            factor = -1;
      }
      age = cal2.get(Calendar.YEAR) - cal1.get(Calendar.YEAR) + factor;
      System.out.println("");
      System.out.println("Your age is: "+age+ " years.");
}
catch (ParseException e)
{
      System.out.println(e);
}
}
}

CALCULATE AREA AND PERIMETER USING C CODE

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

int main(int argc, char *argv[])
{
  int a,b,area,perimeter;

  printf("TO FIND THE AREA AND PERIMETER OF A RECTANGLE\nENTER THE LENGHT AND WIDTH OF A RECTANGLE\n");
  printf("LENTH\t\t");
  scanf("%d",&a);
  printf("WIDTH\t\t");
  scanf("%d",&b);

  area=a*b;
  printf("AREA \t\t=  %d",area);

  perimeter=2*(a+b);
  printf("\nPERIMETER\t=  %d\n\n\n\n",perimeter);
  system("PAUSE");
  return 0;
}

Friday 27 February 2015

PROGRAM SHOWING HOW THREAD AND MOUSEMOTION LISTENER WORK IN JAVA CODE

import java.util.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class MyThread1 implements Runnable{

public void run(){
method();
}

public void method(){
while(true){
try{
Thread.sleep(1000);
System.out.println("Hello to Runnable");
}//end of try
catch(InterruptedException e){

}//end of catch

}//end of while
}//end of method

public static void main(String args[]){

Thread th1=new Thread(new MyThread1());
th1.start();

Thread th2=new Thread(new MyThread2());
th2.start();

}//end of main

}//end of thread1

class MyThread2 extends JPanel implements Runnable,MouseMotionListener{

JFrame frame=new JFrame("Graphis Animations");
int x=0,y=0;

Random ran=new Random();

MyThread2(){

frame.setSize(500,500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(400,400);
addMouseMotionListener(this);
setBackground(new Color(ran.nextInt(255),ran.nextInt(255),ran.nextInt(255)));
frame.add(this);
frame.setVisible(true);


}//end of constructor

public void run(){
while(true){
try{
Thread.sleep(20);

if(x<350){
x++;
y++;
repaint();
}

else
{
y--;
repaint();
}


}//end of try
catch(InterruptedException e){
}

}//end of while

}//end of run



public void paintComponent(Graphics g){

super.paintComponent(g);
g.fillRect(x,y,50,50);
}

public void mouseMoved(MouseEvent e){
x=e.getX()-30;
y=e.getY()-10;

}

public void mouseDragged(MouseEvent e){

}

}

FULL CONCEPT OF POINTER IN C LANGUAGE WITH CODE EXAMPLE

Pointer are the variable that store addresses of other variable as their value.
e.g we declare an ‘int’ type variable and initialize it
                int  x = 12 ;
In memory this ‘x’ must have some addresses where it is stored, that addresses is a stored in pointer variable.
Q ) How pointers are declared ?
A ) Declaration of pointer variable is almost the same as that of other variable, only one change is that we add ‘ * ’ operator before variable name .The type of pointer variable will be same as that of variable whose address is stored in pointer .i.e, if you want to store address of ’ int ‘ type variable than the pointer should also be of  ‘ int ‘ type .
                Int *ptr;    //  declaration of pointer
Q ) How to initialize a pointer ?
A ) As pointer store address only so we cannot initialize it using any number this will create a problem in runtime not compile time, that mean’s it run the program but create problem during execution so we always initialize by giving addresses of some variable using the addresses(&) operator .
                Ptr = &x ;
                This means that pointer ptr store address of x or ptr is pointing toward x.
                Don’t write *ptr during initialize .i will tell why not to write later in this article.
Q ) Working of pointer ?
A ) Different working of pointer , will be discussed one by one.
1)       We can use pointer to print the address of any variable  .
2)      We can print the value of variable whose address is stored in pointer
3)      We can print the address of pointer
This is the program and I use this program to discuss next point, so firstly see this program carefully
You must understand the program as we discuss these thing above
#include<stdio.h>          
int main()
{
        int  a=5;
        int  *ptr;
        ptr = &a;
}

à Print the value using pointer
            The value of a can be print using pointer by simply adding ‘*’
                             e.g ; printf(“THE VALUE OF A %d “ , *ptr);
                               this will print 5 , so value of ‘a’ can be print by two method , directly or by pointer
           As ‘ptr’ is pointing the ‘a’ , so value can be printed using ‘*’.
à print the address of variable using pointer
               The address can be simply print by writing ptr because the ptr contain address of ‘a’ as it value
                               e.g; printf(‘’value of ptr %x’’,ptr); //for address we use the %x
                               this will print the address of ‘a’
àPrint the address of pointer
               The address of pointer can be printed using & operator with the pointer
                               e.g. printf (“address of ptr %x” , &ptr );
                               this will print the pointer’s address
These are the general concept of pointer’s we can use these pointer for different manipulation of program like we can change address because we can access .
Most of the high level language do not support pointer like java. In java pointer are eliminated.
This is the one program for pointer . I will post more program related to pointer later on
Try to solve it’s output
#include<stdio.h>

int main()
{
        int  a=5;
        int  *aptr;
        aptr = &a;
        printf("\nvalue of aptr  =  %p",aptr);
        printf("\naddress of a    =   %p",&a);
        printf("\naddress of aptr  =  %p",&aptr);
        printf("\nvalue of a =  %d",a);
        printf("\n*aptr point to a  =  %d",*aptr);
        printf("\n&*aptr   =  %p",&*aptr);
 return 0;
}


Thursday 26 February 2015

"CHECK WHETHER NUMBER IS PRIME OR NOT" C LANGUAGE CODE

#include<stdio.h>

int main()
{
        int num;
        printf("ENTER THE NUMBER FOR PRIME FACTORS       :        ");scanf("%d",&num);

        prime(num);

}
void prime(int n)
{
        int i;
    for(i=2;i<=n;i++)
            {
            if(n%i==0)
                  {
                    printf("%d,",i);
                     return prime(n/i);
                  }
             }


}

GPA CALCULATOR IN C LANGUAGE


THIS PROGRAM WILL CALCULATE THE GPA FOR 5 SUBJECTS

#include<stdio.h>
#define  SIZE 5
float  gpa(float  n );
char  grade(float  n );
char  sign(float  n );
int main()
{
        int  a[SIZE];
        int sum=0,i,k;
        float  average,mul=0;

        printf("PLEASE  ENTER  THE  NUMBERS  OF  5  SUBJECTS\n");
       for(i=0;i<=SIZE-1;i++)
              scanf("%d",&a[i]);

        printf("NAME   :- \"ADEEL  TALAT\"");printf("\n\nREGISTRATION  NO   :-    \"DDP-SP14-BCS-A-041\"");
        printf("\n\n%s%15s%15s%15s","C.H","MARKS","GRADE","GPA");

       for(k=0;k<=SIZE-1;k++)
       {
           printf("\n3%13d %13c%c%18.1f",a[k],grade(a[k]),sign(a[k]),gpa(a[k]));
          sum+=a[k];
           mul+=3*gpa(a[k]);
       }
       average=sum/SIZE;
       printf("\n\n   TOTAL MARKS   =     %d",sum);
       printf("\n   AVERAGE       =     %.2f",average);
       printf("\n   TOTAL  GPA    =     %.1f",mul/15);
       printf("\n   TOTAL GRADE   =     %.c\n\n\n",grade(average));
return 0;
}

float  gpa(float  n )
{
     if(n>=90)
       {   return 4.0  ;}
       else if(n<90  &&  n>=85)
         {return 3.7 ;}
       else if(n<85  &&  n>=80)
        {return 3.3  ;}
       else if(n<80  &&  n>=75)
        {return 2.0 ;}
       else if(n<75  &&  n>=70)
        {return 2.7  ;}
       else if(n<70  &&  n>=65)
        {return 2.3   ;}
      else if(n<65  &&  n>=60)
        {return 2.0  ;}
      else if(n<60  &&  n>=55)
        {return 1.7   ;}
      else if(n<55  &&  n>=50)
        {return 1.3  ;}
      else if (n<50)
        {return 0.0 ;}

}
char  grade(float  n )
{
     if(n>=90)
       {   return 'A'  ;}
       else if(n<90  &&  n>=85)
         {return 'A' ;}
       else if(n<85  &&  n>=80)
        {return 'B'  ;}
       else if(n<80  &&  n>=75)
        {return 'B' ;}
       else if(n<75  &&  n>=70)
        {return 'B'  ;}
       else if(n<70  &&  n>=65)
        {return 'C'   ;}
      else if(n<65  &&  n>=60)
        {return 'C'  ;}
      else if(n<60  &&  n>=55)
        {return 'C'   ;}
      else if(n<55  &&  n>=50)
        {return 'D' ;}
      else if (n<50)
        {return 'F' ;}

}
char  sign(float  n )
{
     if(n>=90)
       {   return' ';}
       else if(n<90  &&  n>=85)
         {return '-' ;}
       else if(n<85  &&  n>=80)
        {return '+'  ;}
       else if(n<80  &&  n>=75)
        {return' ' ;}
       else if(n<75  &&  n>=70)
        {return '-'  ;}
       else if(n<70  &&  n>=65)
        {return '+'   ;}
      else if(n<65  &&  n>=60)
        {return  ' ' ;}
      else if(n<60  &&  n>=55)
        {return '-'   ;}
      else if(n<55  &&  n>=50)
        {return  ' ';}
      else if (n<50)
        {return  ' ';}


}

Wednesday 25 February 2015

TIC TAC TOE using C language

#include<stdio.h>
void  intro();
int  check(char a[][3],char v);
void select(char a[][3],  int s,char i);
int main()
{
        char  a[3][3],v,ch;
        int s,w,i,j,d,g,h,z,n;
        intro();
        for(i=0;i<=2;i++)
               {
                  for(j=0;j<=2;j++)
                         a[i][j]=' ';
              }
        for(i=1;i<=9;i++)
            {
                           if(i%2!=0)
                                { v='x';   n=1;}
                           else
                                 { v='o';   n=2;}
                printf("\n-------------------------------------------------------------------------");
                printf("\nPLAYER [%d] TURN  :  ",n);scanf("%d",&s);
                select(a,s,v);
                prt(a,3);
                z=check(a,v);
               if(z==30)  break;
        }
        if(z!=30)
           printf("GAME DRAW");
system("PAUSE");
return 0;
}
void  intro()
{
        printf("\t\t\t@@@@@@@@@@@@@@@@@@@@@@");
        printf("\n\t\t\t@@@ TIC- TAC - TOE @@@ ");
         printf("\n\t\t\t@@@@@@@@@@@@@@@@@@@@@@");
        printf("\n\n  1  |  2  |  3  \n");
        printf("-------------------\t\tPLAYER 1  -->  [X] \n");
        printf("  4  |  5  |  6  \n");
        printf("-------------------\t\tPLAYER 2  -->  [O] \n");
        printf("  7  |  8  |  9  \n");
       printf("\n\nENTER THE BLOCK NUMBER:-");
}
void select(char a[][3],  int s,char i)
{
    switch(s)
    {
            case 1 :
                        a[0][0]=i;
                        break;
            case 2:
                        a[0][1]=i;
                        break;
            case 3 :
                        a[0][2]=i;
                        break;
            case 4 :
                        a[1][0]=i;
                        break;
            case 5:
                         a[1][1]=i;
                         break;
            case 6 :
                         a[1][2]=i;
                         break;
            case 7 :
                         a[2][0]=i;
                         break;
            case 8:
                         a[2][1]=i;
                         break;
            case 9 :
                        a[2][2]=i;
                        break;
    }
}
void prt(char a[][3], int s)
{
    int i,j;
    for(i=1;i<=s;i++)
    {
        printf("\t\t\t");
               for(j=1;j<=3;j++)
                  {
                     printf("  %c  ",a[i-1][j-1]);
                    if(j/3!=1)
                        printf("|");
                   }
               if(i/3!=1)
                   printf("\n\t\t\t ---------------\n");
     }
}
int  check(char a[][3],char v)
{
    int g,s,n,m,w;
     for(g=0;g<=2;g++)
     {
         if(v=='x')  w=1;
         if(v=='o')  w=2;
                 if(a[g][0]==v  &&  a[g][1]==v  && a[g][2]==v)
                       {
                          printf("\n\t        *********************************");
                         printf("\n\t\t    CONGRATES PLAYER  (%d)  WIN ",w);
                         printf("\n\t        *********************************\n");
                          return 30;
                        }
                 else  if(a[0][g]==v    &&   a[1][g]==v    &&     a[2][g]==v)
                       {
                          printf("\n\t        *********************************");
                         printf("\n\t\t    CONGRATES PLAYER  (%d)  WIN ",w);
                         printf("\n\t        *********************************\n");
                          return 30;
                        }
                  else  if(a[0][0]==v    &&   a[1][1]==v    &&     a[2][2]==v   ||  a[0][2]==v    &&   a[1][1]==v    &&     a[2][0]==v)
                        {
                          printf("\n\t        *********************************");
                         printf("\n\t\t    CONGRATES PLAYER  (%d)  WIN ",w);
                         printf("\n\t        *********************************\n");
                          return 30;
                        }
    }

}

Tuesday 24 February 2015

By time to time, I will post the data and information related to programming
Stay tuned with the blog
WELCOME to the programmer place