Posts Tagged Function

lastDigit

Given two non-negative int values, return true if they have the same last digit, such as with 27 and 57. Note that the % “mod” operator computes remainders, so 17 % 10 is 7.

lastDigit(7, 17) ? true
lastDigit(6, 17) ? false
lastDigit(3, 113) ? true

public boolean lastDigit(int a, int b) {
if(a%10 == b%10) return true;
else return false;
}

, ,

No Comments

intMax

Given three int values, A B C, return the largest.

intMax(1, 2, 3) ? 3
intMax(1, 3, 2) ? 3
intMax(3, 2, 1) ? 3

public int intMax(int a, int b, int c) {
if(a>b)
if(a>c) return a;
else return c;
else if(b>c) return b;
else return c;
}

, ,

No Comments

front22

Given a string, take the first 2 chars and return the string with the 2 chars added at both the front and back, so “kitten” yields”kikittenki”. If the string length is less than 2, use whatever chars are there.

front22(”kitten”) ? “kikittenki”
front22(”Ha”) ? “HaHaHa”
front22(”abc”) ? “ababcab”

public String front22(String str) {
int n=str.length();
if(n<2) return(str+str+str);
else return( str.substring(0,2)+str+str.substring(0,2));
}

, , , ,

No Comments

Write a program for sleepIn problem.

The parameter weekday is true if it is a weekday, and the parameter vacation is true if we are on vacation. We sleep in if it is not a weekday or we’re on vacation. Return true if we sleep in.

sleepIn(false, false) ->true
sleepIn(true, false) ->false
sleepIn(false, true) ->true

public boolean sleepIn(boolean weekday, boolean vacation) {
if((weekday != true)||(vacation ==true)) return (true);
else return (false);

}

,

No Comments

Write a program that replace two or more blank spaces by single blank.

e.g. ” i    hate   my          books”
output:: “i hate my books”

#include<stdio.h>

void blank(char *str)
{
int i=0;
char *temp,timepass[100];
temp=timepass;

while(*str!='\0')
{ if(*str !=' ')
{ *temp=*str;
temp++;
}
else
{ if(*(str+1) != ' ')
{ *temp=*str;
temp++;
}
}

str++;
}

*temp='\0';
puts(" ");
printf("\n%s",timepass);
puts(" ");
}

main()
{
char str[100];
puts("Enter a string:: ");
gets(str);
blank(str);
}

, , ,

4 Comments

Write a program that convert a string like “123″ to a integer 123.

#include<stdio.h>
main()
{
int i=0,n;
char str[10];
puts("Enter the string:: ");
gets(str);
n=atoi(str);
printf("\nThe integer value is:: %d\n", n);
}

, , ,

No Comments

Write a program to find total number of +ve, -ve, odd and even numbes in an array.

#include<stdio.h>
numbers(int *arr, int n)
{ int i,a=0,b=0,c=0,d=0;
while(n>0)
{ if(*arr < 0) a++;
if(*arr > 0) b++;
if((*arr % 2)==0) c++;
if((*arr % 2)!=0) d++;
n--;
arr++;
}

printf("\nNumber of +ve numbers:: %d\n",b);
printf("\nNumber of -ve numbers:: %d\n",a);
printf("\nNumber of even numbers:: %d\n",c);
printf("\nNumber of odd numbers:: %d\n",d);
}

main()
{
int n, a[100],i=0;
printf("\nEnter the dimension of array::");
scanf("%d",&n);
printf("\nEnter the elments of array:: ");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
numbers(a,n);
}

, , , , , ,

No Comments

Write a program that search a number in an array and also display number of times it appears.

#include<stdio.h>
search(int * arr, int n, int m)
{

int l=0;
while(n>0)
{ if(m== *arr)
l++;
n--;
arr++;

}
return l;
}

main()
{ int n, m,i=0,a[100],l;
printf("\nEnter the dimension of array:: ");
scanf("%d",&n);
printf("\nEnter the elments of array:: ");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("\nEnte the number you want to search:: ");
scanf("%d",&m);
l=search(a,n,m);
printf("\nArray:: ");
for(i=0;i<n;i++)
printf("%d ",a[i]);
if(l!=0)
printf("\nThe number is found and %d times it appears in array\n", l);
else printf("\n number not found!!!\n");
}

, , , ,

No Comments

Program that reads a integer number and calculate sum of its digits using function.

import java.io.*;
class digit
{ private int a,b,c,d;
int sumofdigit(int x)
{
a=x%10;
x=x/10;
b=x%10;
x=x/10;
c=x%10;
x=x/10;
d=x%10;
return(a+b+c+d);
}

}
class main
{ public static void main(String[] asd)throws IOException
{
BufferedReader obj=new BufferedReader(new InputStreamReader(System.in));
int n;
String num;
digit d=new digit();
System.out.println("Enter a number::");
num=obj.readLine();
n=Integer.parseInt(num);
System.out.println("Sum of digits= "+d.sumofdigit(n)+".");
}
}

, , ,

No Comments

Program that reads a integer number and printthe reverse of it using function.

import java.io.*;
class reverse
{ private int a,b,c,d;
int reversedigit(int x)
{
a=x%10;
x=x/10;
b=x%10;
x=x/10;
c=x%10;
x=x/10;
d=x%10;

return(a*1000+b*100+c*10+d);
}

}
class main
{ public static void main(String[] asd)throws IOException
{
BufferedReader obj=new BufferedReader(new InputStreamReader(System.in));
int n;
String num;
reverse d=new reverse();
System.out.println("Enter a four digit number::");
num=obj.readLine();
n=Integer.parseInt(num);
System.out.println("Reverse number= "+d.reversedigit(n)+".");
}
}

, , , ,

No Comments