Tuesday, 30 September 2014

WAP to Reverse String with the use of Pointer

#include<conio.h>
#include<stdio.h>
void main()
{
clrscr();
char a[100],b[100];
char *str=a;
char *rev=b;
int i=-1;
printf("\n\t Enter String : ");
scanf("%s",&a);
while(*str)
{        *str++;
i++;
}
while(i>=0)
{
*str--;
*rev=*str;
*rev++;
i--;
}
*rev!='\0';
printf("\n\tstring in Reverse :  %s  ",b);
getch();
}

WAP to add two matrix

#include<conio.h>
#include<stdio.h>
void main()
{
clrscr();
int a[10][10],b[10][10],c[10][10];
int i,j,x,n;
printf("\t Enter the no. of row in Matrix ");
scanf("%d",&x);
printf("\n\t Enter the no. of column in Matrix ");
scanf("%d",&n);
printf("\n\t Enter the Matrix of A : ");
for(i=0;i<x;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("\n\tEnter the Matrix of B : ");
for(i=0;i<x;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&b[i][j]);
}
}
for(i=0;i<x;i++){
for(j=0;j<n;j++) {
c[i][j]= a[i][j]+b[i][j];
}
}
printf("\n\t Addition is :  ");
for(i=0;i<x;i++)
{
for(j=0;j<n;j++)
{
printf("%d  ",c[i][j]);
}
printf("\n\t\t\t");
}
getch();
}

Monday, 29 September 2014

WAP to use Invert Function

#include<conio.h>
#include<stdio.h>
unsigned invert(unsigned, int ,int);
void main()
{
clrscr();
unsigned a;
int b,c;
printf("\n\t Enter no. value of Unsigned , int and int ");
scanf("%d%d%d",&a,&b,&c);
printf("\n\tThe result : %d",invert(a,b,c));
getch();
}
unsigned invert(unsigned x,int p,int n)
{
return x^(~(~0<<n)<<p+1-n);

}

Create Notepad in C

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

class NotePad
{
 private:
  int x;
  int y;
  int key;
  int total_chars;
  int total_words;
  int total_sentance;
  int line[100];
 public:
  void clipboard()
  {
   x=1,y=1,total_words=0,total_chars=0;
   int line_index=0;

   while(key!=27)
   {
    gotoxy(1,50);cout<<"Col: "<<x<<" ";
    gotoxy(11,50);cout<<"Rows: "<<y;
    gotoxy(21,50);cout<<"Total Chars: "<<total_chars;
    gotoxy(40,50);cout<<"Total Words: "<<total_words;
    total_chars++;
    gotoxy(x,y);
    key=getch();
    gotoxy(x,y);printf("%c",key);
    if(key==13)
    {
     y++;
     line[y]=x;
     x=1;
    }
    else if(key==32)
    {
     total_words++;
    }
    else if(key==8)
    {
     x=line[y]-1;
     gotoxy(x,y);cout<<" ";
    }
    else
    {
     x++;
    }
   }
  }
};


void main(void)
{
 clrscr();
 NotePad np;
 np.clipboard();
 getch();
}

Sunday, 21 September 2014

WAP to print Fibonic series.

#include<conio.h>
#include<stdio.h>
void main()
{
clrscr();
int a,i,k,x;
printf("\n\tEnter the no. to print fibonic series");
scanf("%d",&a);
k=0;
printf("\n\t Fibonic series : 0, 1,");
for(i=1;i<a;)
{
x=k+i;
k=i;
i=x;
if(i<=a){
printf(" %d,",i);
}
}
getch();
}

Saturday, 20 September 2014

WAP to display colorfull watch with running state.

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
#include<dos.h>

void arrow(float a,float b,float c,float d,float e)
{
  float l=44.0/(60.0*7);
  for(;l>0;l-=0.001)
  {
   line(a,b,a+85*cos(e+l),b+85*sin(e+l));
   line(a,b,a+85*cos(e-l),b+85*sin(e-l));
   line(a+85*cos(e+l),b+85*sin(e+l),c,d);
   line(a+85*cos(e-l),b+85*sin(e-l),c,d);
  }
}

void main()
{
  struct time t;
  float a,c,d,e,f,g,h,i,j;
  int gdriver = DETECT, gmode, errorcode;
  initgraph(&gdriver, &gmode, "C:\\turboc3\\bgi");
  setcolor(23);
  gettime(&t);
  c=44.0/7;
  d=c/60;
  e=d/60;
  f=e/12;
  g=((c*t.ti_min/60.0)+(c*t.ti_sec/3600))-11.0/7.0;
  if(t.ti_hour>12)
  h=((c*(t.ti_hour-12.0)/12.0)+(c*t.ti_min/(12.0*60.0)))-11.0/7.0;
  else
  h=((c*t.ti_hour/12.0)+(c*t.ti_min/(60.0*12.0)))-11.0/7.0;
  i=getmaxx()/2;
  j=getmaxy()/2;
  setcolor(5);  a=140;
  setfillstyle(1,5);
  fillellipse(i,j,a,a);
  setcolor(23);
  for (a=0;a<=c;a+=c/12)
  {
   outtextxy(getmaxx()/2+120*cos(a),getmaxy()/2+120*sin(a),"o");
  }
  setcolor(7);
  for(a=140;a<=160;a+=5)
  circle(i,j,a);
  a=(c*t.ti_sec/60.0)-(11.0/7.0);
  for(;;)
  {
    setcolor(5);
    arrow(i,j,i+100*cos(a),j+100*sin(a),a);
    setcolor(1);
    arrow(i,j,i+100*cos(a+d),j+100*sin(a+d),a+d);
    setcolor(5);
    arrow(i/*+30*cos(g)*/,j/*+30*sin(g)*/,i+100*cos(g),j+100*sin(g),g);
    setcolor(4);
    arrow(i/*+30*cos(g+e)*/,j/*+30*sin(g+e)*/,i+100*cos(g+e),j+100*sin(g+e),g+e);
    setcolor(5);
    arrow(i/*+50*cos(h)*/,j/*+50*sin(h)*/,i+100*cos(h),j+100*sin(h),h);
    setcolor(3);
    arrow(i/*+50*cos(h+f)*/,j/*+50*sin(h+f)*/,i+100*cos(h+f),j+100*sin(h+f),h+f);
    delay(1000);
    g=g+e;
    h=h+f;
    a+=d;
    if(kbhit()){
    break;
  }
 }
  getch();
}

WAP to display hello world

#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
char c;
printf("\n\t Hello world ");
getch();
}

WAP to display ASCII value

#include<conio.h>
#include<stdio.h>
void main()
{
clrscr();
char c;
printf("\t Enter a character");
scanf("%c",&c);
printf("\n \t ASCII value is : %d",c);
getch();
}

WAP to Bubble Short

#include <stdio.h>

int main()
{
  int array[100], n, c, d, swap;

  printf("Enter number of elements\n");
  scanf("%d", &n);

  printf("Enter %d integers\n", n);

  for (c = 0; c < n; c++)
    scanf("%d", &array[c]);

  for (c = 0 ; c < ( n - 1 ); c++)
  {
    for (d = 0 ; d < n - c - 1; d++)
    {
      if (array[d] > array[d+1]) /* For decreasing order use < */
      {
        swap       = array[d];
        array[d]   = array[d+1];
        array[d+1] = swap;
      }
    }
  }

  printf("Sorted list in ascending order:\n");

  for ( c = 0 ; c < n ; c++ )
     printf("%d\n", array[c]);

  return 0;
}

WAP to show Even ,Odd ,average of enven and average of odd no. using Switch case and for loop with array

#include<conio.h>
#include<stdio.h>
void avgOfEvenNumbreInArray(int [], int);
void avgOfOddNumberInArray(int [], int);

void main()
{
clrscr();

int i, j, evenCount, oddCount, evenSum, oddSum, n;
char ch;
int b[10];

//iniitalize variable with 0
evenCount = 0;
oddCount = 0;
evenSum = 0;
oddSum = 0;
i = 0;
n = 0;
ch = ' ';
printf("Enter the no: ");
//check for two condition either user quit in between or array is full.
while(i < 10)
{
scanf("%d", &b[i]);
i++; //increment the value;
}
//i contain the total element in array, i < 20 in every case.
printf("\nEnter your choise: ");
printf("\nPress 1 Even number in array . : ");
printf("\nPress 2 Odd number in array . : ");
printf("\nPress 3 Average of even no. : ");
printf("\nPress 4 Average of Odd no. : ");
printf("\nChoice : ");
scanf("%d",&n);

switch( n )
{
case 1 :
printf("\n Even no. :  ");
for(j = 0; j < i; j++)
{
if(b[j] % 2 == 0)
{
printf("\t%d", b[j]);
}
}
break;
case 2:
printf("\n Even no. :  ");
for(j = 0; j < i; j++)
{
if(b[j] % 2 != 0)
{
printf("\t%d", b[j]);
}
}
break;
case 3:
avgOfEvenNumbreInArray(b, i);
break;
case 4:
avgOfOddNumberInArray(b, i);
break;
default:
printf("\nInvalid choice");
}
getch();
}


void avgOfEvenNumbreInArray(int a[], int length) {
int i, totalEven = 0;
int sum = 0;
int avg = 0;
for(i = 0; i < length; i++) {
if(a[i] % 2 == 0) {
sum +=  a[i];
totalEven ++;
}
}
printf("\n Sum of even number = %d", sum);
printf("\n Total even number in array = %d", totalEven);
printf("\n Avg = %d", (sum / totalEven));
}

void avgOfOddNumberInArray(int a[], int length) {
int i, totalOdd = 0;
int sum = 0;
int avg = 0;
for(i = 0; i < length; i++) {
if(a[i] % 2 == 0) {
sum +=  a[i];
totalOdd ++;
}
}
printf("\n Sum of odd number = %d", sum);
printf("\n Total odd number in array = %d", totalOdd);
printf("\n Avg = %d", (sum / totalOdd));
}

Friday, 19 September 2014

WAP to use Switch case.

#include<stdio.h>
#include<conio.h>
main()
{
   clrscr();
   int a;
   scanf("%d",&a);
   switch( a )
     {
case 1 : printf( "Excellent\n" );
  break;
case 2 : printf( "Good\n" );
  break;
case 3 : printf( "OK\n" );
  break;
case 4 : printf( "Mmmmm....\n" );
  break;
case 5 : printf( "You must do better than this\n" );
  break;
default  : printf( "What is your grade anyway?\n" );
  break;
     }
}

WAP to Reverse a given integer number

#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int s,a,b,x;
printf("\n  Enter the no. to reverse ");
scanf("%d",&a);
s=0;
b=a;
while(b>0)
{
x=b%10;
s=s*10+x;
b=int(b/10);
}
printf("\n  Reverse : %d ",s);
getch();
}

WAP to check the no. is Armstrong or not.

#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int a,b,k,x;
printf("Enter the no. to find the no. is armstrong or not : ");
scanf("%d",&a);
b=a;
int m=0;
while(b>0)
{
k=b%10;
m=m+k*k*k;
b=b/10;

}
if(m==a)
{
printf("%d is Armstrong no.",a);

}       else
{
printf("%d is not Armstrong no.",a);
}
getch();
}

WAP to find Simple interest and Compound Interest

#include<conio.h>
#include<stdio.h>
#include<math.h>
void main()
{
int p,t,n,si,ci;
float r;
printf("Simple Interest ")  ;
printf("\nEnter Principal, Rate and Time ");
scanf("%d%f%d",&p,&r,&t);
si=(p*r*t)/100;
printf("\nSimple interest is : %d",si);
printf("\nCompound Interest");
printf("\nEnter principal,rate,time and CI per yr ");
scanf("%d%d%d%d",&p,&r,&t,&n);
ci=p*pow((1+r/n),(n*t));
printf("\nCompound Interest is : %d",ci);
getch();
}

WAP to find the root of a quadratic equation.

#include<conio.h>
#include<stdio.h>
#include<math.h>
void main()
{
clrscr();
float a,b,c,d,x1,x2;
printf("  Enter the cofficient of a,b, c ");
scanf("%f %f %f",&a,&b,&c);
d=b*b-4*a*c;
if(d>0)
{
x1=(-b-sqrt(d))/(2*a);
x2=(-b+sqrt(d))/(2*a);
printf("\n  Root are Real and Unequal : %f , %f",x1,x2);
}
else
if(d==0)
{
x1=-b/(2*a);
printf("\n  Root are Real and equal : %f",x1);
}
else
printf("  No real root ,Root are complex");
getch();
}

WAP to check whether given two no. are amicable or not.

#include<conio.h>
#include<stdio.h>
void main()
{
clrscr();
int a,b,x,k;
k=0;
x=0;
printf("Enter the first no. for Amplicable or not : ");
scanf("%d",&a);
printf("\nEnter the second no.");
scanf("%d",&b);
for(int i=1;i<=a/2;i++)
{
if(a%i==0)
{
k+=i;
}
}
for(int j=1;j<=b/2;j++)
{
if(b%j ==0)
{
x+=j;
}
}
if(k==b && x==a)
{
printf("\n (%d,%d) is an Amplicable no.",a,b);
}
else
{
printf("\n (%d,%d) is an not Amplicable no.",a,b);
}
getch();
}

WAP to check whether the given no. is perfet or not.Hint 6=1+2+3 divisor.

#include<conio.h>
#include<stdio.h>
void main()
{
clrscr();
int a,b,x;
char ch;
do
{
x=0;
printf("\n  Enter the no. to check perfect no. or not : ");
scanf("%d",&a);
b=a/2;
for(int i=1;i<=b;i++)
{
if(a%i==0)
{
x=x+i;
}
}
if(x==a)
{
printf("\n  %d is a Perfect no. ",a);
}
else
{
printf("\n  %d is Not a perfect no. ",a);
}
printf("\n  Do you want to continue (y/n)");
ch=getche();
}while(ch=='y' || ch=='Y');
getch();
}

WAP to find all Armstrong no. in range of 0 and 999

#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int m,b,k,x;
printf("  Armstrong no. between 0 to 999 are ");
for(int a=0;a<=999;a++)
{
b=a;
int m=0;
while(b<0)
{
k=b%10;
m+=m+k*k*k;
b=b/10;
}
if(m==a)
{
printf("%d ",a);
}
}
getch();
}

WAP to generate the divisor of number.

#include<conio.h>
#include<stdio.h>
void main()
{
clrscr();
int a;
printf("\nEnter valid no. to generate Divisor : ");
scanf("%d",&a);
printf("\n Divisors are  ");
for(int i=1;i<=a/2;i++)
{
if(a%i==0)
{
printf("%d ",i);
}
}
getch();
}

Assume that USA uses the following income tax code formula for their annual income:- First US$ 5000 of income tax is 0% tax, next US$ 10,000 of income 10% tax, next US$ 20,000 of income 15% tax, and an amount above US$ 35,000 20% tax.


#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
char ch;
long int k,b,a;
do
{
printf("\nEnter the income to be calculate tax");
scanf("%ld",&a);
k=0;
if(a>5000)
{
k=k+0;
}
if(a>10000)
{
k=k+1000;
}
if(a>20000)
{
k=k+3000;
}
if(a > 35000)
{
b=a-35000;
b=b*20/100;
k+=b;
}
printf("\n The calculate tax is = %ld ",k);
printf("\n Do you want to continue(y/n)...");
ch=getche();
}while(ch=='y' || ch=='Y');
getch();
}