Tuesday 21 February 2017

Implement an itoa function


How do you implement an itoa function?

We can use below logic
1.divide Number by base and store the reminder in a buffer.
2.Change the Number with division result and repeat step 1 till Number become 1
3. reverse the buffer to have final result.


int my_itoa(int number, unsigned short base, char *output, size_t max_len){
        int length = 0;
        if(output == NULL || base > 36){
                return -1;
        }
        for( length=0; length < max_len; length++){
                char digit = number%base;
                if(digit<10){
                        digit += '0';
                }else{
                        digit += ('A'-10);
                }
                output[length] = digit;
                number = number/base;
                if(number==0){
                        break;
                }
        }

        if(length < max_len){
                my_strrev(output); // reverse buffer
        }else{
                return -1;
        }
        return length;
}

complete code


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

int my_strrev(char *string){
        if(string == NULL){
                return 0;
        }
        int string_len = strlen(string);
        for(int left=0, right=string_len-1; left < right; left++, right--){
                char temp_ch = string[left];
                string[left] = string[right];
                string[right] = temp_ch;
        }
}

int my_itoa(int number, unsigned short base, char *output, size_t max_len){
        int length = 0;
        if(output == NULL || base > 36){
                return -1;
        }
        for( length=0; length < max_len; length++){
                char digit = number%base;
                if(digit < 10){
                        digit += '0';
                }else{
                        digit += ('A'-10);
                }
                output[length] = digit;
                number = number/base;
                if(number==0){
                        break;
                }
        }

        if(length < max_len){
                my_strrev(output);
        }else{
                return -1;
        }
        return length;
}

int main(int argc, char *argv[]){
        char buffer[256]={0x00};
        int number, base;
        for(int i=0; i < 10; i++){
                number = (rand()%1000)+1;
                base = (rand()%35)+1;
                memset(buffer, 0x00, sizeof(buffer));
                my_itoa(number, base, buffer, 256);
                printf("%d in Base %d =%s\n", number, base, buffer );
        }
        return 0;
}

Compile and Output


rajesh@ideapad:~/Rajesh/Blog/string$ gcc myitoa.c 
rajesh@ideapad:~/Rajesh/Blog/string$ ./a.out 
384 in Base 12 =280
778 in Base 6 =3334
794 in Base 11 =662
387 in Base 3 =112100
650 in Base 32 =KA
363 in Base 13 =21C
691 in Base 5 =10231
764 in Base 22 =1CG
541 in Base 2 =1000011101
173 in Base 17 =A3

Monday 20 February 2017

find circular linked list in c


How do you check whether a linked list is circular?

Most common algorithm is using "Floyd's Tortoise and hare" algorithm.

For More in Details

int list_find_cicular(node_p head){
        node_p fast = head;
        node_p slow = head;
        if(head == NULL || head->next == NULL){
                return 0;
        }
        do{
                if(fast->next->next){
                        fast = fast->next->next;
                        slow = slow->next;
                        if(slow == fast){
                                return 1;
                        }
                }else{
                        break;
                }
        }while(fast->next && slow->next);
        return 0;
}

Full Code


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

struct _node{
        void * data;
        struct _node* next;
};
typedef struct _node node_t;
typedef struct _node * node_p;

node_p new_list(void){
        node_p node= malloc(sizeof(node_t));
        assert(node!=NULL);
        node->data = NULL;
        node->next = NULL;
        return node;
}

node_p new_node(void *data){
        node_p node = malloc(sizeof(node_t));
        assert(node!=NULL);
        node->data = data;
        node->next = NULL;
        return node;
}

int list_free(node_p head){
        node_p iterator = head, temp;
        while(iterator){
                temp = iterator;
                iterator = iterator->next;
                free(temp);
        }
        return 0;
}
node_p list_tail(node_p head){
        node_p iterator = head;
        while(iterator->next != NULL){
                iterator = iterator->next;
        }
        return iterator;
}

node_p list_append(node_p head, void *data){
        node_p iterator = head;
        while(iterator->next != NULL){
                iterator = iterator->next;
        }
        iterator->next = new_node(data);
        return head;
}

node_p list_prepend(node_p head, void *data){
        node_p iterator = head;
        head = new_node(data);
        head->next = iterator;
        return head;
}

node_p list_for_each(node_p head, int (*func)(void *)){
        node_p iterator = head->next;
        while(iterator){
                func(iterator->data);
                iterator = iterator->next;
        }
}
int print_list_ele(void *data){
        if(data == NULL){
                return 0;
        }
        int number = *(int *)data;
        printf("%d\n", number);
        return 0;
}

int free_list_ele(void *data){
        if(data == NULL){
                return 0;
        }
        free(data);
        return 0;
}

int list_find_circular(node_p head){
        node_p fast = head;
        node_p slow = head;
        if(head == NULL || head->next == NULL){
                return 0;
        }
        do{
                if(fast->next->next){
                        fast = fast->next->next;
                        slow = slow->next;
                        if(slow == fast){
                                return 1;
                        }
                }else{
                        break;
                }
        }while(fast->next && slow->next);
        return 0;
}

int main(int argc, char *argv[]){
        if(argc < 2){
                fprintf(stderr, "USAGE: %s <numbers>\n", argv[0]);
                return -1;
        }
        node_p mylist = new_list();
        int *data = NULL;
        for(int i=1; i < argc; i++){
                data = malloc(sizeof(int));
                *data = atoi(argv[i]);
                list_append(mylist, data);
        }
        node_p mytail = list_tail(mylist);
        mytail->next = mylist; /*Making List Circular*/
        printf("List is %s\n", list_find_circular(mylist) ? "CIRCULAR":"NOT CIRCULAR");
        mytail->next = NULL; /* Removing Circular */
        list_for_each(mylist, print_list_ele);
        list_for_each(mylist, free_list_ele);
        list_free(mylist);
        return 0;
}

Compile and Output


rajesh@ideapad:~/Rajesh/Blog/single_linkedlist$ gcc list.c 
rajesh@ideapad:~/Rajesh/Blog/single_linkedlist$ ./a.out 10 20 30 40 50 60 70 80
List is CIRCULAR
10
20
30
40
50
60
70
80

Thursday 16 February 2017

number is in power series of 2


Write a Program to Find Given Number is in power series of 2 or Not?

Logic 1:
If Number Will keep on divide by 2 till the number become 1


int find_in_power_serise(int number, int power){
        do{
                if((number % power) ==0){
                        number = number/power;
                }else{
                        break;
                }
        }while(number > 1);
        if(number == 1){
                return 1;
        }else{
                return 0;
        }
}

Logic 2:
If we will see binary re-presentation of a Number.
we can come to know that if a single bit set then that number is in power series of 2.


int is_nth_bit_set(unsigned int num, int bit){
        return ((num >> bit) & 0x01);
}

int find_in_power_serise_of_2(unsigned int number){
        int count = 0;
        for(int i=0; i < (sizeof(number)*8) ; i++){
                if(is_nth_bit_set(number, i)){
                        count ++;
                }
        }
        if(count == 1){
                return 1;
        }else{
                return 0;
        }
}

without Loop and Without using % operator


int count_bit_set(unsigned int number, int count)
{
        if(number){
                if(number&0x01)
                        count++;
                return count_bit_set((number >> 1), count);
        }
        return count;
}

int find_in_power_serise_of_2_no_loop(unsigned int number){
        if(count_bit_set(number, 0)==1){
                return 1;
        }else{
                return 0;
        }
}

full code


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

int find_in_power_serise(int number, int power){
        do{
                if((number % power) ==0){
                        number = number/power;
                }else{
                        break;
                }
        }while(number > 1);
        if(number == 1){
                return 1;
        }else{
                return 0;
        }
}

int is_nth_bit_set(unsigned int num, int bit){
        return ((num >> bit) & 0x01);
}

int find_in_power_serise_of_2(unsigned int number){
        int count = 0;
        for(int i=0; i < (sizeof(number)*8) ; i++){
                if(is_nth_bit_set(number, i)){
                        count ++;
                }
        }
        if(count == 1){
                return 1;
        }else{
                return 0;
        }
}

int count_bit_set(unsigned int number, int count)
{
        if(number){
                if(number&0x01)
                        count++;
                return count_bit_set((number >> 1), count);
        }
        return count;
}

int find_in_power_serise_of_2_no_loop(unsigned int number){
        if(count_bit_set(number, 0)==1){
                return 1;
        }else{
                return 0;
        }
}

int main(int argc, char *argv[]){
        if(argc != 2){
                fprintf(stderr, "USAGE:%s <number>\n", argv[0]);
                return -1;
        }
        int num = atoi(argv[1]);
        int power = 2;
        printf("%d is %s Power Serise of %d\n", num, find_in_power_serise(num, power)?"IN":"NOT IN", power);
        printf("%d is %s Power Serise of %d\n", num, find_in_power_serise_of_2(num)?"IN":"NOT IN", 2);
        printf("%d is %s Power Serise of %d\n", num, find_in_power_serise_of_2_no_loop(num)?"IN":"NOT IN", 2);
        return 0;
}

Compilation and Output


rajesh@ideapad:~/Rajesh/Blog/math$ gcc power_serise_2.c
rajesh@ideapad:~/Rajesh/Blog/math$ ./a.out 64
64 is IN Power Serise of 2
64 is IN Power Serise of 2
64 is IN Power Serise of 2
rajesh@ideapad:~/Rajesh/Blog/math$ ./a.out 65
65 is NOT IN Power Serise of 2
65 is NOT IN Power Serise of 2
65 is NOT IN Power Serise of 2

Wednesday 15 February 2017

Count the how many 7 are there between 1 to 100


Write a Program to Count How many times a digit is there in a range

There is a generic formula for this also n(10(n-1)) where range is 0 to 10n


/*one generic function to find char count in an string*/
unsigned int count_char_in_str(char ch, const char *str){
        unsigned int count=0;
        while((str=strchr(str, ch))!=NULL){
                count++;
                str+=1;
        }
        return count;
}
/*find single digit count in a series of numbers*/
unsigned int count_digit_in_range(unsigned int digit, unsigned int min , unsigned int max){
        char str_number[1024]={0x00};
        unsigned int count = 0;
        for(int i=min; i < max; i++){
                sprintf(str_number, "%02d", i);
                count += count_char_in_str(digit+0x30, str_number);
                memset(str_number, 0x00, sizeof(str_number));
        }
        return count;
}

Complete Code


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

unsigned int count_char_in_str(char ch, const char *str){
        unsigned int count=0;
        while((str=strchr(str, ch))!=NULL){
                count++;
                str+=1;
        }
        return count;
}
unsigned int count_digit_in_range(unsigned int digit, unsigned int min , unsigned int max){
        char str_number[1024]={0x00};
        unsigned int count = 0;
        for(int i=min; i <= max; i++){
                sprintf(str_number, "%d", i);
                count += count_char_in_str(digit+0x30, str_number);
                memset(str_number, 0x00, sizeof(str_number));
        }
        return count;
}

int main(int argc, char *argv[]){
        int number=7;
        int range_min = 1;
        int range_max = 100;
        printf("%d Times Number %d Coming between %d to %d\n", count_digit_in_range( number, range_min, range_max), number, range_min, range_max);
        return 0;
}

compile and output


rajesh@ideapad:~/Rajesh/Blog/string$ gcc cout_total_number_of_7_coming_serise.c
rajesh@ideapad:~/Rajesh/Blog/string$ ./a.out 
20 Times Number 7 Coming between 1 to 100

Tuesday 14 February 2017

print "xay" in place of letter 'a' in string


Write A Program to print string "xay" for every letter a inside provided string


void modified_print(const char *str){
        int i=0;
        while(str[i] != '\0'){
                if(str[i] == 'a'){
                        printf("%s", "xay");
                }else{
                        printf("%c", str[i]);
                }
                i++;
        }
        printf("\n");
}

complete code


#include <stdio.h>

void modified_print(const char *str){
        int i=0;
        while(str[i] != '\0'){
                if(str[i] == 'a'){
                        printf("%s", "xay");
                }else{
                        printf("%c", str[i]);
                }
                i++;
        }
        printf("\n");
}


int main(int argc, char *argv[]){
        if(argc != 2){
                fprintf(stderr, "USAGE: %s <string>\n", argv[0]);
                return -1;
        }
        modified_print(argv[1]);
        return 0;
}

Compilation and Output


rajesh@ideapad:~/Rajesh/Blog/string$ gcc string_printer.c
rajesh@ideapad:~/Rajesh/Blog/string$ ./a.out "apple orange"
xaypple orxaynge

Monday 13 February 2017

triangle of letters in increasing order of lines


Write a Program to print triangle of letters in increasing order of lines.

This type of question usually asked to check looping concept


/*A triangle can be represented using rows and columns.
 *Each next row will contain one extra column of letters.
 */
void print_triangle(unsigned int height, char ch){
        int column=0, row=0;
        for(row=0 ; row < height; row++, printf("\n")){
                for(column=0; column <=row; column++){
                        printf("%c", ch);
                        if(ch == 'Z'){
                                ch = 'a';
                        }else if(ch == 'z'){
                                ch = 'A';
                        }else{
                                ch++;
                        }
                }
        }
}

Full Source Code for this Problem


#include <stdio.h>
#include <stdlib.h>
/*A triangle can be represented using rows and columns.
 *Each next row will contain one extra column of letters.
 */
void print_triangle(unsigned int height, char ch){
        int column=0, row=0;
        for(row=0 ; row < height; row++, printf("\n")){
                for(column=0; column <= row; column++){
                        printf("%c", ch);
                        if(ch == 'Z'){
                                ch = 'a';
                        }else if(ch == 'z'){
                                ch = 'A';
                        }else{
                                ch++;
                        }
                }
        }
}

int main(int argc, char *argv[]){
        if(argc != 2){
                fprintf(stderr, "USAGE: %s <triangle_height>\n", argv[0]);
                return -1;
        }
        int height = atoi(argv[1]);
        print_triangle(height, 'A');
        return 0;
}

Compile and Output


rajesh@ideapad:~/Rajesh/Blog/loop$ gcc triangle.c
rajesh@ideapad:~/Rajesh/Blog/loop$ ./a.out 10
A
BC
DEF
GHIJ
KLMNO
PQRSTU
VWXYZab
cdefghij
klmnopqrs
tuvwxyzABC

Sunday 12 February 2017

longest word in string


Find longest word in string in c

function to get base address of longest word in string


const char* largest_word(const char *str){
        unsigned int word_len=0, cur_word_len=0;
        const char *word = str, *cur_word=str;
        int i = 0;
        while(str[i] != '\0'){
                if(isspace(str[i])){
                        cur_word_len = (str+i) - cur_word;
                        if(word_len < cur_word_len){
                                word_len = cur_word_len;
                                word = cur_word;
                        }
                        while(isspace(str[i])){
                                i++;
                        }
                        cur_word = str+i;
                }else{
                        i++;
                }
        }
        cur_word_len = (str+i) - cur_word;
        if(word_len < cur_word_len){
                word_len = cur_word_len;
                word = cur_word;
        }
        return word;
}

find longest word using above function


int find_largest_word(const char *str, char result[]){
        const char *word = largest_word(str);
        int i=0;
        while(word[i] != '\0' && (!isspace(word[i]))){
                result[i] = word[i];
                i++;
        }
        return i;
}

full code


#include <stdio.h>
#include <ctype.h>

int my_isspace(char ch){
        return (ch == ' ' || ch == '\n' || ch == '\r' || ch == '\t' || ch == '\v' || ch == '\f');
}

const char* largest_word(const char *str){
        unsigned int word_len=0, cur_word_len=0;
        const char *word = str, *cur_word=str;
        int i = 0;
        while(str[i] != '\0'){
                if(isspace(str[i])){
                        cur_word_len = (str+i) - cur_word;
                        if(word_len < cur_word_len){
                                word_len = cur_word_len;
                                word = cur_word;
                        }
                        while(isspace(str[i])){
                                i++;
                        }
                        cur_word = str+i;
                }else{
                        i++;
                }
        }
        cur_word_len = (str+i) - cur_word;
        if(word_len < cur_word_len){
                word_len = cur_word_len;
                word = cur_word;
        }
        return word;
}
int find_largest_word(const char *str, char result[]){
        const char *word = largest_word(str);
        int i=0;
        while(word[i] != '\0' && (!isspace(word[i]))){
                result[i] = word[i];
                i++;
        }
        return i;
}

int main(int argc, char *argv[]){
        char word[256]={0x00};
        if(argc != 2){
                fprintf(stderr, "USAGE: %s <string>\n", argv[0]);
                return -1;
        }
        find_largest_word(argv[1], word);
        printf("%s\n", word);
        return 0;
}

Compile and Output


rajesh@ideapad:~/Rajesh/Blog/string$ gcc word_length.c 
rajesh@ideapad:~/Rajesh/Blog/string$ ./a.out 
USAGE: ./a.out 
rajesh@ideapad:~/Rajesh/Blog/string$ ./a.out "hello world"
hello
rajesh@ideapad:~/Rajesh/Blog/string$ ./a.out "welcome to bangalore"
bangalore
rajesh@ideapad:~/Rajesh/Blog/string$ ./a.out "India welcomes you"
welcomes

Saturday 11 February 2017

print done 100 times without using any loop, conditional operator or conditional clause


Normally This question asked after a recursion question or after series of loop question. To verify how much you are listening problem statement without in a hurry behind solution,

Here is Much difficult Answer of Interview


#include <stdio.h>
int print_done_without_loop_and_condition(void){
        printf("DONE\nDONE\nDONE\nDONE\nDONE\nDONE\nDONE\nDONE\nDONE\nDONE\n");
        printf("DONE\nDONE\nDONE\nDONE\nDONE\nDONE\nDONE\nDONE\nDONE\nDONE\n");
        printf("DONE\nDONE\nDONE\nDONE\nDONE\nDONE\nDONE\nDONE\nDONE\nDONE\n");
        printf("DONE\nDONE\nDONE\nDONE\nDONE\nDONE\nDONE\nDONE\nDONE\nDONE\n");
        printf("DONE\nDONE\nDONE\nDONE\nDONE\nDONE\nDONE\nDONE\nDONE\nDONE\n");
        printf("DONE\nDONE\nDONE\nDONE\nDONE\nDONE\nDONE\nDONE\nDONE\nDONE\n");
        printf("DONE\nDONE\nDONE\nDONE\nDONE\nDONE\nDONE\nDONE\nDONE\nDONE\n");
        printf("DONE\nDONE\nDONE\nDONE\nDONE\nDONE\nDONE\nDONE\nDONE\nDONE\n");
        printf("DONE\nDONE\nDONE\nDONE\nDONE\nDONE\nDONE\nDONE\nDONE\nDONE\n");
        printf("DONE\nDONE\nDONE\nDONE\nDONE\nDONE\nDONE\nDONE\nDONE\nDONE\n");
}

int main(int argc, char *argv[]){
        print_done_without_loop_and_condition();
        return 0;
}

Compile and Output


rajesh@ideapad:~/Rajesh/Blog$ gcc print_done.c
rajesh@ideapad:~/Rajesh/Blog$ ./a.out 
DONE
DONE
DONE
DONE
DONE
DONE
DONE
DONE
DONE
DONE
DONE
DONE
DONE
DONE
DONE
DONE
DONE
DONE
DONE
DONE
DONE
DONE
DONE
DONE
DONE
DONE
DONE
DONE
DONE
DONE
DONE
DONE
DONE
DONE
DONE
DONE
DONE
DONE
DONE
DONE
DONE
DONE
DONE
DONE
DONE
DONE
DONE
DONE
DONE
DONE
DONE
DONE
DONE
DONE
DONE
DONE
DONE
DONE
DONE
DONE
DONE
DONE
DONE
DONE
DONE
DONE
DONE
DONE
DONE
DONE
DONE
DONE
DONE
DONE
DONE
DONE
DONE
DONE
DONE
DONE
DONE
DONE
DONE
DONE
DONE
DONE
DONE
DONE
DONE
DONE
DONE
DONE
DONE
DONE
DONE
DONE
DONE
DONE
DONE
DONE

print DONE 100 times without using any loop in c

when every without using loop any one asking. That means we have to give answer using recursion.



int print_done_without_loop(unsigned int times){
        if(times){
                printf("DONE\n");
                print_done_without_loop(times-1);
        }
}

Full Code


#include < stdio.h >


int print_done_without_loop(unsigned int times){
        if(times){
                printf("DONE\n");
                print_done_without_loop(times-1);
        }
}

int main(int argc, char *argv[]){
        print_done_without_loop(100);
        return 0;
}

Compile and OUT PUT


rajesh@ideapad:~/Rajesh/Blog$ gcc print_done.c
rajesh@ideapad:~/Rajesh/Blog$ ./a.out 
DONE
DONE
DONE
DONE
DONE
DONE
DONE
DONE
DONE
DONE
DONE
DONE
DONE
DONE
DONE
DONE
DONE
DONE
DONE
DONE
DONE
DONE
DONE
DONE
DONE
DONE
DONE
DONE
DONE
DONE
DONE
DONE
DONE
DONE
DONE
DONE
DONE
DONE
DONE
DONE
DONE
DONE
DONE
DONE
DONE
DONE
DONE
DONE
DONE
DONE
DONE
DONE
DONE
DONE
DONE
DONE
DONE
DONE
DONE
DONE
DONE
DONE
DONE
DONE
DONE
DONE
DONE
DONE
DONE
DONE
DONE
DONE
DONE
DONE
DONE
DONE
DONE
DONE
DONE
DONE
DONE
DONE
DONE
DONE
DONE
DONE
DONE
DONE
DONE
DONE
DONE
DONE
DONE
DONE
DONE
DONE
DONE
DONE
DONE
DONE

find a string is colindrome or not?


Colindrome is a string which satisfy this condition
"Colidrome is a word that has 3 alphabets followed by the reverse of the 3 alphabets and so on."


/*
 * This Function will return 0 if not colindrome and return 1 if colindrome.
 */
int find_colindrome(const char *str){
        size_t len = strlen(str);
        if( (len % 6) != 0){
                return 0;
        }
        for(int itr1=0; itr1 < len; itr1+=6){
                for( int itr2=0; itr2 < 3 ; itr2++){
                        if(str[itr1+itr2] != str[itr1+5-itr2]){
                                //printf("%d:%d-%c:%c\n", itr1+itr2, itr1+5-itr2, str[itr1+itr2], str[itr1+5-itr2]);
                                return 0;
                        }
                }
        }
        return 1;
}

Full Source Code


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

int find_colindrome(const char *str){
        size_t len = strlen(str);
        if( (len % 6) != 0){
                return 0;
        }
        for(int itr1=0; itr1 < len; itr1+=6){
                for( int itr2=0; itr2 < 3 ; itr2++){
                        if(str[itr1+itr2] != str[itr1+5-itr2]){
                                //printf("%d:%d-%c:%c\n", itr1+itr2, itr1-itr2+5, str[itr2+itr1], str[itr1-itr2+5]);
                                return 0;
                        }
                }
        }
        return 1;
}


int main(int argc, char *argv[]){
        printf("%s is %s\n", argv[1], find_colindrome(argv[1])? "Colindrome": "Not a Colindrome");
        return 0;
}

Compile and Output


rajesh@ideapad:~/Rajesh/Blog/colindrome$ gcc colindrome.c 
rajesh@ideapad:~/Rajesh/Blog/colindrome$ ./a.out cappac
cappac is Colindrome
rajesh@ideapad:~/Rajesh/Blog/colindrome$ ./a.out abcdef
abcdef is Not a Colindrome
rajesh@ideapad:~/Rajesh/Blog/colindrome$ ./a.out abccba
abccba is Colindrome
rajesh@ideapad:~/Rajesh/Blog/colindrome$ ./a.out abccba123321
abccba123321 is Colindrome
rajesh@ideapad:~/Rajesh/Blog/colindrome$ ./a.out abccba123456
abccba123456 is Not a Colindrome

Friday 10 February 2017

Display Multiplication Table Of a Given Number in C


Multiplication Tables for a number in mathematics is multiplication with 1 to 10 to that number


/* for multiplication table of a given number */
/*
 * num    : provided number
 * table  : result multiplication table
 * size   : size of provided table to avoid out of bound for array
*/
int multiplication_table(unsigned int num, unsigned int table[] , size_t size){
        int i=0;
        for( i=0; i < 10; i++){
                table[i] = num* (i+1);
        }
        return i;
}

Complete Code


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

int multiplication_table(unsigned int num, unsigned int table[] , size_t size){
        int i=0;
        for( i=0; i < 10; i++){
                table[i] = num* (i+1);
        }
        return i;
}

int print_multiplication_table(unsigned int num, unsigned int table[], size_t size){
        int i=0;
        for( i=0; i < size; i++){
                printf("%d\tX\t%d\t=\t%d\n", num, i+1, table[i]);
        }
        return i;
}

int main(int argc, char *argv[]){
        if(argc != 2){
                fprintf(stderr, "USAGE: %s \n", argv[0]);
                return -1;
        }
        int number=0;
        number = atoi(argv[1]);
        if(number < 1){
                fprintf(stderr, "Please Enter a Positive Number\n");
                return -1;
        }
        unsigned int table[10]={0x00};
        multiplication_table(number, table, 10);
        print_multiplication_table(number, table, 10);
        return 0;
}

Compile and Output


rajesh@ideapad:~/Rajesh/Blog/math$ gcc multiplication_table.c
rajesh@ideapad:~/Rajesh/Blog/math$ ./a.out 
USAGE: ./a.out 
rajesh@ideapad:~/Rajesh/Blog/math$ ./a.out 0
Please Enter a Positive Number
rajesh@ideapad:~/Rajesh/Blog/math$ ./a.out -12
Please Enter a Positive Number
rajesh@ideapad:~/Rajesh/Blog/math$ ./a.out 12
12 X 1 = 12
12 X 2 = 24
12 X 3 = 36
12 X 4 = 48
12 X 5 = 60
12 X 6 = 72
12 X 7 = 84
12 X 8 = 96
12 X 9 = 108
12 X 10 = 120

Thursday 9 February 2017

factorial of a number using erlang


Factorial of a non negative Number in math represented as n!.
for example:-
5!= 5x4x3x2x1=120


-module(my_math).
-export([factorial/1]).

factorial(Num) when is_integer(Num) and (Num >= 0) ->
        factorial(Num, 1).
factorial(0, Res)->
        Res;
factorial(Num, Res)->
        factorial(Num -1, Num*Res).

Compile and Run


rajesh@ideapad:~/Rajesh/Blog/factorial$ erl
Erlang/OTP 18 [erts-7.3] [source] [64-bit] [smp:4:4] [async-threads:10] [kernel-poll:false]

Eshell V7.3  (abort with ^G)
1> c(my_math).
{ok,my_math}
2> my_math:factorial(5).
120
3> my_math:factorial(0).
1
4> my_math:factorial(1).
1
5> my_math:factorial(10).
3628800
6> 

valid input number from user


Question: Write a program which asks the user for a number between 1 to 9 and shows the number.
If the user inputs a number out of the specified range, the program should show an error and prompt the user for a valid input.


/*function to accept user input and validate*/
int accept_number_form_user(void)
{
        int num=0;
        do{
        printf("Enter a Number between 1 to 9 : ");
        scanf(" %d", &num);
        if(num<1 || num >9){
                printf("Please Try Again...\n");
        }
        }while(num<1 || num >9);
        return num;
}

Program For Same


#include <stdio.h>
/*function to accept user input and validate*/
int accept_number_form_user(void)
{
        int num=0;
        do{
        printf("Enter a Number between 1 to 9 : ");
        scanf(" %d", &num);
        if(num<1 || num >9){
                printf("Please Try Again...\n");
        }
        }while(num<1 || num >9);
        return num;
}
int main(int argc, char *argv[]){
        printf("You Entered: %d\n", accept_number_form_user());
        return 0;
}

above code is simplest one. but can lead to side effects like
when user input text instead of number, also mix of number and text.
Below code is handling those scenarios also.


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

void flush_stdin(void)
{
        char ch;
        do{
                ch = getchar();
        }while( ch != EOF && ch != '\n');
}

int accept_number_form_user(void)
{
        int num=0;
        char buffer[256]={0x00};
        do{
                printf("Enter a Number between 1 to 9 : ");
                scanf(" %[^\n]s", buffer);
                if(strlen(buffer) == 1){
                        sscanf(buffer," %d", &num);
                }
                if(num<1 || num >9){
                        printf("Please Try Again...\n");
                }
                flush_stdin();
                memset(buffer, 0x00, sizeof(buffer));
        }while(num<1 || num >9);
        return num;
}
int main(int argc, char *argv[]){
        printf("You Entered: %d\n", accept_number_form_user());
        char buffer[256]={0x00};
        return 0;
}

Wednesday 8 February 2017

Implement wc command in c


wc command of linux show 3 things
1. Number of Lines In the File
2. Number of Words
3. Number of characters

1. number of lines means we have to count new line characters in file


/*Finding Number of new line characters in a buffer*/
int find_count_of_lines(const char *str){
        int liness=0, i=0;
        for(i=0; str[i] != '\0'; i++){
                if( str[i] == '\n' ){
                        liness+=1;
                }
        }
        return liness;
}

2. number of words means we have to count white space characters, but if multiple white space characters will be there consecutively then we have to count then as one


/* Number of Words in a buffer*/
int find_count_of_words(const char *str){
        int words=0, i=0;
        // skip initial white space characters
        while(str[i] != '\0'){
                if( str[i] == ' ' || str[i] == '\n' || str[i]== '\r' || str[i] == '\t'){
                        i++;
                }else{
                        break;
                }
        }
        for(; str[i] != '\0'; i++){
                if( str[i] == ' ' || str[i] == '\n' || str[i]== '\r' || str[i] == '\t' ){
                        words+=1;
                        // skip consucative white space
                        while(str[i]==' ' || str[i] == '\n' || str[i]== '\r' || str[i] == '\t'){
                                i++;
                        }
                }
        }
        return words;
}

3. number of bytes is equal to number of character in a text file


int find_count_of_chars(const char *str){
    return strlen(str);
}
/*while reading of file we can get how much we read so no need to use this function*/

Code for Finding lines, words and characters in a file using above functions


int txt_my_wc(FILE *fp, int *lines_p, int *words_p, int *chars_p){
        int  lines=0, words=0, chars=0;
        char buffer[1024]={0x00};
        size_t bytes=0;
        fseek(fp, 0L, SEEK_SET);
        do{
                memset(buffer, 0x00, sizeof(buffer));
                bytes = fread(buffer, sizeof(char), sizeof(buffer)-1, fp);
                if(bytes > 0){
                        lines += find_count_of_lines(buffer);
                        words += find_count_of_words(buffer);
                        chars += bytes;
                }
        }while(bytes > 0);
        *lines_p = lines;
        *words_p = words;
        *chars_p = chars;
        return chars;
}

Full Source Code of Program


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


/*Finding Number of new line characters in a buffer*/
int find_count_of_lines(const char *str){
        int liness=0, i=0;
        for(i=0; str[i] != '\0'; i++){
                if( str[i] == '\n' ){
                        liness+=1;
                }
        }
        return liness;
}

int find_count_of_words(const char *str){
        int words=0, i=0;
        // skip initial white space characters
        while(str[i] != '\0'){
                if( str[i] == ' ' || str[i] == '\n' || str[i]== '\r' || str[i] == '\t'){
                        i++;
                }else{
                        break;
                }
        }
        for(; str[i] != '\0'; i++){
                if( str[i] == ' ' || str[i] == '\n' || str[i]== '\r' || str[i] == '\t' ){
                        words+=1;
                        // skip consucative white space
                        while(str[i]==' ' || str[i] == '\n' || str[i]== '\r' || str[i] == '\t'){
                                i++;
                        }
                }
        }
        return words;
}

int txt_my_wc(FILE *fp, int *lines_p, int *words_p, int *chars_p){
        int  lines=0, words=0, chars=0;
        char buffer[1024]={0x00};
        size_t bytes=0;
        fseek(fp, 0L, SEEK_SET);
        do{
                memset(buffer, 0x00, sizeof(buffer));
                bytes = fread(buffer, sizeof(char), sizeof(buffer)-1, fp);
                if(bytes > 0){
                        lines += find_count_of_lines(buffer);
                        words += find_count_of_words(buffer);
                        chars += bytes;
                }
        }while(bytes > 0);
        *lines_p = lines;
        *words_p = words;
        *chars_p = chars;
        return chars;
}


int main(int argc, char *argv[]){
        FILE *fp = NULL;
        int lines=0, words=0, chars=0;
        if(argc != 2){
                fprintf(stderr, "USAGE: %s <filename> \n", argv[0]);
                return -1;
        }
        fp = fopen(argv[1], "r");
        txt_my_wc(fp, &lines, &words, &chars);
        printf("  %d  %d  %d  %s\n", lines, words, chars, argv[1]);
        fclose(fp);
        return 0;
}

Compilation and Running


rajesh@ideapad:~/Rajesh/Blog/file$ gcc mywc.c -o mywc
rajesh@ideapad:~/Rajesh/Blog/file$ ./mywc 
USAGE: ./mywc 
rajesh@ideapad:~/Rajesh/Blog/file$ ./mywc mywc.c 
  72  224  2141  mywc.c
rajesh@ideapad:~/Rajesh/Blog/file$ wc mywc.c 
  72  224 2141 mywc.c

find Number of words in a text file in c


To Find Number of words in a file. we have to count white spaces available in a text file, But we have to count one for multiple white spaces, also if white spaces are there in beginning then we have to skip them all.


int find_count_of_words(const char *str){
        int words=0, i=0;
        // skip initial white space characters
        while(str[i] != '\0'){
                if( str[i] == ' ' || str[i] == '\n' || str[i]== '\r' || str[i] == '\t'){
                        i++;
                }else{
                        break;
                }
        }
        for(; str[i] != '\0'; i++){
                if( str[i] == ' ' || str[i] == '\n' || str[i]== '\r' || str[i] == '\t' ){
                        words+=1;
                        // skip consucative white space
                        while(str[i]==' ' || str[i] == '\n' || str[i]== '\r' || str[i] == '\t'){
                                i++;
                        }
                }
        }
        return words;
}

int txt_file_number_of_words(FILE *fp){
        int  words=0;
        char buffer[1024]={0x00};
        size_t bytes=0;
        fseek(fp, 0L, SEEK_SET);
        do{
                memset(buffer, 0x00, sizeof(buffer));
                bytes = fread(buffer, sizeof(char), sizeof(buffer)-1, fp);
                if(bytes > 0){
                        words += find_count_of_words(buffer) ;
                }
        }while(bytes > 0);
        return words;
}

full Program code


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

int find_count_of_words(const char *str){
        int words=0, i=0;
        // skip initial white space characters
        while(str[i] != '\0'){
                if( str[i] == ' ' || str[i] == '\n' || str[i]== '\r' || str[i] == '\t'){
                        i++;
                }else{
                        break;
                }
        }
        for(; str[i] != '\0'; i++){
                if( str[i] == ' ' || str[i] == '\n' || str[i]== '\r' || str[i] == '\t' ){
                        words+=1;
                        // skip consucative white space
                        while(str[i]==' ' || str[i] == '\n' || str[i]== '\r' || str[i] == '\t'){
                                i++;
                        }
                }
        }
        return words;
}

int txt_file_number_of_words(FILE *fp){
        int  words=0;
        char buffer[1024]={0x00};
        size_t bytes=0;
        fseek(fp, 0L, SEEK_SET);
        do{
                memset(buffer, 0x00, sizeof(buffer));
                bytes = fread(buffer, sizeof(char), sizeof(buffer)-1, fp);
                if(bytes > 0){
                        words += find_count_of_words(buffer) ;
                }
        }while(bytes > 0);
        return words;
}


int main(int argc, char *argv[]){
        FILE *fp = NULL;
        int number_of_words = 0;
        if(argc != 2){
                fprintf(stderr, "USAGE: %s <filename> \n", argv[0]);
                return -1;
        }
        fp = fopen(argv[1], "r");
        number_of_words = txt_file_number_of_words(fp);
        printf("%s having %d Words\n", argv[1], number_of_words);
        fclose(fp);
        return 0;
}

Code compilation and Running


rajesh@ideapad:~/Rajesh/Blog/file$ gcc words.c 
rajesh@ideapad:~/Rajesh/Blog/file$ ./a.out words.c 
words.c having 164 Words
rajesh@ideapad:~/Rajesh/Blog/file$ wc words.c 
  54  164 1545 words.c

Find Number Of Lines In a File


To Find Number of Lines we can Just count how many new line characters ('\n') are there in our file


/*Finding Number of new line characters in a buffer*/
int find_count_of_lines(const char *str){
        int liness=0, i=0;
        for(i=0; str[i] != '\0'; i++){
                if( str[i] == '\n' ){
                        liness+=1;
                }
        }
        return liness;
}
/* Read a File chunk by chunk and sum number of new line character in individual chunks*/
int txt_file_number_of_line(FILE *fp){
        int lines=0;
        char buffer[1024]={0x00};
        size_t bytes=0;
        fseek(fp, 0L, SEEK_SET);
        do{
                memset(buffer, 0x00, sizeof(buffer));
                bytes = fread(buffer, sizeof(char), sizeof(buffer)-1, fp);
                if(bytes > 0){
                        lines += find_count_of_lines(buffer);
                }
        }while(bytes > 0);
        return lines;
}

complete code


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

/*Finding Number of new line characters in a buffer*/
int find_count_of_lines(const char *str){
        int liness=0, i=0;
        for(i=0; str[i] != '\0'; i++){
                if( str[i] == '\n' ){
                        liness+=1;
                }
        }
        return liness;
}
/* Read a File chunk by chunk and sum number of new line character in individual chunks*/
int txt_file_number_of_lines(FILE *fp){
        int lines=0;
        char buffer[1024]={0x00};
        size_t bytes=0;
        fseek(fp, 0L, SEEK_SET);
        do{
                memset(buffer, 0x00, sizeof(buffer));
                bytes = fread(buffer, sizeof(char), sizeof(buffer)-1, fp);
                if(bytes > 0){
                        lines += find_count_of_lines(buffer);
                }
        }while(bytes > 0);
        return lines;
}

int main(int argc, char *argv[]){
        FILE *fp = NULL;
        int number_of_lines = 0;
        if(argc != 2){
                fprintf(stderr, "USAGE: %s <filename>\n", argv[0]);
                return -1;
        }
        fp = fopen(argv[1], "r");
        number_of_lines= txt_file_number_of_lines(fp);
        printf("%s having %d Lines\n", argv[1], number_of_lines);
        fclose(fp);
        return 0;
}

Compiling and Output of Program


rajesh@ideapad:~/Rajesh/Blog/file$ gcc find_lines_count.c
rajesh@ideapad:~/Rajesh/Blog/file$ ./a.out 
USAGE: ./a.out 
rajesh@ideapad:~/Rajesh/Blog/file$ ./a.out find_lines_count.c 
find_lines_count.c having 42 Lines
rajesh@ideapad:~/Rajesh/Blog/file$ wc find_lines_count.c
  42  126 1210 find_lines_count.c

Monday 6 February 2017

Concatenate two strings without using strcat function


we can implement our own strcat function


int my_strlen(const char *str){
        int len=0;
        if(str){
                while(str[len] != '\0'){
                        len++;
                }
        }

        return len;
}
char * my_strcat(char *dest, const char *src)
{
        int dest_len = my_strlen(dest);
        if( dest == NULL || src == NULL){
                return dest;
        }
        int src_itr=0;
        while(src[src_itr] != '\0'){
                dest[dest_len] = src[src_itr];
                dest_len++;
                src_itr++;
        }
        dest[dest_len] = '\0';
        return dest;
}

complete code


#include <stdio.h>
#include <string.h>
int my_strlen(const char *str){
        int len=0;
        if(str){
                while(str[len] != '\0'){
                        len++;
                }
        }

        return len;
}
char * my_strcat(char *dest, const char *src)
{
        int dest_len = my_strlen(dest);
        if( dest == NULL || src == NULL){
                return dest;
        }
        int src_itr=0;
        while(src[src_itr] != '\0'){
                dest[dest_len] = src[src_itr];
                dest_len++;
                src_itr++;
        }
        dest[dest_len] = '\0';
        return dest;
}
int main(int argc, char *argv[]){
        char str1[256]="helllo ";
        my_strcat(str1, "world!!!");
        printf("%s\n", str1);
        return 0;
}

Compilation and Output of Program


rajesh@ideapad:~/Rajesh/Blog/string$ gcc own_strcat.c
rajesh@ideapad:~/Rajesh/Blog/string$ ./a.out 
helllo world!!!

Saturday 4 February 2017

Write a program to compare two strings without using strcmp() function


We have to write our own string comparison function


int own_strcmp(const char *str1, const char *str2)
{
        int i=0;
        while(str1[i] && str2[i]){
                if( str1[i] != str2[i]){
                        break;
                }
                i++;
        }
        return str1[i]-str2[i];
}

Full Code


#include <stdio.h>
int own_strcmp(const char *str1, const char *str2)
{
        int i=0;
        while(str1[i] && str2[i]){
                if( str1[i] != str2[i]){
                        break;
                }
                i++;
        }
        return str1[i]-str2[i];
}

int main(int argc, char *argv[]){
        printf("%s and %s is %d\n", argv[1], argv[2], own_strcmp(argv[1], argv[2]));
        return 0;
}

Compilation and Output


rajesh@ideapad:~/Rajesh/Blog/string$ gcc own_strcmp.c
rajesh@ideapad:~/Rajesh/Blog/string$ ./a.out hello hello
hello and hello is 0
rajesh@ideapad:~/Rajesh/Blog/string$ ./a.out hello hello1
hello and hello1 is -49
rajesh@ideapad:~/Rajesh/Blog/string$ ./a.out hello12 hello1
hello12 and hello1 is 50

Friday 3 February 2017

Fibonacci series printing


Fibonacci Series number are follows


1,1,2,3,5,8,13,21 ... and so on


So Logic is current number = previous number + 2nd previous number



Finding number of terms using for loop


/*
 * serise variable:  used to return fibonacci numbers, provide enough
 *                   space so that index out of bound should not come.
 * */
int fibonacci_serise(int max, int serise[]){
        int i = 0;
        serise[0] = 1;
        serise[1] = 1;
        for(i=2; i < max; i++){
                serise[i] = serise[i-1] + serise[i-2];
        }
        return i;
}

Finding series up to max number using for loop


int fibonacci_serise_upto(int max_value, int serise[]){
        int i = 0;
        serise[0] = 1;
        serise[1] = 1;
        for(i=2; ; i++){
                if((serise[i-1] + serise[i-2]) <= max_value){
                        serise[i] = serise[i-1] + serise[i-2];
                }else{
                        break;
                }
        }
        return i;
}

Full Source Code

#include <stdio.h>
#include <string.h>
/*
 * serise variable:  used to return fibonacci numbers, provide enough
 *                   space so that index out of bound should not come.
 * */
int fibonacci_serise(int max, int serise[]){
        int i = 0;
        serise[0] = 1;
        serise[1] = 1;
        for(i=2; i < max; i++){
                serise[i] = serise[i-1] + serise[i-2];
        }
        return i;
}

int fibonacci_serise_upto(int max_value, int serise[]){
        int i = 0;
        serise[0] = 1;
        serise[1] = 1;
        for(i=2; ; i++){
                if((serise[i-1] + serise[i-2]) <= max_value){
                        serise[i] = serise[i-1] + serise[i-2];
                }else{
                        break;
                }
        }
        return i;
}

int main(int argc, char *argv[]){
        int serise[100] = {0x00};
        int max_term = fibonacci_serise(15, serise);
        for(int i=0; i < max_term; i++){
                if ( (max_term-1) == i){
                        printf("%d\n", serise[i]);
                }else{
                        printf("%d,",serise[i]);
                }
        }
        memset(serise, 0x00, sizeof(serise));
        max_term = fibonacci_serise_upto(100, serise);
        for(int i=0; i < max_term; i++){
                if ( (max_term-1) == i){
                        printf("%d\n", serise[i]);
                }else{
                        printf("%d,",serise[i]);
                }
        }
        return 0;
}

Compile and Out put


rajesh@ideapad:~/Rajesh/Blog/fibonacci$ gcc fibonacci_serise.c 
rajesh@ideapad:~/Rajesh/Blog/fibonacci$ ./a.out 
1,1,2,3,5,8,13,21,34,55,89,144,233,377,610
1,1,2,3,5,8,13,21,34,55,89

Thursday 2 February 2017

Find Palindrome in C


Palindrome is a sequence of characters which reads same backward or forward.

sample code for finding palindrome string


int is_palindrome_string(const char string[]){
        int left = 0, right = 0;
        int result = 1;
        right = strlen(string)-1;
        for(;left < right; left++, right--){
                if(string[left] != string[right]){
                        result = 0;
                        break;
                }
        }

        return result;
}


sample code for finding palindrome number. Using above logic


int is_palindrome_number(unsigned int number){
        char buffer[256]={0x00};
        char pale_buffer[256]={0x00};
        int len =0;
        len = snprintf(buffer, sizeof(buffer)-1, "%d", number);
        return is_palindrome_string(buffer);
}

Full Source code


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

int is_palindrome_string(const char string[]){
        int left = 0, right = 0;
        int result = 1;
        right = strlen(string)-1;
        for(;left < right; left++, right--){
                if(string[left] != string[right]){
                        result = 0;
                        break;
                }
        }

        return result;
}

int is_palindrome_number(unsigned int number){
        char buffer[256]={0x00};
        char pale_buffer[256]={0x00};
        int len =0;
        len = snprintf(buffer, sizeof(buffer)-1, "%d", number);
        return is_palindrome_string(buffer);
}


int main(int argc, char *argv[]){
        char pale[] = "madam";
        int num = 13431;
        if(is_palindrome_number(num)){
                printf("%d is Palindrome\n", num);
        }else{
                printf("%d is Not Palindrome\n", num);
        }
        if(is_palindrome_string(pale)){
                printf("%s is Palindrome\n", pale);
        }else{
                printf("%s is Not Palindrome\n", pale);
        }
        return 0;
}

Wednesday 1 February 2017

Find whether given number is prime number or not?


A positive number is a prime number only when it will be divisible by itself and 1 only.

sample function to test prime number or not


int is_prime_number(unsigned int number){
        if(number <= 1){
                return 0;
        }
        for(unsigned int devisor=2; devisor < number/2; devisor++){
                if(number % devisor == 0){
                        return 0;
                }
        }
        return 1;
}

Complete Code


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

int is_prime_number(unsigned int number){
        if(number <= 1){
                return 0;
        }
        for(unsigned int devisor=2; devisor < number/2; devisor++){
                if(number % devisor == 0){
                        return 0;
                }
        }
        return 1;
}

int main(int argc, char *argv[]){
        unsigned int num = 97;
        if( argc > 1){
                num =  atoi(argv[1]);
        }
        printf("%d is %s\n", num, is_prime_number(num) ? "prime number": "non prime numebr");
        return 0;
}

Compilation and Output


rajesh@ideapad:~/Rajesh/Blog/primenumber$ gcc primenumber.c
rajesh@ideapad:~/Rajesh/Blog/primenumber$ ./a.out 
97 is prime number
rajesh@ideapad:~/Rajesh/Blog/primenumber$ ./a.out 103
103 is prime number
rajesh@ideapad:~/Rajesh/Blog/primenumber$ ./a.out 102
102 is non prime numebr

Find Greatest Number among 10 numbers?


Traverse all elements in array, then store greater number in a variable by comparing with current element


int greatest_numers_in_array(const int numbers[], long unsigned int max_ele){
        int result=0;
        for(long unsigned int i=0; i < max_ele; i++){
                if(numbers[i] > result){
                        result = numbers[i];
                }
        }
        return result;
}

Complete code


#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define ARRAY_LENGTH(ARR) \
        (sizeof(ARR)/sizeof(ARR[0]))

int greatest_numers_in_array(const int numbers[], long unsigned int max_ele){
        int result=0;
        for(long unsigned int i=0; i < max_ele; i++){
                if(numbers[i] > result){
                        result = numbers[i];
                }
        }
        return result;
}

int main(int argc, const char *argv[]){
        int numbers[10]={0x00};
        srand(time(NULL));
        for( int i=0; i< 10; i++){
                for( int j=0; j < 10; j++){
                        numbers[j] = rand()%1000;
                }
                printf("Greatest among (%d,%d,%d,%d,%d,%d,%d,%d,%d,%d) is %d\n", numbers[0], numbers[1], numbers[2], numbers[3], numbers[4], numbers[5], numbers[6], numbers[7], numbers[8], numbers[9], greatest_numers_in_array(numbers, ARRAY_LENGTH(numbers)) );
        }

        return 0;
}

code compilation and out put


rajesh@ideapad:~/Rajesh/Blog/findgreatest$ gcc find_greatest_number.c 
rajesh@ideapad:~/Rajesh/Blog/findgreatest$ ./a.out 
Greatest among (978,235,957,270,361,360,637,514,64,558) is 978
Greatest among (635,491,917,22,547,762,78,609,7,845) is 917
Greatest among (291,277,135,181,738,581,269,845,254,262) is 845
Greatest among (943,232,849,900,502,210,261,139,77,677) is 943
Greatest among (49,64,169,318,438,716,432,869,677,440) is 869
Greatest among (714,969,69,849,150,807,431,772,4,685) is 969
Greatest among (386,947,269,235,199,771,798,460,262,227) is 947
Greatest among (490,663,291,11,981,730,79,413,951,757) is 981
Greatest among (205,665,726,274,514,228,433,297,352,789) is 789
Greatest among (982,738,736,603,974,288,374,124,748,988) is 988