Monday, July 30, 2018

C PROGRAM TO ARRANGE THE GIVEN NUMBERS IN ORDER


STATEMENT:

A set of numbers of size N which are separated by one or more spaces will be passed as input. The program should print the prime numbers first followed by odd numbers and finally even numbers.
Each of these categories, prime numbers, odd numbers and even numbers must be sorted in ascending order among themselves. The numbers which are prime must be excluded from the list of odd and even numbers (In the case of even numbers only 2 is prime as well as even)

PROGRAM:

#include <stdio.h>
#include <math.h>

struct NumberItem
{
    int val;
    int alreadyprinted;
};

int isPrime(int num);

int main()
{
    char input[200];
    fgets(input,200,stdin);
    char *ptr = input;
    int offset=0;
    int currentNumber;

    int  N=0;
    while(sscanf(ptr,"%d%n",&currentNumber,&offset) == 1)
    {
        N++;
        ptr+=offset;
    }

    struct NumberItem values[N];

    int index=0;
    ptr = input;
    while(sscanf(ptr,"%d%n",&currentNumber,&offset) == 1)
    {
        values[index].val = currentNumber;
        values[index].alreadyprinted = 0;
        ptr+=offset;
        index++;
    }

    //Sort the numbers
    index = 0;
    while(index < N-1)
    {
        int compareindex=index+1;
        while(compareindex < N)
        {
            if(values[index].val > values[compareindex].val)
            {
                int temp = values[index].val;
                values[index].val = values[compareindex].val;
                values[compareindex].val = temp;
            }
            compareindex++;
        }
        index++;
    }


    //Now print prime
    index = 0;
    while(index < N)
    {
        if(isPrime(values[index].val))
        {
            printf("%d ",values[index].val);
            values[index].alreadyprinted = 1;
        }
        index++;
    }

    //Now print odd
    index = 0;
    while(index < N)
    {
        if(!values[index].alreadyprinted)
        {
            if(values[index].val%2 == 1)
            {
                printf("%d ",values[index].val);
                values[index].alreadyprinted = 1;
            }

        }

        index++;
    }


    //Now print even
    index = 0;
    while(index < N)
    {
        if(!values[index].alreadyprinted)
        {
            if(values[index].val%2 ==  0)
            {
                printf("%d ",values[index].val);
                values[index].alreadyprinted = 1;
            }

        }

        index++;
    }

}

int isPrime(int num)
{
    if(num < 2)
    {
        return 0;
    }

    if(num == 2)
    {
        return 1;
    }

    int  square_root = (int) sqrt((double)num);
    int counter=2;
    while(counter <= square_root)
    {
        if(num%counter == 0)
        {
            return 0;
        }

        counter++;
    }

    return 1;

}



Input:
611953 494147 493137 493133 493138

Output:
493133 494147 611953 493137 493138

Explanation:
493133 494147 611953 are prime numbers.

Saturday, July 28, 2018

C PROGRAM TO CREATE A STRONG PASSWORD FOR SECURITY PURPOSE


PROGRAM TO CREATE A STRONG PASSWORD FOR EMPLOYEE SECURITY:

STATEMENT:

a security committee decided to enforce the following rules when an employee creates/changes his/her password.
- The password must contain atleast one special character among # ! _ $ @
- The password must contain atleast two numbers
- The password must contain atleast one upper case alphabet and one lower case alphabet.
- The password must have a minimum length of 8.
- The password must have a maximum length of 25.

The program must accept a given password string P as input and check for these rules and output VALID or INVALID.

PROGRAM;

#include <stdio.h>

#include <regex.h>

int isValid(char *regex, char *inputpassword);

int main()
{
 
 char input[50];

    scanf("%s",input);


    char REGEX_SPECIAL[20] = "[#!_$@]+";

    char REGEX_NUMBERS[20] = ".*[0-9]+.*[0-9]+";

    char REGEX_SMALLER[20] = "[a-z]+";

    char REGEX_UPPER[20] = "[A-Z]+";

    int len = strlen(input);

    int INVALID=0;

    if(len < 8 || len > 25)
    {

        INVALID=1;
    }

    if(isInvalid(REGEX_SPECIAL,input))
    {

        INVALID=1;
    }



    if(isInvalid(REGEX_NUMBERS,input))
    {

        INVALID=1;
    }

    if(isInvalid(REGEX_SMALLER,input))
    {

        INVALID=1;
    }


    if(isInvalid(REGEX_UPPER,input))
    {

        INVALID=1;
    }


    if(INVALID)
    {

        printf("INVALID");
    }
    else
    {

        printf("VALID");
    }


}

int isInvalid(char *REGEX, char *inputpassword)
{

    regex_t reg;

    int regexcompileerror = regcomp(&reg, REGEX,REG_EXTENDED);

    if(regexcompileerror)

    {

        printf("Error in regex.");

        return 1;
    }


    if (regexec(&reg, inputpassword, 0, NULL, 0) == REG_NOMATCH)
    {

        return 1;

    }

    else
    {

        //Valid as matching the criteria

        return 0;
    }
}


Input:
m@d31nindia

Output:
INVALID

Explanation:
No alphabet in uppercase.

CREATING A SIMPLE GAME USING C PROGRAM


PROGRAM TO CREATE A GAME IN WHICH A BETTING GAME IS DEVELOPED BY ROLLING A DICE:


STATEMENT:

In a betting game involving the roll of a dice, Rupesh gains Rs.X if an odd number turns up and he loses Rs.Y is an even number turns up. The numbers shown on the face of the dice in a certain number of games is passed as input. The values of X and Y are also passed as input. The program must print the net gain or loss as the output.


PROGRAM:

#include <stdio.h>

int main()

{

char input[1000];

int gainAmount,lossAmount;

fgets(input,1000,stdin);
   
scanf("%d%d",&gainAmount,&lossAmount);

char* ptr=input;

int offset=0;

int currentDiceNumber;

    int finalAmount=0;
     
        while(sscanf(ptr,"%d%n",&currentDiceNumber,&offset) == 1)

        if(currentDiceNumber%2 == 1){

            finalAmount+=gainAmount;

        }else{

            finalAmount-=lossAmount;

        }

        ptr+=offset;


 
    printf("%d",finalAmount);

}


Input:
1 4 3
10
30

Output:
-10

Explanation:
He gains 20 rupees for 1 and 3 and loses 30 rupees for 4. Hence there is a net loss of 20-30 = -10

Tuesday, July 24, 2018

SIMPLE C PROGRAMMING PART 2 FOR BEGINNERS


PROGRAM TO CHECK WHETHER THE INPUT YEAR GIVEN IS A LEAP YEAR OR NOT;

STATEMENT:

A year Y will be passed as input. The program must find if the given year is a leap year or not.
- If it is leap year, the program must print yes else it should print no

PROGRAM:


#include<stdio.h>

#include <stdlib.h>

int main()

{

    int year;

    scanf("%d",&year);
 
    if(year%400 == 0){

        printf("yes");

    }

    else

    if(year%100 == 0){

        printf("no");

    }

    else

    if(year%4 == 0){

        printf("yes");

    }

    else

    {

        printf("no");

    }

}

 Input/Output:

If 2000 is the input, the program must print yes
If 2100 is the input, the program must print no
If 2013 is the input, the program must print no



PROGRAM TO CONVERT THE GIVEN RUPEE(INDIAN CURRENCY) TO PAISE:


STATEMENT:

A floating point value F indicating the amount in rupees is passed as input. The program must print the corresponding value in paise.

PROGRAM:


#include <stdio.h>

int main()

{
 
int rupee,paise;

    scanf("%d.%d",&rupee,&paise);

    int result = rupee*100 + paise;

    printf("%d",result);           

 
}

INPUT:

11.30

OUTPUT:

1130

Wednesday, July 18, 2018

CONTACT



CONTACT :


Feel free to contact us for any help and queries

Facebook

Instagram

Twitter

ABOUT



ABOUT

I am an engineering student along with  programming skills , I post various programs in different programming languages to get you skilled and solve different program and succeed in life.
MY HEARTY WISHES TO EVERYONE..
Feel free to contact me for any queries and information.:


PRIVACY POLICY


PRIVACY POLICY:
” We do not share personal information with third-parties nor do we store information we collect about your visit to this blog for use other than to analyze content performance through the use of cookies, which you can turn off at anytime by modifying your Internet browser’s settings. We are not responsible for the republishing of the content found on this blog on other Web sites or media without our permission. This privacy policy is subject to change without notice. “
Cookies : 
Cookies are files with small amount of data that is commonly used an anonymous unique identifier. These are sent to your browser from the website that you visit and are stored on your computer’s hard drive.
Our website uses these “cookies” to collection information and to improve our Service. You have the option to either accept or refuse these cookies, and know when a cookie is being sent to your computer. If you choose to refuse our cookies, you may not be able to use some portions of our Service.

Links to Other Sites
Our Service may contain links to other sites. If you click on a third-party link, you will be directed to that site. Note that these external sites are not operated by us. Therefore, we strongly advise you to review the Privacy Policy of these websites. We have no control over, and assume no responsibility for the content, privacy policies, or practices of any third-party sites or services.
Contact Us
If you have any questions or suggestions about our Privacy Policy, do not hesitate to contact us.

Tuesday, July 17, 2018

SIMPLE SAMPLE PROGRAMS FOR C BEGINNERS



SAMPLE PROGRAM -1

PROGRAM TO PRINT WHETHER THE INPUT GIVEN IS A SQUARE OR RECTANGLE:



STATEMENT:

The length L and breadth B of a rectangle is passed as input. If L is equal to B, the program must print SQUARE. Else the program must print RECTANGLE.

PROGRAM:

#include<stdio.h>

#include <stdlib.h>

int main()

{   

int length,breadth;   

scanf("%d%d",&length,&breadth);   

if(length == breadth)

{       

printf("SQUARE");   

}

else   

{       
printf("RECTANGLE"); 

  }

}

Input:
22
22

Output:

SQUARE



SAMPLE PROGRAM -2

PROGRAM TO PRINT THE REVERSE ORDER FROM 1 TO N:

STATEMENT:

A number N is passed as the input. The program must print the numbers from 1 to N with each number separated by a space.

PROGRAM;


#include <stdio.h>

int main()

{

    int X,Y;

    scanf("%d%d",&X,&Y);

    while(X!=Y)

    {

        if(X > Y)

        {

            X = X-Y;

        }

        else

        {

            Y = Y-X;

        }

    }

    /* You can also print Y as both X and Y will be equal at this step */
 
printf("%d",X);
}

Input:
10

Output:
1 2 3 4 5 6 7 8 9 10