C/C++ Coding challenges

DURApix

Well-known member
  • Aug 1, 2010
    1,259
    192
    63
    /dev/null
    dURAs_nic2bd

    first two digits: birth year

    next three digits: birth day n sex:
    if the value is greater than 500 it is female else male
    if > 500 then reduce 500 to take the birth day.
    for ex: 80509xxxxv
    a girl born in 1980 Janu 09:

    last four digits: registration code

    hope this help

    thanx machan..
    it helped me a lot..elazz

    I solved it using java.. :yes:

    ___________________________________

    /*
    (c)2010 Aug 08
    SicodURA Presents :)

    Hidden BirthDate in NIC
    with Gender Identity.
    [2nd Release]

    Note : 'V' is optional to Enter.
    Outputs : Gender,Birthday

    DEMO
    Enter your NIC No. : 860597845V
    Boy , Your Birth date is : 86/2/28
    */

    import java.util.Scanner;

    class dURAs_nic2bd{
    public static void main(String[]args){

    int year=0,month=0,day=0;
    int flag;String Gender="";

    Scanner in=new Scanner(System.in);

    System.out.println("\n\n\tSicOdURA Presents");
    System.out.println(" Hidden BirthDate in NIC - V1.0");
    System.out.println("________________________________\n");

    System.out.print("Enter your NIC No. : ");
    String nic=in.nextLine();

    String yearof = nic.substring(0,2);
    String dates= nic.substring(2,5);
    int days = Integer.parseInt(dates);
    year = Integer.parseInt(yearof);

    if(days >= 500){
    days=days-500;flag=0;

    if (days > 500 && days <= 531) {month = 1;day = days-500;}
    if (days > 32 && days <= 60) {month = 2;day = days- 31;}
    if (days > 61 && days <= 91) {month = 3;day = days - 60;}
    if (days > 92 && days <= 121) {month = 4;day = days- 91;}
    if (days > 122 && days <= 152) {month = 5;day = days- 121;}
    if (days > 153 && days <= 183) {month = 6;day = days - 152;}
    if (days > 184 && days <= 213) {month = 7;day = days - 183;}
    if (days > 214 && days <= 244) {month = 8;day = days- 213;}
    if (days > 255 && days <= 274) {month = 9;day = days - 244;}
    if (days > 275 && days <= 305) {month = 10;day = days - 274;}
    if (days > 306 && days <= 335) {month = 11;day = days - 305;}
    if (days > 336 && days <= 366) {month = 12;day = days- 335;}
    }

    else{

    flag=1;
    if (days > 0 && days <= 31) {month = 1;day = days;}
    if (days > 32 && days <= 60) {month = 2;day = days - 31;}
    if (days > 61 && days <= 91) {month = 3;day = days - 60;}
    if (days > 92 && days <= 121) {month = 4;day = days - 91;}
    if (days > 122 && days <= 152) {month = 5;day = days - 121;}
    if (days > 153 && days <= 183) {month = 6;day = days - 152;}
    if (days > 184 && days <= 213) {month = 7;day = days - 183;}
    if (days > 214 && days <= 244) {month = 8;day = days - 213;}
    if (days > 255 && days <= 274) {month = 9;day = days - 244;}
    if (days > 275 && days <= 305) {month = 10;day = days - 274;}
    if (days > 306 && days <= 335) {month = 11;day = days - 305;}
    if (days > 336 && days <= 366) {month = 12;day = days - 335;}
    }

    if (flag==1) Gender="Boy";
    else if(flag==0) Gender="Girl";

    System.out.println("\n\n "+Gender+" , Your Birth date is : "+year + "/"+month+"/"+day+"\n\n");
    }
    }
     

    Enigma_1

    Junior member
  • Feb 5, 2008
    70
    3
    8
    Colombo
    Hey friends... it’s so nice to see the thread has gone so well. I was very interested about this thread when I first saw it. I was little busy last few days, but I’m free now & I’m looking forward to post more in this great thread. I give this very simple question as my first challenge to this thread. :yes:
    Ok. All you have to do is to build a national ID card validate & birth day extractor algorithm.
    1. Algorithm must validate a particular string is valid national ID number.
    2. If it is valid, then the algorithm must output the birthday of the person.

    INPUT ------------------------> Validate & extract b’day ---------------------> OUTPUT (b'day of ID card holder)
    (ID number)

    Guess this one will be a piece of cake for you. Anyway I’ll post the answer within next few days. Let’s see which one of us has the best algorithm. :yes:

    here is my solution for this

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    #define Aththa 1
    #define Boru 0
    
    typedef int BOOL;
    
    char* NicNumer;
    char* temp;
    int year,days,index,WeekDay;
    BOOL IsLeap = Boru;
    BOOL IsMale = Aththa;
    
    char* Week[] =  {"Sunday", "Monday","Tuesday", "Wednesday","Thursday", "Friday", "Saturday"};
    char* MonthsOfYear[] ={"January","February","March","April","May","June",
                        "July","August","September","October","November","December"};
    int DaysOfMonth[] ={31,29,31,30,31,30,31,31,30,31,30,31};
    
    
    int main()
    {
    NicNumer = (char*)malloc(10);
    temp = (char*)malloc(3);
    
    printf("please enter your NIC number :");
    scanf("%s",NicNumer);
    
    strncpy(temp,NicNumer,2);
    year = atoi(temp);
    
    if(year%4 ==0)
    {
        IsLeap =Aththa;
    }
    else
    {
        IsLeap =Boru;
    }
    
    NicNumer = NicNumer+2;
    strncpy(temp,NicNumer,3);
    days =atoi(temp);
    
    if(days<500)
    {
        IsMale = Aththa;
    }
    else
    {
        IsMale = Boru;
    }
    
    while(days>DaysOfMonth[index])
    {
        days = days-DaysOfMonth[index];
        index++;
    }
    
    if(index ==1 && IsLeap == Aththa)
    {
        days--;
    }
    
    
    WeekDay= days%7;
    
    printf("You are a ");
    if(IsMale == Aththa)
    {
        printf("boy ");
    }
    else
    {
        printf("girl ");
    }
    
    printf("and your birthday is %d %s %d\n",year,MonthsOfYear[index],days);
    printf("It was  %s\n",Week[WeekDay]);
    
    system("pause");
        return 0;
    }

    tested on vs2008 :D:D:D
     

    DURApix

    Well-known member
  • Aug 1, 2010
    1,259
    192
    63
    /dev/null
    New Challenge - Pascal Triangle

    Pascal Triangle

    onna ehenam machanlaa magen alut challenge 1k...;);)
    maths wala paskal thrikonaya mataka ati....
    anna eka gana....

    example >>>>>>

    1
    1 1
    1 2 1
    1 3 3 1
    1 4 6 4 1
    1 5 10 10 5 1
    1 ........ X ........... 1

    likewise.....user kiyana ganakata karanna oni...
    maat tama karala naha......:lol:
    try 1k deela balanna...
    elakiri.....:yes::yes::yes:
     
    Last edited:

    DURApix

    Well-known member
  • Aug 1, 2010
    1,259
    192
    63
    /dev/null
    here is my solution for this

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    #define Aththa 1
    #define Boru 0
    
    typedef int BOOL;
    
    char* NicNumer;
    char* temp;
    int year,days,index,WeekDay;
    BOOL IsLeap = Boru;
    BOOL IsMale = Aththa;
    
    char* Week[] =  {"Sunday", "Monday","Tuesday", "Wednesday","Thursday", "Friday", "Saturday"};
    char* MonthsOfYear[] ={"January","February","March","April","May","June",
                        "July","August","September","October","November","December"};
    int DaysOfMonth[] ={31,29,31,30,31,30,31,31,30,31,30,31};
    
    
    int main()
    {
    NicNumer = (char*)malloc(10);
    temp = (char*)malloc(3);
    
    printf("please enter your NIC number :");
    scanf("%s",NicNumer);
    
    strncpy(temp,NicNumer,2);
    year = atoi(temp);
    
    if(year%4 ==0)
    {
        IsLeap =Aththa;
    }
    else
    {
        IsLeap =Boru;
    }
    
    NicNumer = NicNumer+2;
    strncpy(temp,NicNumer,3);
    days =atoi(temp);
    
    if(days<500)
    {
        IsMale = Aththa;
    }
    else
    {
        IsMale = Boru;
    }
    
    while(days>DaysOfMonth[index])
    {
        days = days-DaysOfMonth[index];
        index++;
    }
    
    if(index ==1 && IsLeap == Aththa)
    {
        days--;
    }
    
    
    WeekDay= days%7;
    
    printf("You are a ");
    if(IsMale == Aththa)
    {
        printf("boy ");
    }
    else
    {
        printf("girl ");
    }
    
    printf("and your birthday is %d %s %d\n",year,MonthsOfYear[index],days);
    printf("It was  %s\n",Week[WeekDay]);
    
    system("pause");
        return 0;
    }
    tested on vs2008 :D:D:D

    elakiri machan...ube program eka mage program ekata wada godak sira...maxxa..... :yes::yes::yes:
     

    madurax86

    Member
    Jun 29, 2006
    4,385
    88
    0
    here is my solution for this

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    #define Aththa 1
    #define Boru 0
    
    typedef int BOOL;
    
    char* NicNumer;
    char* temp;
    int year,days,index,WeekDay;
    BOOL IsLeap = Boru;
    BOOL IsMale = Aththa;
    
    char* Week[] =  {"Sunday", "Monday","Tuesday", "Wednesday","Thursday", "Friday", "Saturday"};
    char* MonthsOfYear[] ={"January","February","March","April","May","June",
                        "July","August","September","October","November","December"};
    int DaysOfMonth[] ={31,29,31,30,31,30,31,31,30,31,30,31};
    
    
    int main()
    {
    NicNumer = (char*)malloc(10);
    temp = (char*)malloc(3);
    
    printf("please enter your NIC number :");
    scanf("%s",NicNumer);
    
    strncpy(temp,NicNumer,2);
    year = atoi(temp);
    
    if(year%4 ==0)
    {
        IsLeap =Aththa;
    }
    else
    {
        IsLeap =Boru;
    }
    
    NicNumer = NicNumer+2;
    strncpy(temp,NicNumer,3);
    days =atoi(temp);
    
    if(days<500)
    {
        IsMale = Aththa;
    }
    else
    {
        IsMale = Boru;
    }
    
    [COLOR="Red"]while(days>DaysOfMonth[index])
    {
        days = days-DaysOfMonth[index];
        index++;
    }[/COLOR]
    
    if(index ==1 && IsLeap == Aththa)
    {
        days--;
    }
    
    
    WeekDay= days%7;
    
    printf("You are a ");
    if(IsMale == Aththa)
    {
        printf("boy ");
    }
    else
    {
        printf("girl ");
    }
    
    printf("and your birthday is %d %s %d\n",year,MonthsOfYear[index],days);
    printf("It was  %s\n",Week[WeekDay]);
    
    system("pause");
        return 0;
    }

    tested on vs2008 :D:D:D

    *Clap* *Clap* for the part in red, nicely done!
     

    madurax86

    Member
    Jun 29, 2006
    4,385
    88
    0
    Pascal Triangle

    onna ehenam machanlaa magen alut challenge 1k...;);)
    maths wala paskal thrikonaya mataka ati....
    anna eka gana....

    example >>>>>>

    1
    1 1
    1 2 1
    1 3 3 1
    1 4 6 4 1
    1 5 10 10 5 1
    1 ........ X ........... 1

    likewise.....user kiyana ganakata karanna oni...
    maat tama karala naha......:lol:
    try 1k deela balanna...
    elakiri.....:yes::yes::yes:

    this can be done thru binomial theorem :P but it uses more resources and it is slow for this usage. Thus I use some nifty algorithm workaround with some c++ STL. This is C++, so use an appropriate compiler; I recommend g++. :D:D and oh it prints the triangle for 500 lines in a few seconds, do stress test this one.
    Code:
    #include <iostream>
    #include <vector>
    #define TYPE unsigned long int
    
    using namespace std;
    void pas(int n)
    {
        if (n>=1) cout<<"1\n"; else return;
        if (n>=2) cout<<"1 1\n";
        
        vector<TYPE> *pv=new vector<TYPE>;
        vector<TYPE> *pvw;
        vector<TYPE>::iterator iter;
        
        TYPE i, tot;
        
        pv->push_back(1);
        pv->push_back(1);
        while (n>2){
            pvw = new vector<TYPE>;
            pvw->push_back(1);
            iter=pv->begin();
            i = *iter;
            cout<<"1 ";
            for (iter++; iter!=pv->end(); iter++){
                tot = i + *iter;
                i = *iter;
                pvw->push_back(tot);
                cout<<tot<<" ";
            }
            pvw->push_back(1);
            cout<<"1\n";
            delete pv;
            pv = pvw;
            n--;
        }
    }
    int main()
    {   int inp;
        cout<<"How many levels of the pascal triangle do you want? ";
        cin>>inp;
        pas(inp);
        return 0;
    }
     

    nagaya

    Member
    Mar 18, 2007
    12,671
    194
    0
    machnla mata symbian development igena ganna one,honda tutorail tikak ehema dannwada,man me carbide C++ ide ekai sdk ekai install karanna yanne,help me bros
     

    madurax86

    Member
    Jun 29, 2006
    4,385
    88
    0
    machn image procesing gana dannwada symbian wala

    nope, but you can use any normal library that can process images but you'll have to compile it to on whatever that symbian runs. why dont you try android? it runs normal linux executables and all tools are free and cross platform:lol:
     

    DURApix

    Well-known member
  • Aug 1, 2010
    1,259
    192
    63
    /dev/null
    machan maat aasai Symbian programming igena ganna.
    ekata oni wena dewal monada? like IDEs and compilors
    ekata honda Tutorials and samples tiyena site monada?

    MADHURA ayya I need your help pls

    thanx
    dURA
     

    madurax86

    Member
    Jun 29, 2006
    4,385
    88
    0
    machan maat aasai Symbian programming igena ganna.
    ekata oni wena dewal monada? like IDEs and compilors
    ekata honda Tutorials and samples tiyena site monada?

    MADHURA ayya I need your help pls

    thanx
    dURA

    I've not done development on symbian tho, I'd recommend Android since its much better :P plus its open so it'll have more and more folks(different versions by other companies but your programs will run on them too)
    for nokia phones Qt is the way to go soon after acquiring Qt from Trolltech nokia implemented Qt in to its mobile platform. Qt is the toolkit that will let you work on most platforms without a hassle you can use almost every programming language with it.

    http://qt.nokia.com/
     
    Last edited:

    manura-prince

    Well-known member
  • Jul 28, 2007
    1,153
    199
    63
    unDer tHe heAtSinK
    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 !!!

    macho C++ wala print karanne "cout<<" kiyala. "System.out.println" kiyala nemei babo.......
    scan karanne "cin>>" kiyala baby........
    ube try eka hodai..Good...
    But ara errors tika hadaganna hode.....
    TRY AGAIN!!!!!!!!!!