New Android apps UniqueKey

Friday 15 May 2015

Prime numbers between 2 numbers


#include<stdio.h>
#include<math.h>

main()
{
 int n1,n2,i,j,flag;
 
 printf("Enter 2 numbers\n");
 scanf("%d %d",&n1,&n2);
 
 for(i=n1+1 ; i<n2 ; i++)
 {
  flag = 0;
  
  for(j=2 ; j<=sqrt(i) ; j++)
  {
   if(i%j==0){
    flag=1;
    break;
   }
  }
  if(flag==0)
  printf("%d\t",i);
 }
}

Tuesday 12 May 2015

Class Program in C++

This is a C++ program to illustrate the class and its use in C++ programming language. Class is a main feature in object oriented programming language like C++.

#include<iostream>
#include<string.h>
using namespace std;

class employee
{
  char name[50];
  int number;
  float basic_pay,allowance,it,gross,net;
  public:
    void read(char e_name[],int e_number,float e_basic_pay);
    void calculate(void);
    void display(void);
};

void employee::read(char e_name[],int e_number,float e_basic_pay)
{
  strcpy(name,e_name);
  number=e_number;
  basic_pay=e_basic_pay;
  
}

void employee::calculate(void)
{
  allowance=(123*basic_pay)/100;
  gross=basic_pay+allowance;
  it=(30*gross)/100;
  net=basic_pay+allowance-it;
}

void employee::display(void)
{
  cout<<"Name: "<<name<<endl;
  cout<<"Number: "<<number<<endl;
  cout<<"Basic pay: "<<basic_pay<<endl;
  cout<<"Allowance: "<<allowance<<endl;
  cout<<"Income tax: "<<it<<endl;
  cout<<"Net Salary: "<<net<<endl;
}

main()
{
  employee e1;
  char e_name[50];
  int e_number;
  float e_basic_pay;
  cout<<"Name: ";
  cin>>e_name;
  cout<<"Employee number: ";
  cin>>e_number;
  cout<<"Basic Pay: ";
  cin>>e_basic_pay;
  e1.read(e_name,e_number,e_basic_pay);
  e1.calculate();
  e1.display();
}

Double Linked List Program

This is a program to show how double linked list works.

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
 
struct node
{
     struct node *previous;
     int data;
     struct node *next;
}*head, *last; 

void insert_begning(int value);
int delete_from_end();
void display();

int main()
{
    int value, i, loc;
    head=NULL;
    printf("Select the choice of operation on link list");
    printf("\n1.) insert at begning\n2.)delete from end \n3.) display\n4.exit\n");

    while(1)
    {
          printf("\n\nenter the choice\n");
          scanf("%d",&i);
          switch(i)
          {
            case 1:
                {
                 printf("enter the value you want to insert in node ");
                 scanf("%d",&value);
                 insert_begning(value);
                 display();
                 break;
                }
            case 2:
                   delete_from_end();
                   display();
                   break;
            case 3 :
                   display();
                   break;
            case 4 :
                 {
                      exit(0);
                      break;
                 }
          }
    }
    printf("\n\n%d",last->data);
    display();
    getch();
}

void insert_begning(int value)
{
    struct node *var,*temp;
     var=(struct node *)malloc(sizeof(struct node));
     var->data=value;
     if(head==NULL)
     {
         head=var;
         head->previous=NULL;
         head->next=NULL;
         last=head;
     }
     else
     {
         temp=var;
         temp->previous=NULL;
         temp->next=head;
         head->previous=temp;
         head=temp;
     }
} 

int delete_from_end()
{
      struct node *temp;
      temp=last;
      if(temp->previous==NULL)
      {
           free(temp);
           head=NULL;
           last=NULL;
           return 0;
      }
      printf("\nData deleted from list is %d \n",last->data);
      last=temp->previous;
      last->next=NULL;
      free(temp);
      return 0;
} 

void display()
{
     struct node *temp;
     temp=head;
     if(temp==NULL)
      {
         printf("List is Empty");
      }
     while(temp!=NULL)
     {
          printf("-> %d ",temp->data);
          temp=temp->next;
     }
} 

Android Tutorial : Simple Program

Introducing a simple basic android app. by this program you will understand a basic structure of android app development. 
xml file(activity_main.xml) for layout design is given, and then you have to connect this xml layout file with java file for functionality of this xml layout. 
manifest file is very important in android development, all the activities should be defined in manifest file otherwise app with not run. 

activity_main.xml  File:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="176dp"
        android:text="Click button" />

</RelativeLayout>




MainActivity.java  File:

package com.example.sample;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends ActionBarActivity {

 Button b;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  
  b = (Button)findViewById(R.id.button1);
  b.setOnClickListener(new View.OnClickListener() {
   
   @Override
   public void onClick(View v) {
    // TODO Auto-generated method stub
   Toast.makeText(getApplicationContext(), "Button is pressed", Toast.LENGTH_LONG).show();
   }
  });
 }

 @Override
 public boolean onCreateOptionsMenu(Menu menu) {
  // Inflate the menu; this adds items to the action bar if it is present.
  getMenuInflater().inflate(R.menu.main, menu);
  return true;
 }

 @Override
 public boolean onOptionsItemSelected(MenuItem item) {
  // Handle action bar item clicks here. The action bar will
  // automatically handle clicks on the Home/Up button, so long
  // as you specify a parent activity in AndroidManifest.xml.
  int id = item.getItemId();
  if (id == R.id.action_settings) {
   return true;
  }
  return super.onOptionsItemSelected(item);
 }
}

Manifest  File:


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.sample"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="21" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

VB Script : Input Box

Write this program in notepad and save it as _filename.vbs 
name=inputbox("Whats your name?")

msgbox("Have a good day, ") + name

VB Script : Fun Program

This is a fun VB Script program. if someone run this program in his pc then it will never end until you finish it from task manager in pc. this is for simple prank. have fun..


Write this program in notepad and save it as _filename.vbs 

DO
msgbox("manoj")
LOOP

VB Script : Simple Program



Write this program in notepad and save it as _filename.vbs 
pass=inputbox("password")

if pass="manoj" then msgbox("correct password")
 else msgbox("password is not correct  try again")

Clock using Notepad


@echo off
:start
echo date:%date% time:%time%
goto start

Wednesday 6 May 2015

FIFO, SJF, Priority Scheduling, Round Robin Scheduling Algorithms

Scheduling algorithms are very important concept in operating system to decide which process has to allocate CPU next. there are many types of Scheduling algorithms e.g. FIFO (first in first out), SJF (Shortest job first), Priority scheduling, RR (round robin), Multilevel queue, Multilevel feedback queue. 
following program describes FIFO, SJF, RR and Priority scheduling algorithms.

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

struct templet
{
    char name[8];
    int at,st,pt,ft,tat,wt,priority,remain,flag;
}p[20],temp;


int n,Bu[20],Twt,Ttt,A[10],Wt[10],w,wait[50],turn[50];
float Awt,Att;
char pname[20][20],c[20][20];

void Getdata();
void Gantt_chart();
void Calculate();
void chart();
void fcfs();
void sjf();
void rr();
void prio();
//srand(time(NULL));
int main()
{
    int ch;
    do{
    printf("\n1.FCFS\n2.SJF\n3.Round Robin\n4.Priority\n5.exit");
    printf("\nEnter choice : ");
    scanf("%d",&ch);
    switch(ch)
    {
        case 1: Getdata();
          fcfs();
                break;
        case 2:sjf();
                break;
        case 3: rr();
                break;
        case 4: prio();
                break;
    }}while(ch!=5);
}
void fcfs()
{
 int i,j,temp, temp1;
 Twt=0;
 Ttt=0;
 printf("\n\n FIRST COME FIRST SERVED ALGORITHM\n");
 for(i=1;i<=n;i++)
 {
  for(j=i+1;j<=n;j++)
  {
   if(A[i]>A[j])
   {
    temp=Bu[i];
    temp1=A[i];
    Bu[i]=Bu[j];
    A[i]=A[j];
    Bu[j]=temp;
    A[j]=temp1;
    strcpy(c[i],pname[i]);
    strcpy(pname[i],pname[j]);
    strcpy(pname[j],c[i]);
   }
  }
 }
 Calculate();
 Gantt_chart();
 chart();
}

void sjf()
{
 srand(time(NULL));
    int no,i,j,fcnt=0,scnt=0;
    printf("\nEnter no of processes : ");
    scanf("%d",&no);
    for(i=0;i<no;i++)
    {
        printf("\n\nEnter process name : ");
        scanf("%s",p[i].name);
        p[i].at=rand()%50;
  printf("\nArrival time is %d ",p[i].at);
        p[i].pt=rand()%50;
        printf("\nEnter Burst time is %d ",p[i].pt);
        
        p[i].remain=p[i].pt;
        p[i].flag=0;
    }
    for(i=0;i<no;i++)
    {
        for(j=0;j<no;j++)
        {
            if(p[i].at<p[j].at)
            {
                temp=p[i];
                p[i]=p[j];
                p[j]=temp;
            }
        }
    }
    printf("\n\t Grant Chart : \n\t\t");
   
    i=0;
    scnt=p[i].at;
    while(1)
    {
        if(p[i].flag==0)
        {
            printf("%s--->",p[i].name);
            p[i].remain--;
            scnt++;
            if(p[i].remain>0)
            {
                for(j=0;j<no;j++)
                {
                    if(p[j].at<=scnt && p[j].flag==0)
                    {
                        if(p[i].remain>p[j].remain)
                        {
                            i=j;
                            printf("- ");
                        }
                    }
                }
            }
            else
            {
                p[i].wt=scnt-p[i].pt-p[i].at;
                p[i].tat=scnt-p[i].at;
                p[i].flag=1;
                fcnt++;
                printf("+");
                if(fcnt==no)
                    break;
                if(i<no-1)
                    i++;
                else
                    i=0;
                for(j=i;j>0;j--)
                        if(p[j].remain < p[i].remain && p[j].flag==0)
                            i=j;
            }
        }
        else
            i++;
    }
   
    printf(" Finish\n");
    fcnt=0,scnt=0;
    printf("\n\tProcess \t burst time \t waiting time \t turn around time");
    for(i=0;i<no;i++)
    {
        printf("\n\t%s\t\t\t%d\t\t%d\t \t%d",p[i].name,p[i].pt,p[i].wt,p[i].tat);
        fcnt+=p[i].wt;
        scnt+=p[i].tat;
    }
    printf("\n-------------------------------------------------------------------");
    printf("\n\n\t\t\t\tAverage : \t %d\t   \t%d",fcnt/no,scnt/no);
}

void rr()
{
 srand(time(NULL));
    int no,i,j,fcnt=0,scnt=0,interval;
    printf("\nEnter no of processes : ");
    scanf("%d",&no);
    for(i=0;i<no;i++)
    {
        printf("\nEnter process name : ");
        scanf("%s",p[i].name);
        //p[i].pt=rand()%50;
  printf("\nenter Burst time : ");
        scanf("%d",&p[i].pt);
        p[i].remain=p[i].pt;
        p[i].flag=0;
    }
    //interval=rand()%10;
 printf("\nenter time quantum : ");
 scanf("%d",&interval);
    
   
    printf("\n\t Grant Chart : \n\t\t");
   
    i=0;
    while(1)
    {
        for(i=0;i<no;i++)
        {
        if(p[i].flag==0)
        {
            if(p[i].remain<=interval)
            {
                printf("-- %s (C)--",p[i].name);
                scnt+=p[i].remain;
                p[i].wt=scnt-p[i].pt;
                p[i].tat=scnt;
                p[i].flag=1;
                fcnt++;
            }
            else
            {
                printf("-- %s --",p[i].name);
                p[i].remain-=interval;
                scnt+=interval;
            }
        }
        }
        if(fcnt==no)
            break;
    }
   
    printf("Finish");
    fcnt=0,scnt=0;
    printf("\n\tProcess \t burst time \t waiting time \t turn around time");
    for(i=0;i<no;i++)
    {
        printf("\n\t%s\t\t\t%d\t\t%d\t \t%d",p[i].name,p[i].pt,p[i].wt,p[i].tat);
        fcnt+=p[i].wt;
        scnt+=p[i].tat;
    }
    printf("\n-----------------------------------------------------------------\n");
    printf("\n\n\t\t\t\tAverage : \t %d\t   \t%d",fcnt/no,scnt/no);
}

void prio()
{
 srand(time(NULL));
    int no,i,j,fcnt=0,scnt=0;
    printf("\nEnter no of processes : ");
    scanf("%d",&no);
    for(i=0;i<no;i++)
    {
        printf("\nEnter process name : ");
        scanf("%s",p[i].name);
        p[i].pt=rand()%50;
  printf("\nBurst time  is %d ",p[i].pt);
        p[i].priority=rand()%10;
        printf("\npriority is %d ",p[i].priority);
        
    }
    for(i=0;i<no;i++)
    {
        for(j=0;j<no;j++)
        {
            if(p[i].priority<p[j].priority)
            {
                temp=p[i];
                p[i]=p[j];
                p[j]=temp;
            }
        }
    }
    printf("\n\t Grant Chart : \n\t\t");
    for(i=0;i<no;i++)
    {
        p[i].st=fcnt;
        fcnt+=p[i].pt;
        p[i].wt=fcnt-p[i].pt;
        p[i].tat=fcnt;
        printf("%s -->",p[i].name);
    }
    printf("Finish");
    fcnt=0;
    printf("\n\tProcess \t burst time \t waiting time \t turn around time");
    for(i=0;i<no;i++)
    {
        printf("\n\t%s\t\t\t%d\t\t%d\t \t%d",p[i].name,p[i].pt,p[i].wt,p[i].tat);
        fcnt+=p[i].wt;
        scnt+=p[i].tat;
    }
    printf("\n\n\t\t\t\tAverage : \t %d\t   \t%d",fcnt/no,scnt/no);
}
void Getdata()
{
 srand(time(0));
 int i;
 printf("\n Enter the number of processes: ");
 scanf("%d",&n);
 for(i=1;i<=n;i++)
 {
  fflush(stdin);
  printf("\n Enter the process name: ");
  scanf("%s",&pname[i]);
  Bu[i] = rand()%50;
  printf(" burst time for process %s is %d \n",pname[i],Bu[i]);
  A[i] = rand()%50;
  printf(" Arrival time for process %s is %d \n",pname[i],A[i]);
 }
}

void Gantt_chart()
{
 int i;
 printf("\n\n\t\t\tGANTT CHART\n");
 printf("\n-----------------------------------------------------------\n");
 for(i=1;i<=n;i++)
  printf("|\t%s\t",pname[i]);
 printf("|\t\n");
 printf("\n-----------------------------------------------------------\n");
 printf("\n");
 for(i=1;i<=n;i++)
 printf("%d\t\t",Wt[i]);
 printf("%d",Wt[n]+Bu[n]);
 //turn[i]=Wt[n]+Bu[n];
 printf("\n-----------------------------------------------------------\n");
 printf("\n");
 }

void Calculate()
{
 int i;
 Wt[1]=0;
 wait[1]=0;
 turn[1]=Bu[1];
 for(i=2;i<=n;i++)
 {
  Wt[i]=Bu[i-1]+Wt[i-1];
  wait[i]=Wt[i];
  turn[i]=Wt[i]+Bu[i];
 }
 for(i=1;i<=n;i++)
 {
  Twt=Twt+(Wt[i]-A[i]);
  Ttt=Ttt+((Wt[i]+Bu[i])-A[i]);
 }
 Att=(float)Ttt/n;
 Awt=(float)Twt/n;
 printf("\n\n Average Turn around time=%3.2f ms ",Att); 
 printf("\n\n AverageWaiting Time=%3.2f ms",Awt);
}

void chart(){
 int k;
 printf("name\t burst time\t Arrival time \t Waiting time  turnaround time\n");
 for (k=1;k<=n;k++)
 {
  printf("%s\t   %d\t\t %d\t\t\t %d\t\t%d\n",pname[k],Bu[k],A[k],wait[k],turn[k]);
 }
}

Saturday 21 March 2015

Simple Linked list Program

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

struct node
{
 int data;
 struct node *link;                 //to creating self reference pointer
};

int display(struct node *,int);
void insertion(struct node *,int);
void deletion(struct node *,int);

struct node *head;
int count=0;

main() 
{
   int i,choice,inst=0,del,chk1=1,chk=1,num;
  
   do
   {
   printf("\nMENU:\n\n 1.Insertion\n 2.Deletion \n 3.Display\n 4.Exit\n\nenter your choice :     ");
   scanf("%d",&choice);
   
   switch(choice)
   {
  case 1:
          if(count==0)
          { 
           printf("\nThis is First Node\n"); 
          head=(struct node *)malloc(sizeof(struct node));
           insertion(head,inst);
          }
         else
         {
           printf("Where do you want to insert data\n 1. In starting \n2. End\n");
           scanf("%d",&inst);
           insertion(head,inst);
         }   
         break;
         
  case 2:
          printf("from where do you want to delete\n 1. In starting 2. End\n");
          scanf("%d",&del);
          deletion(head,del);
          break;
          
  case 3:
         chk1=display(head,count);
         if(chk1==1)
         printf("NULL\n");
         break;
         
  case 4:
          chk=0;
          break;
     
    default : printf("invalid option selected\ntry again\n\n");        
   }
 }
 while(choice!=4);
  }
  
void insertion(struct node *list,int num)
{ 
  struct node *new1;
  int num1;
  
  printf("Enter the number\n");
  scanf("%d",&num1);
  
  if(count==0&&num==0)
  {
    head->data=num1;
 head->link=NULL;
  }
  else if(num==1&&count>0)
  {
 new1=(struct node *)malloc(sizeof(struct node));
 new1->data=num1;
 new1->link=head;
 head=new1;
  }
  else if(num==2&&count>0)
  {
 for( ; ;)
 {
   if(list->link!=NULL)
      list=list->link;
   else
  {
    new1=(struct node *)malloc(sizeof(struct node));
    new1->data=num1;
    list->link=new1;
    new1->link=NULL;
    break;
  }
 }
  }
  
 count++;
 
}

void deletion(struct node *list,int num)
{
  struct node *temp;
  
  if(num==1&&count>0)
 {
   head=list->link;
   free(list);
 }
  else if(num==2&&count>1)
 {
   for( ; ;)
    {
  if(list->link->link!=NULL)
     list=list->link;
  else
   {
     free(list->link);
     list->link=NULL;
   break;
    }
   }
 }
 else if(count==1)
   free(head);
 else if(count<=0)
 {
    printf("List is empty\n\n"); 
    count--; 
 }
 count--;
 
}

int display(struct node *list,int num)
{ 
  int chk=1;
  
  if(num>0)
   {
  printf("%d->",list->data);
  num--;
  display(list->link,num);
   }
  else if(count<=0)
 {
   chk=0;
   printf("list is empty\n\n");
 }
 return(chk);
}

Inline Function

Inline function: whenever we use inline function in our program, and whenever compiler executes inline function, each time it create a copy of function code and execute the functions.
for use inline function you just have to write "inline" in front of function name.  

#include<iostream>
using namespace std;

inline float area(float radius)
{
  return 3.14*radius*radius;
}

inline float peri(float radius)
{
  return 2*3.14*radius;
}

inline float simple(float amount,float interest,int year)
{
  return amount*(interest/100)*year;
}

main()
{
  int year;
  float amount,radius,interest;
  cout << "Enter radius: ";
  cin >> radius;
  cout << "Area : " << area(radius)<<endl;
  cout << "Perimeter : " << peri(radius)<<endl;
  cout << "Simple Interest Calculator" << endl;
  cout << "Amount: "<<endl;
  cin >> amount;
  cout << "Interest (in %): ";
  cin >> interest;
  cout << "Year: ";
  cin >> year;
  cout << "Simple interest: "<< simple(amount,interest,year)<<endl;
}