C/C++ Coding challenges

Enigma_1

Junior member
  • Feb 5, 2008
    70
    3
    8
    Colombo
    This thread is for C/C++ programming.
    I’ll give you guyz an algorithm. Give me a C/C++ implementation of it. Here goes. Say I have a number 896235716. I need to validate this. Validation algorithm is like this.


    From the last digit you start extracting. Here the last digit is 6. So you take number 6. Then you don’t take 1 but you take 7. Likewise extraction goes on.

    8 9 6 2 3 5 7 1 6

    8 6 3 7 6


    multiply all the extracted digits by 2
    8X2 = 16
    6X2 = 12
    3X2 = 6
    7X2 = 14
    6x2 = 12

    Then add all the digits together.
    1+6 + 1+2 + 6 + 1+4 + 1+2 = 24

    if the total is divisible by 10 then its a valid number.
    If 24mod10 !=0 then 896235716 is not a valid number.
    say total is 20. Then 20mod10 ==0 so the corresponding number is valid.

    Please note that validation number (896235716 in this case) can be varying in length. Extraction always starts from the last digit. If you have doubts on this let me know.

    mama example valid number ekak dennum.
    5482742451 number eka valid number ekak.

    kattiya try ekak danna. good luck.
    :rofl::rofl::rofl::rofl::rofl::rofl:
     
    Last edited:
    • Like
    Reactions: koondeGoda

    rclakmal

    Active member
  • May 8, 2008
    698
    183
    43
    In a galaxy far far away
    Code:
    class ValidTest {
    
        String number;
    
        public ValidTest(String number) {
            this.number = number;
        }
    
        public String testValid() {
            char numbers[] = number.toCharArray();
            String sumWord ="";
            int sum=0;
            for (int i = numbers.length - 1; i >= 0; i -= 2) {
                sumWord+=Integer.parseInt(Character.toString(numbers[i]))*2;
            }
            for(int k=0;k<sumWord.length();k++){
                sum+=Integer.parseInt(Character.toString(sumWord.charAt(k)));
            }
    
            if (sum % 10 == 0) {
                return "valid";
            } else {
                return "false";
            }
        }
    
        public static void main(String[] args) {
            System.out.println("Enter your Number");
            String s = new Scanner(System.in).nextLine();
            System.out.println(new ValidTest(s).testValid());
        }
    }

    C++ wadiya dannaha bro !!! But try ekka denna one !!!
     

    Enigma_1

    Junior member
  • Feb 5, 2008
    70
    3
    8
    Colombo
    seems like nobody s have a interest on this. anyway here is the code.

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    char* number =NULL;
    char temp[2];
    int finalval=0;
    int tempval =0;
    int main()
    {
        number =(char*)malloc(1000);
        printf("Please enter the number :");
        scanf("%s",number);
    
        if(number !=NULL)
        {
            number = number+(strlen(number)-1);
        }
        else
        {
            printf("\nerror on getting the number");
            return -1;
        }
    
        while(number[0] !='\0')
        {
            temp[0] =number[0];
            tempval = atoi(temp)*2;
            if(tempval>9)
            {
                finalval = (int)(tempval/10)+ tempval%10 + finalval;
            }
            else
            {
                finalval = tempval+finalval;
            }
            number = number-2;
        }
    
        if(finalval%10 ==0)
        {
            printf("\nThis is a valid number\n");
        }
        else
        {
            printf("\nThis is an invalid number\n");
        }
        system("pause");
        return 0;
    }
    tested in VS 2008

    :dull::dull:
     

    Enigma_1

    Junior member
  • Feb 5, 2008
    70
    3
    8
    Colombo
    thx machan. thanks for the encouragement. Above algorithm is use to validate the credit card numbers. I will add another algorithm like this.
    Thnkas :yes::yes:
     

    Enigma_1

    Junior member
  • Feb 5, 2008
    70
    3
    8
    Colombo
    oky onna next challenge eka. C/C++ program code ekak denna date ekak dunnama (year eka 1899 ta wada wadi date ekak) weekday eka mokakda kiyala denna one.
    for an Eg

    Please enter the date (dd-mm-yyyy) : 30-07-2010 - user input eka
    This day is Friday. - Computer response eka .

    :D:D
     

    hasiwebmail

    Member
    Feb 27, 2007
    501
    52
    0
    Sri Lanka
    oky onna next challenge eka. C/C++ program code ekak denna date ekak dunnama (year eka 1899 ta wada wadi date ekak) weekday eka mokakda kiyala denna one.
    for an Eg

    Please enter the date (dd-mm-yyyy) : 30-07-2010 - user input eka
    This day is Friday. - Computer response eka .

    :D:D



    #include<stdio.h>
    #include<conio.h>
    void main()
    {
    unsigned int days,year,date,month;
    int i,m,sum=0,j,y,od=0;
    clrscr();
    printf("\nEnter the DATE::");
    scanf("%d",&date);
    printf("\nEnter the MONTH::");
    scanf("%d",&month);
    printf("\nEnter the YEAR:: ");
    scanf("%d",&year);
    y=year-1;
    while(1)
    {
    if(y>400)
    y-=400;
    else
    break;
    }
    if(y>300)
    {
    y-=300;
    od+=1;
    }
    else if(y>200)
    {
    y-=200;
    od+=3;
    }
    else if (y>100)
    {
    y-=100;
    od+=5;
    }
    od+=(y/4)*2+(y-(y/4));
    od=od%7;
    for(m=1;m<month;m++)
    {
    switch(m)
    {
    case 1:
    case 3:
    case 5:
    case 7:
    case 8:
    case 10:
    case 12: days=31;
    break;
    case 4:
    case 6:
    case 9:
    case 11: days=30;
    break;
    case 2:
    {
    if(year%4==0 && year%100!=0 || year%400==0)
    days=29;
    else
    days=28;
    break;
    }
    }
    sum=sum+days;
    }
    sum=(sum+date)%7;
    od+=sum;
    switch(od)
    {
    case 1:printf("MONDAY");break;
    case 2:printf("TUESDAY");break;
    case 3:printf("wensdAY");break;
    case 4:printf("thusday");break;
    case 5:printf("FRIDAY");break;
    case 6:printf("SATURDAY");break;
    case 7:printf("SUNDAY");break;
    }
    getch();
    }


    dated.jpg
     
    Last edited:

    Dileepa.

    Member
    Jul 29, 2010
    153
    11
    0
    This thread is for C/C++ programming.
    I’ll give you guyz an algorithm. Give me a C/C++ implementation of it. Here goes. Say I have a number 896235716. I need to validate this. Validation algorithm is like this.


    From the last digit you start extracting. Here the last digit is 6. So you take number 6. Then you don’t take 1 but you take 7. Likewise extraction goes on.

    8 9 6 2 3 5 7 1 6

    8 6 3 7 6


    multiply all the extracted digits by 2
    8X2 = 16
    6X2 = 12
    3X2 = 6
    7X2 = 14
    6x2 = 12

    Then add all the digits together.
    1+6 + 1+2 + 6 + 1+4 + 1+2 = 24

    if the total is divisible by 10 then its a valid number.
    If 24mod10 !=0 then 896235716 is not a valid number.
    say total is 20. Then 20mod10 ==0 so the corresponding number is valid.

    Please note that validation number (896235716 in this case) can be varying in length. Extraction always starts from the last digit. If you have doubts on this let me know.

    mama example valid number ekak dennum.
    5482742451 number eka valid number ekak.

    kattiya try ekak danna. good luck.
    :rofl::rofl::rofl::rofl::rofl::rofl:

    Simple Logic - in C#.net

    capture1y.png


    Code:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    
    namespace CreditCardValidator
    {
        public partial class Form1 : Form
        {
            Int64 l, y,p,x;
            static Int64 tot=0;
            int z = 0;
            public Form1()
            {
                InitializeComponent();
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                l = Int64.Parse(txt_cno.Text);
                x = l;
               while (true)
                {
                    
                        x = x / 10;
                        y = l % 10;
                        if (z % 2 == 0)
                        {
                            if (y != 0)
                            {
                                p = y * 2;
                                Int64 x1 = p % 10;
                                Int64 x2 = p / 10;
                                tot += x1 + x2;
                            }
                        }
                        z++;
                        if (x == 0)
                            break;
    
                  }
               if (tot % 10 == 0)
                   MessageBox.Show("Valid Credit Card Number!");
               else
                   MessageBox.Show("Fuker! Enter Valid number !");
               }
            }
        }
    :P:P:P
     

    Enigma_1

    Junior member
  • Feb 5, 2008
    70
    3
    8
    Colombo
    Code:
    l = Int64.Parse(textBox1.Text);
                x = l;
               while (true)
                {
                    
                        x = x / 10;
                        y = l % 10;
                      [B][COLOR=Red]  l = x;[/COLOR][/B]
                        if (z % 2 == 0)
                        {
                            if (y != 0)
                            {
                                p = y * 2;
                                Int64 x1 = p % 10;
                                Int64 x2 = p / 10;
                                tot += x1 + x2;
                            }
                        }
                        z++;
                        if (x == 0)
                            break;
    
                  }
               if (tot % 10 == 0)
                   MessageBox.Show("Valid Credit Card Number!");
               else
                   MessageBox.Show("Fuker! Enter Valid number !");
    very nice logic machan. but thr is a small bug (I have corrected it.). check the above code. you can track the bug by entering the number 1243 . Thks for the post :yes::yes::yes:. ela ma thama
     

    Dileepa.

    Member
    Jul 29, 2010
    153
    11
    0
    Code:
    l = Int64.Parse(textBox1.Text);
                x = l;
               while (true)
                {
                    
                        x = x / 10;
                        y = l % 10;
                      [B][COLOR=Red]  l = x;[/COLOR][/B]
                        if (z % 2 == 0)
                        {
                            if (y != 0)
                            {
                                p = y * 2;
                                Int64 x1 = p % 10;
                                Int64 x2 = p / 10;
                                tot += x1 + x2;
                            }
                        }
                        z++;
                        if (x == 0)
                            break;
    
                  }
               if (tot % 10 == 0)
                   MessageBox.Show("Valid Credit Card Number!");
               else
                   MessageBox.Show("Fuker! Enter Valid number !");
    very nice logic machan. but thr is a small bug (I have corrected it.). check the above code. you can track the bug by entering the number 1243 . Thks for the post :yes::yes::yes:. ela ma thama
    Just compiled and posted machan....... not tested !!
    Thanks for the correction :yes:
     

    Enigma_1

    Junior member
  • Feb 5, 2008
    70
    3
    8
    Colombo
    #include<stdio.h>
    #include<conio.h>
    void main()
    {
    unsigned int days,year,date,month;
    int i,m,sum=0,j,y,od=0;
    clrscr();
    printf("\nEnter the DATE::");
    scanf("%d",&date);
    printf("\nEnter the MONTH::");
    scanf("%d",&month);
    printf("\nEnter the YEAR:: ");
    scanf("%d",&year);
    y=year-1;
    while(1)
    {
    if(y>400)
    y-=400;
    else
    break;
    }
    if(y>300)
    {
    y-=300;
    od+=1;
    }
    else if(y>200)
    {
    y-=200;
    od+=3;
    }
    else if (y>100)
    {
    y-=100;
    od+=5;
    }
    od+=(y/4)*2+(y-(y/4));
    od=od%7;
    for(m=1;m<month;m++)
    {
    switch(m)
    {
    case 1:
    case 3:
    case 5:
    case 7:
    case 8:
    case 10:
    case 12: days=31;
    break;
    case 4:
    case 6:
    case 9:
    case 11: days=30;
    break;
    case 2:
    {
    if(year%4==0 && year%100!=0 || year%400==0)
    days=29;
    else
    days=28;
    break;
    }
    }
    sum=sum+days;
    }
    sum=(sum+date)%7;
    od+=sum;
    switch(od)
    {
    case 1:printf("MONDAY");break;
    case 2:printf("TUESDAY");break;
    case 3:printf("wensdAY");break;
    case 4:printf("thusday");break;
    case 5:printf("FRIDAY");break;
    case 6:printf("SATURDAY");break;
    case 7:printf("SUNDAY");break;
    }
    getch();
    }


    dated.jpg

    ela kiri ne. thx machan post ekata. :yes::yes::yes::yes::yes:. mage version ekath mama dannum.
     

    croshanlg

    Member
    Jan 7, 2008
    2,665
    285
    0
    6° 54' N 79° 52' E
    CoOOoL thread bro(hope u r a bro.. :P ).. keep it up... :yes::yes:

    May I give u an idea..?? why don't you expand this thread to other languages also, like Java, VB and some scripting languages (PHP is my fav.)

    I think it will be great for all the people who stick with different kinds languages.
    :yes::yes::yes::yes::yes::yes:

    And mind you.. it will attract many people (who r doin some sort of programming or IT related studies) towards Elakiri...:nerd::nerd::nerd::nerd:


    It will definitely attract the IT students who desperately look for solutions and clues for their assignment.
    GT will be very grateful to you for new members..;);)

    + rep.. :D:D:D
     
    Last edited: