Total Pageviews

Saturday 29 June 2013

Write a program to check whether the year is leap year or not.

 Write a Code in main.m

#import <Foundation/Foundation.h>
@interface LeapYear :NSObject
{
    int yr;
}
-(void)display;
@end


@implementation LeapYear
-(void)display
{
    printf("Enter the year:");
    scanf("%d",&yr);

    if(yr%2==0 && yr%100!=0)
    {
            printf("Year is LEAP YEAR");
    }
    else
    {
            printf("Year is Not LEAP YEAER");
    }
}

@end
int main (int argc, const char * argv[])
{
   
    LeapYear *lyr=[[LeapYear alloc]init];
    [lyr display];
}

OUTPUT:-

Year is Leap Year


 
Year is not Leap Year

Friday 28 June 2013

Write a program in Objective C, take a number in a method as an argument and print the digits of a number in Word form. Using interface and implementation. E.g.123 output one two three

#import <Foundation/Foundation.h>
@interface digits:NSObject
{
    int rev,n,n1,d;
   
}
-(void)reverse;
@end

@implementation digits
-(void)reverse
{
    printf("Enter any Numeric Value:");
    scanf("%d",&n);
   
    while (n!=0)
    {
        d=n%10;
        rev=rev*10+d;
        n=n/10;
    }
   
    while (rev!=0)
    {
        d=rev%10;
        switch (d)
        {
            case 0:
                printf("Zero\n");
                break;
               
            case 1:
                printf("One\n");
                break;
               
            case 2:
                printf("Two\n");
                break;
               
            case 3:
                printf("Three\n");
                break;
               
            case 4:
                printf("Four\n");
                break;
               
            case 5:
                printf("Five\n");
                break;
               
            case 6:
                printf("Six\n");
                break;
               
            case 7:
                printf("Seven\n");
                break;
               
            case 8:
                printf("Eight\n");
                break;
               
            case 9:
                printf("Nine\n");
                break;
               
            default:
                printf("Please Enter any digit");
                break;
        }
        rev=rev/10;
    }
}


@end

int main (int argc, const char * argv[])
{

    digits *dig=[[digits alloc]init];
    [dig reverse];

   
    return 0;

}

Define a new class called Rectangle having width and height. Develop the methods to set rectangle's width and height. Retrieve these values and calculate area of the rectangle and perimeter. Using interface and implementation.

made an Xcode Program and write a code into .h file

#import <Foundation/Foundation.h>
@interface rectangle:NSObject
{
    float l,w;
    float area,per;
}

-(void)calculate;
         
@end


@implementation rectangle

-(void)calculate;
{

    printf("Enter the 2 values for each side of the rectangle:\n");
    scanf("%f%f",&l,&w);
   
    area=l*w;
    per=2*(l+w);
    printf("Area of Rectangle is:%f\n",area);
    printf("\nPerimeter of Rectangle is:%f",per);
   
   
}

@end


int main (int argc, const char * argv[])
{

    rectangle *rec=[[rectangle alloc]init];
    [rec calculate];
    return 0;

}

Write a program and print the calculator operations using interface and implementation (Use switch case).


#import <Foundation/Foundation.h>
@interface calc:NSObject
{
    int a,b,res,ch;
}

-(void)calculate;

@end


@implementation calc

-(void)calculate;
{

printf("\n Menu:\n");
printf("\n\t(1)ADDITION");
printf("\n\t(2)SUBTRACTION");
printf("\n\t(3)MULTIPLICATION");
printf("\n\t(4)DIVISION");
printf("\n\t(0)Exit");
printf("\n Enter your Choice:\n");
scanf("%d",&ch);  

if (ch<=4 && ch>0)
{
    printf("Enter two numbers:\n");
    scanf("%d%d",&a,&b);
}

switch (ch)
{
    case 1:
        res=a+b;
        printf("\n Addition:%d",res);
        break;
       
    case 2:
        res=a-b;
        printf("\n Subtraction:%d",res);
        break;
       
    case 3:
        res=a*b;
        printf("\n Multiplication:%d",res);
        break;
       
    case 4:
        res=a/b;
        printf("\n Division:%d",res);
        break;
       
    case 0:
        printf("\n Choice Terminated");
        exit(1);
        break;
       
    default:
        printf("\n Invalid Choice..!!");
              
}
}
@end


int main (int argc, const char * argv[])
{
    calc *cal=[[calc alloc]init];
    [cal calculate];   
   
    return 0;
}