- Print Without Semicolon
-(void)printWithoutSemicolon
{
while(!printf("naveen"));
}
- Case Sensitive Compare
-(void)caseSensitiveCompare
{
if([@"test" isEqualToString:@"Test"])
{
printf("Case Sensitive Compare :: Both String Are Equal");
}
else
{
printf("Case Sensitive Compare :: Both String Are Not Equal");
}
}
- Case InSensitive Compare
-(void)caseInSensitiveCompare
{
if([@"Some String" caseInsensitiveCompare:@"some string"] == NSOrderedSame )
{
printf("Case InSensitive Compare :: Both String Are Equal");
}
else
{
printf("Case InSensitive Compare :: Both String Are Not Equal");
}
}
- Perfect Number
In number theory, a perfect number is a positive integer that is equal to the sum of its proper positive divisors, that is, the sum of its positive divisors excluding the number itself (also known as its aliquot sum).
-(void)perfectNumber
{
int n = 6;
int i = 1;
int sum = 0;
while(i < n)
{
if(n % i == 0)
{
sum = sum + i;
}
i++;
}
if(sum == n)
printf("%d is a perfect number",i);
else
printf("%d is not a perfect number",i);
}
- Perfect Number In Range
-(void)perfectNumberInRange
{
int n;
int i;
int sum;
int min = 1;
int max = 100;
for(n = min; n <= max; n++)
{
i = 1;
sum = 0;
while(i < n)
{
if(n%i == 0)
{
sum = sum + i;
}
i++;
}
if(sum == n)
printf("Perfect numbers in given range is : %d\n",n);
}
}
- Fibonacci Series
The Fibonacci Sequence is the series of numbers: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, ... The next number is found by adding up the two numbers before it. The 2 is found by adding the two numbers before it (1+1)
-(void)fibonacciSeries
{
int first = 0;
int second = 1;
int sum =0, range = 10, counter = 3;
printf("%d %d ", first,second);
while (counter <= range)
{
sum = first + second;
printf("%d ", sum);
first = second;
second = sum;
counter++;
}
}
- Armstrong Number
-(void)armstrongNumber
{
int num = 153;
int sum = 0;
int r;
int temp;
temp = num;
while(temp != 0)
{
r = temp % 10;
temp = temp/10;
sum = sum + (r*r*r);
}
if(sum == num)
printf("%d is an Armstrong number",num);
else
printf("%d is not an Armstrong number",num);
}
- Armstrong Number In Range
-(void)armstrongNumberInRange
{
int num, sum, r, temp;
int min = 1;
int max = 200;
for(num = min; num <= max; num++)
{
temp = num;
sum = 0;
while(temp != 0)
{
r = temp % 10;
temp = temp/10;
sum = sum + (r*r*r);
}
if(sum == num)
printf("Armstrong numbers in given range are : %d",num);
}
}
- Palindrome Number
A palindromic number is a number that is the same when written forwards or backwards. The first few palindromic numbers are therefore are 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 22, 33, 44, 55, 66, 77, 88, 99, 101, 111, 121, ... (OEIS A002113).
-(void)palindromeNumber
{
int num = 131;
int r;
int sum = 0;
int temp;
temp = num;
while(temp != 0)
{
r = temp % 10;
temp = temp/10;
sum = (sum * 10) + r;
}
if(num == sum)
printf("%d is a palindrome",num);
else
printf("%d is not a palindrome",num);
}
- Palindrome Number In Range
-(void)palindromeNumberInRange
{
int num;
int r;
int sum;
int temp;
int min = 1;
int max = 100;
for(num = min; num <= max; num++)
{
temp = num;
sum = 0;
while(temp != 0)
{
r = temp % 10;
temp = temp/10;
sum = (sum * 10) + r;
}
if(num == sum)
printf("Palindrome numbers in given range is : %d\n",num);
}
}
- Palindrome String
-(void)palindromeString
{
NSString *str = @"121";
NSInteger length = str.length;
NSInteger halfLength = (length / 2);
BOOL isPalindrome = YES;
for (int i = 0; i < halfLength; i++)
{
if ([str characterAtIndex:i] != [str characterAtIndex:length - i - 1])
{
isPalindrome = NO;
break;
}
}
if(isPalindrome)
NSLog(@"%@ is a palindrome string",str);
else
NSLog(@"%@ is not a palindrome string",str);
}
- Prime Number
A prime number (or a prime) is a natural number greater than 1 that has no positive divisors other than 1 and itself. A natural number greater than 1 that is not a prime number is called a composite number.
-(void)primeNumber
{
int num = 3;
int i;
int count = 0;
for(i = 2; i <= num/2; i++)
{
if(num %i == 0)
{
count++;
break;
}
}
if(count == 0 && num != 1)
printf("%d is a prime number",num);
else
printf("%d is not a prime number",num);
}
- Prime Number In Range
-(void)primeNumberInRange
{
int num, i, count;
int min = 1;
int max = 200;
for(num = min; num <= max; num++)
{
count = 0;
for(i = 2; i <= num/2; i++)
{
if(num %i == 0)
{
count++;
break;
}
}
if(count == 0 && num != 1)
printf("Prime numbers in given range are : %d",num);
}
}
- Prime Number Sum
-(void)primeNumberSum
{
int num, i, count;
int sum = 0;
int min = 1;
int max = 100;
for(num = min; num <= max; num++)
{
count = 0;
for(i = 2; i <= num/2; i++)
{
if(num %i == 0)
{
count++;
break;
}
}
if(count == 0 && num != 1)
{
sum = sum + num;
}
}
printf("Sum of prime numbers is : %d",sum);
}
- Factorial With While Loop
-(void)factorial1
{
int i = 1;
int f = 1;
int num = 3;
while(i <= num)
{
f = f*i;
i++;
}
printf("Factorial of %d is: %d",num,f);
}
- Factorial With For Loop
-(void)factorial2
{
int f = 1;
int num = 3;
for(int i = 1; i <= num; i++)
f = f*i;
printf("Factorial of %d is: %d",num,f);
}
- Factorial In Range
-(void)factorialInRange
{
int f;
int num;
int min = 1;
int max = 10;
for(num = min; num <= max; num++)
{
f = 1;
for(int i = 1; i <= num; i++)
f = f*i;
printf("Factorial of %d is: %d \n",num,f);
}
}
- Multiplication Table
Enter the number range: 5
1*1=1 1*2=2 1*3=3 1*4=4 1*5=5 1*6=6 1*7=7 1*8=8 1*9=9 1*10=10
2*1=2 2*2=4 2*3=6 2*4=8 2*5=10 2*6=12 2*7=14 2*8=16 2*9=18 2*10=20
3*1=3 3*2=6 3*3=9 3*4=12 3*5=15 3*6=18 3*7=21 3*8=24 3*9=27 3*10=30
4*1=4 4*2=8 4*3=12 4*4=16 4*5=20 4*6=24 4*7=28 4*8=32 4*9=36 4*10=40
5*1=5 5*2=10 5*3=15 5*4=20 5*5=25 5*6=30 5*7=35 5*8=40 5*9=45 5*10=50
-(void)multiplicationTable
{
int range = 3;
int i,j;
for(i = 1; i <= range; i++)
{
for(j = 1; j <= 10; j++)
{
printf("%d*%d = %d ",i,j,i*j);
}
printf("\n");
}
}
- ASCII Table
-(void)tableASCII
{
int i;
for(i = 0; i <= 255; i++)
printf("ASCII value of character %c is %d\n",(char)i,i);
}
- Leap Year
Definition of leap year:
Rule 1: A year is called leap year if it is divisible by 400.
For example: 1600, 2000 etc leap year while 1500, 1700 are not leap year.
Rule 2: If year is not divisible by 400 as well as 100 but it is divisible by 4 then that year are also leap year.
For example: 2004, 2008, 1012 are leap year.
Leap year logic or Algorithm of leap year or Condition for leap year:
IF year MODULER 400 IS 0
THEN leap_year
ELSE IF year MODULER 100 IS 0
THEN not_leap_year
ELSE IF year MODULER 4 IS 0
THEN leap_year
ELSE
not_leap_year
-(void)leapYear
{
int year = 2010;
if(((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0))
printf("%d is a leap year",year);
else
printf("%d is not a leap year",year);
}
- Leap Year In Range
-(void)leapYearInRange
{
int year;
int minYear = 2001;
int maxYear = 2050;
for(year = minYear; year <= maxYear; year++)
{
if(((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0))
printf("Leap years in given range is :: %d \n",year);
}
}
- Reverse Number
-(void)reverseNumber
{
int num = 123;
int r;
int reverse = 0;
int temp = num;
while(temp != 0)
{
r = temp % 10;
temp = temp/10;
reverse = (reverse * 10) + r;
}
printf("Reverse of number %d is %d",num,reverse);
}
- Sum of Digits in Number
-(void)digitSum
{
int num = 123;
int sum = 0;
int r;
int temp = num;
while(temp != 0)
{
r = temp % 10;
temp = temp/10;
sum = sum + r;
}
printf("Sum of digits %d is %d",num,sum);
}
- Power of a Number
-(void)power
{
int pow = 2;
int num = 3;
int i = 1;
int sum = 1;
while(i <= pow)
{
sum = sum * num;
i++;
}
printf("%d to the power %d is: %d",num,pow,sum);
}
- Generic Root
It sum of digits of a number unit we don't get a single digit. For example:
Generic root of 456: 4 + 5 + 6 = 15 since 15 is two digit numbers so 1 + 5 = 6
So, generic root of 456 = 6
-(void)genericRoot
{
int num = 456;
int x;
printf("Generic root :: %d",(x = num % 9) ? x : 9);
}
- Print 1 to 100 Without Using Loop.
-(void)print1To100WithoutUsingLoop
{
int num = 1;
[self print:num];
}
-(void)print:(int)num
{
if(num<=100)
{
printf("%d ",num);
[self print:num+1];
}
}
- Swap Two Number Without Using Third
-(void)swapTwoNumber
{
int a=5, b=10;
a=b+a;
b=a-b;
a=a-b;
printf("a = %d & b = %d",a,b);
}
- Max No Using Conditional or Ternary Operator
max = a > b ? a : b;
max = a > b ? (a > c ? a : c) : (b > c ? b : c);
max = a > b ? ((a > c) ? (a > d ? a : d) : (c > d ? c : d))
: ((b > c) ? (b > d ? b : d) : (c > d ? c : d));
- Sum of Series 1+2+3+....n
-(void)sumOfSeries
{
int maxNum = 3;
int sum = 0;
for(int i = 1; i <= maxNum; i++)
{
sum = sum + i;
}
printf("Sum is %d",sum);
}
- Sum of Series 1^2+2^2+3^2+......+n^2
-(void)sumOfSeries
{
int maxNum = 3;
int power = 2;
int sum = 0;
for(int i = 1; i <= maxNum; i++)
{
int j = 1;
int sum1 = 1;
while(j <= power)
{
sum1 = sum1 * i;
j++;
}
sum = sum + sum1;
}
printf("Sum is %d",sum);
}
- Sum of Series 1 + ½ + ⅓ + ¼ + … 1/n
-(void)sumOfSeries
{
float n = 4, sum = 0, i;
for (i = 1; i <= n; i++)
{
sum = sum + (1 / i);
}
printf("\n The sum of the given series is %.2f", sum);
}
- Sum of Series 1 + ¼ + 1/9 + 1/16 + … 1/n
-(void)sumOfSeries4
{
float n = 5, sum = 0, i, sqr;
for (i = 1; i <= n; i++)
{
sqr = i*i;
sum = sum + (1 / sqr);
}
printf("\n The sum of the given series is %.2f", sum);
}
- Sum of AP Series
Definition of arithmetic progression (A.P.):
A series of numbers in which difference of any two consecutive numbers is always a same number that is constant. This constant is called as common difference.
Example of A.P. series:
5 10 15 20 25 …
Here common difference is 5 since difference of any two consecutive numbers for example 20 – 15 or 25 -20 is 5.
Sum of A.P. series:
Sn = n/2(2a + (n-1) d)
Tn term of A.P. series:
Tn = a + (n-1) d
-(void)sumOfAPSeries
{
int a = 1; //first number of the A.P. series
int d = 2; //common difference of A.P. series
int n = 5; //total numbers in the A.P. series
int temp;
int sum = 0;
temp = a;
for(int i = 1; i <= n; i++)
{
printf("%d ", temp);
sum = sum + temp;
temp = temp + d;
}
printf("\n Sum of the AP series till %d terms is %d", n, sum);
}
- Sum of GP Series
Definition of geometric progression (G.P.):
A series of numbers in which ratio of any two consecutive numbers is always a same number that is constant. This constant is called as common ratio.
Example of G.P. series:
2 4 8 16 32 64
Here common difference is 2 since ratio any two consecutive numbers for example 32 / 16 or 64/32 is 2.
Sum of G.P. series:
Sn =a(1–rn+1)/(1-r)
Tn term of G.P. series:
Tn = arn-1
Sum of infinite G.P. series:
Sn = a/(1-r) if 1 > r
= a/(r-1) if r > 1
-(void)sumOfGPSeries
{
int a = 1; //first number of the G.P. series
int r = 2; //common ratio of G.P. series
int n = 5; //total numbers in the G.P. series
int temp;
int sum = 0;
temp = a;
for(int i = 1; i <= n; i++)
{
printf("%d ", temp);
sum = sum + temp;
temp = temp * r;
}
printf("\n Sum of the GP series till %d terms is %d", n, sum);
}
- Largest Element in Array
-(void)largestElementInAnArray
{
NSArray *array = @[@"12",@"15",@"5",@"50",@"10"];
int largestNo = [[array objectAtIndex:0] intValue];
for(int i = 1; i < array.count; i++)
{
if(largestNo <= [[array objectAtIndex:i] intValue])
{
largestNo = [[array objectAtIndex:i] intValue];
}
}
printf("Largest Number is %d",largestNo);
}
- Largest Two Element In Array If no duplicate number in Array
-(void)twoLargestElementInAnArray
{
NSArray *array = @[@"12",@"15",@"5",@"50",@"20"];
int largest1 = [[array objectAtIndex:0] intValue];
int largest2 = [[array objectAtIndex:1] intValue];
int temp;
if(largest1 < largest2)
{
temp = largest1;
largest1 = largest2;
largest2 = temp;
}
for(int i = 2; i < array.count; i++)
{
if ([array[i] intValue] > largest1)
{
largest2 = largest1;
largest1 = [array[i] intValue];
}
else if ([array[i] intValue] > largest2)
{
largest2 = [array[i] intValue];
}
}
printf("%d is the first largest \n", largest1);
printf("%d is the second largest \n", largest2);
printf("Average of %d and %d = %d", largest1, largest2,(largest1 + largest2) / 2);
}
- Find first and fourth digit’s sum of Four digit Number Like 1548
-(void)sum
{
int num, a, b, sum;
a = num % 10;
b = num / 1000;
sum = a + b;
printf(“Sum is :: %d”,sum);
}
- Total Number of Digits in Number
-(void)digitCount
{
int n = 54156;
int i = 0;
while(n != 0)
{
n = n / 10;
i++;
}
printf("Total Number of digits is :: %d",i);
}
- Union of Sorted Array when no duplicate element
For union of two arrays, follow the following merge procedure.
1) Use two index variables i and j, initial values i = 0, j = 0
2) If arr1[i] is smaller than arr2[j] then print arr1[i] and increment i.
3) If arr1[i] is greater than arr2[j] then print arr2[j] and increment j.
4) If both are same then print any of them and increment both i and j.
5) Print remaining elements of the larger array.
-(void)unionOfSortedArray
{
NSArray *array1 = @[@"2", @"3", @"4", @"5", @"7"];
NSArray *array2 = @[@"2", @"3", @"5", @"8",@"10"];
int i = 0, j = 0;
int m = array1.count;
int n = array2.count;
while (i < m && j < n)
{
int a = [[array1 objectAtIndex:i] intValue];
int b = [[array2 objectAtIndex:j] intValue];
if (a < b)
{
printf("%d ", a);
i++;
}
else if (b < a)
{
printf("%d ", b);
j++;
}
else
{
printf("%d ", b);
i++;
j++;
}
}
/* Print remaining elements of the larger array */
while(i < m)
{
int a = [[array1 objectAtIndex:i] intValue];
printf("%d ", a);
i++;
}
while(j < n)
{
int b = [[array2 objectAtIndex:j] intValue];
printf("%d ", b);
j++;
}
}
- Intersection of Sorted Array When No Duplicate Items
For Intersection of two arrays, print the element only if the element is present in both arrays.
1) Use two index variables i and j, initial values i = 0, j = 0
2) If arr1[i] is smaller than arr2[j] then increment i.
3) If arr1[i] is greater than arr2[j] then increment j.
4) If both are same then print any of them and increment both i and j.
-(void)intersectionOfSortedArray
{
NSArray *array1 = @[@"2", @"3", @"4", @"5", @"7"];
NSArray *array2 = @[@"2", @"3", @"5", @"8",@"10"];
int i = 0, j = 0;
int m = array1.count;
int n = array2.count;
while (i < m && j < n)
{
int a = [[array1 objectAtIndex:i] intValue];
int b = [[array2 objectAtIndex:j] intValue];
if (a < b)
{
i++;
}
else if (b < a)
{
j++;
}
else
{
printf("%d ", b);
i++;
j++;
}
}
}
- Largest Three Number In array
-(void)threeLargestElementInAnArray
{
NSArray *array = @[@"12",@"15",@"5",@"50",@"20"];
int largest1 = [[array objectAtIndex:0] intValue];
int largest2 = [[array objectAtIndex:0] intValue];
int largest3 = [[array objectAtIndex:0] intValue];
for(int i = 0; i < array.count; i++)
{
if ([array[i] intValue] > largest1)
{
largest1 = [array[i] intValue];
}
}
for(int i = 0; i < array.count; i++)
{
if ([array[i] intValue] > largest2 && largest1 > [array[i] intValue])
{
largest2 = [array[i] intValue];
}
}
for(int i = 0; i < array.count; i++)
{
if ([array[i] intValue] > largest3 && largest2 > [array[i] intValue])
{
largest3 = [array[i] intValue];
}
}
printf("%d is the first largest \n", largest1);
printf("%d is the second largest \n", largest2);
printf("%d is the third largest \n", largest3);
}
- Left Rotate Array By One
-(void)leftRotateArrayByOne
{
NSMutableArray *array = [NSMutableArray arrayWithObjects:@"10",@"20",@"30",@"40", nil];
int temp = [array[0] intValue];
for(int i = 0; i < array.count-1; i++)
{
array[i] = array[i+1];
}
array[array.count-1] = [NSString stringWithFormat:@"%d",temp];
NSLog(@"Array :: %@",array);
}
- Right Rotate Array By One
-(void)rightRotateArrayByOne
{
NSMutableArray *array = [NSMutableArray arrayWithObjects:@"10",@"20",@"30",@"40", nil];
int temp = [array[array.count-1] intValue];
for(int i = array.count-1; i > 0; i--)
{
array[i] = array[i-1];
}
array[0] = [NSString stringWithFormat:@"%d",temp];
NSLog(@"Array :: %@",array);
}
- Insert Item In Sorted Array
-(void)insertItemInSortedArray
{
NSMutableArray *array = [NSMutableArray arrayWithObjects:@"10",@"20",@"30",@"40", nil];
int item = 25;
int i;
for(i = array.count-1; [array[i] intValue] > item; i--)
{
array[i+1] = array[i];
}
array[i+1] = [NSString stringWithFormat:@"%d",item];
NSLog(@"Array :: %@",array);
}
- Insert Item In Unsorted Array
-(void)insertItemInUnsortedArray
{
NSMutableArray *array = [NSMutableArray arrayWithObjects:@"20",@"10",@"50",@"40", nil];
int item = 25;
int pos = 2;
int i;
for(i = array.count-1; i >= pos-1; i--)
{
array[i+1] = array[i];
}
array[i+1] = [NSString stringWithFormat:@"%d",item];
//array[pos-1] = [NSString stringWithFormat:@"%d",item];
NSLog(@"Array :: %@",array);
}
- Linear Search
-(void)linearSearch
{
NSArray *array = @[@"12",@"10",@"50",@"13",@"45",@"23"];
int item = 13;
int foundi;
int i;
for (i = 0, found = 0; i < array.count; i++)
{
if([array[i] intValue] == item)
{
found = 1;
break;
}
}
if(found == 1)
printf("Found at position %d",i+1);
else
printf("not found");
}
- Binary Search
-(void)binarySearch
{
NSArray *array = @[@"12",@"10",@"50",@"13",@"45",@"23"];
int item = 13;
int found = 0;
int low = 0;
int high = array.count;
int mid = 0;
while(low <= high)
{
mid = (low + high)/2;
if(item == [array[mid] intValue])
{
found = 1;
break;
}
else if (item > [array[mid] intValue])
{
low = mid + 1;
}
else
{
high = mid - 1;
}
}
if(found == 1)
{
printf("Found at position %d",mid);
}
else
{
printf("Item not found");
}
}
- Check At Least Two Boolean are true Out of Three
-(void)checkTwoBooleanOutofThreeAreTrue
{
BOOL a = true;
BOOL b = false;
BOOL c = false;
printf("is two boolean are true :: %d \n",a ? (b || c) : (b && c));
printf("is two boolean are true :: %d \n",(a && (b || c)) || (b && c));
printf("is two boolean are true :: %d \n",a ^ b ? c : a);
printf("is two boolean are true :: %d \n",(a?1:0)+(b?1:0)+(c?1:0) >= 2);
}
- Pattern
*
***
*****
*******
*********
-(void)printPattern1
{
int row;
int c;
int n = 5;
int temp;
temp = n;
for (row = 1; row <= n; row++)
{
for (c = 1; c < temp; c++)
{
printf(" ");
}
temp--;
for (c = 1; c <= 2*row - 1; c++)
{
printf("*");
}
printf("\n");
}
}
- Pattern
*
**
***
****
*****
-(void)printPattern2
{
int n = 5; //No of row
int c;
int k;
for ( c = 1; c <= n; c++ )
{
for( k = 1; k <= c; k++)
printf("*");
printf("\n");
}
}
- Pattern
*****
****
***
**
*
-(void)printPattern3
{
int n = 5; //No of row
int i, j;
for(i = n; i >= 1; i--)
{
for(j =1; j <= i; j++)
{
printf("*");
}
printf("\n");
}
}
- Pattern
1
232
34543
4567654
567898765
-(void)printPattern4
{
int n = 5; //No of row
int c, d, num = 1, space;
space = n - 1;
for ( d = 1; d <= n; d++)
{
num = d;
for ( c = 1; c <= space; c++)
{
printf(" ");
}
space--;
for (c = 1; c <= d; c++)
{
printf("%d", num);
num++;
}
num--;
num--;
for (c = 1; c < d; c++)
{
printf("%d", num);
num--;
}
printf("\n");
}
}
- Pattern
*********
*******
*****
***
*
-(void)printPattern5
{
int n = 5; //No of row
int i, j, k;
for(i = n; i >= 1; i--)
{
for(j = n; j > i; j--)
{
printf(" ");
}
for(k = 1; k < (i*2); k++)
{
printf("*");
}
printf("\n");
}
}
- Pattern
*
***
*****
*******
*********
*******
*****
***
*
-(void)printPattern6
{
int n = 5; //No of row
int i, j, k;
for(i = 1; i <= n; i++)
{
for(j = i; j < n; j++)
{
printf(" ");
}
for(k =1; k < (i*2); k++)
{
printf("*");
}
printf("\n");
}
for(i = (n-1); i >= 1; i--)
{
for( j = n; j > i; j--)
{
printf(" ");
}
for(k = 1; k <(i*2); k++)
{
printf("*");
}
printf("\n");
}
}
- Pattern
*
**
***
****
*****
-(void)printPattern7
{
int n = 5; //No of row
int i, j, k;
for(i = n; i >= 1; i--)
{
for(j = 1; j < i; j++)
{
printf(" ");
}
for(k = n; k >= i; k--)
{
printf("*");
}
printf("\n");
}
}
- Pattern
*****
****
***
**
*
-(void)printPattern8
{
int n = 5; //No of row
int i, j, k;
for(i = n; i >= 1; i--)
{
for(j = n; j > i; j--)
{
printf(" ");
}
for(k = 1; k <= i; k++)
{
printf("*");
}
printf("\n");
}
}
- Pattern
*
* *
* *
* *
* *
* *
* *
* *
*
-(void)printPattern9
{
int n = 5; //No of row
int i, j;
for(i=1; i<=n; i++)
{
for(j=n; j>i; j--)
{
printf(" ");
}
printf("*");
for(j=1; j<(i-1)*2; j++)
{
printf(" ");
}
if(i==1)
printf("\n");
else
printf("*\n");
}
for(i=(n-1); i>=1; i--)
{
for(j=n; j>i; j--)
{
printf(" ");
}
printf("*");
for(j=1; j<(i-1)*2; j++)
{
printf(" ");
}
if(i==1)
printf("\n");
else
printf("*\n");
}
}
- Pattern
*****
*****
*****
*****
*****
-(void)printPattern10
{
int n = 5; //No of row
int i, j;
for(i=1; i<=n; i++)
{
for(j=1; j<=n; j++)
{
printf("*");
}
printf("\n");
}
}
- Reverse String
-(void)reverseString
{
NSString *str = @"abcde";
NSMutableString *reversedString = [NSMutableString string];
NSInteger charIndex = [str length];
while (charIndex > 0)
{
charIndex--;
NSRange subStrRange = NSMakeRange(charIndex, 1);
[reversedString appendString:[str substringWithRange:subStrRange]];
}
NSLog(@"Reversed String :: %@", reversedString);
}
-(void)reverseString
{
char str[100], temp;
int i, j = 0;
printf("\nEnter the string :");
gets(str);
i = 0;
j = strlen(str) - 1;
while (i < j)
{
temp = str[i];
str[i] = str[j];
str[j] = temp;
i++;
j--;
}
printf("Reverse string is :%s", str);
}
- Search String
-(void)searchString
{
NSString *car = @"Maserati GranCabrio";
NSRange searchResult = [car rangeOfString:@"Cabrio"];
if (searchResult.location == NSNotFound)
{
NSLog(@"Search string was not found");
}
else
{
NSLog(@"'Cabrio' starts at index %lu and is %lu characters long",(unsigned long)searchResult.location, (unsigned long)searchResult.length); //13 // 6
}
}
- Subdivide String
-(void)subdivideString
{
NSString *car = @"Maserati GranTurismo";
NSLog(@"%@", [car substringToIndex:8]); // Maserati
NSLog(@"%@", [car substringFromIndex:9]); // GranTurismo
NSRange range = NSMakeRange(9, 4);
NSLog(@"%@", [car substringWithRange:range]); // Gran
}
This was truly awesome. thanks so much for this!! iOS App Development Online Training
ReplyDelete