Skip to main content

Simple C program

i/p  :  getting n terms
o/p :  see below example

eg:

i/p : 1 2 3 4 5 6 7 8
o/p: 1 3 5 7 2 4 6 8


#include<stdio.h>
int main(){
    int i,a[30],n,odd=0,even;
    scanf("%d",&n);
    //slight neat trick to find where even number start
    even = (n%2==0)?n/2:n/2+1;
    for(i=0;i<n;i++){
        //loop to seperate the odd index and even index
        if(i%2==0){
            scanf("%d",&a[odd++]);
        }else{
            scanf("%d",&a[even++]);
        }
    }
    for(i=0;i<n;i++)//neat mission accomplished victory
        printf("%d ",a[i]);
    return 0;
}

Comments

  1. On seeing the question, was thinking in the perspective of getting the input, and then processing it as per the need. But your solution does the job as it gets input. Superb implementation! 👌🏻

    ReplyDelete
  2. This way you can save half the space by only storing the numbers in either odd or even places and directly printing the others. :D
    Could prove useful when the input numbers don't need to be stored.

    #include
    #include
    using std::vector;
    using std::cout;
    using std::cin;

    int main() {
    vector array;
    bool index = 0;
    int n, temp;
    cin>>n;
    for(int i=0 ; i>temp;
    if(index){
    array.push_back(temp);
    } else {
    cout<<temp<<' ';
    }
    index = !index;
    }
    for(int i=0; i<array.size() ; i++){
    cout<<array.at(i)<<' ';
    }
    return 0;
    }

    ReplyDelete

Post a Comment

Popular posts from this blog

Vim - Text Editor which last for Decade

what's Vim?     Vim is a highly configurable text editor for efficiently creating and changing any kind of text. It is included as "vi" with most UNIX systems and with Apple OS X                                                                                                            ---> From Vim.org  when i first heard it, what a command line editor which is awesome and i said to myself  NO WAY, there are tons of editor which looks good and easy learn curve such as Atom,Sublime,VSCode and bunch others What makes vim special than other editors?  Different from everything you have used before ( because it has modes - insert mode,visual mode,Command mode ) Forget the mouse ( why?...

Demystify - Linux GUI

GUI In Linux GUI ( Graphics User Interface ) as everyone know about.  I am writing this article so that we can able to understand how to run GUI apps in containers but we need to understand how it works in linux. Why linux?   Most of the container we use are Linux based inorder run GUI in Linux we need know how it works.. Back in early days computer fill the entire room and if you want to access it you will be presented TTY (TeleType Machine) you can still see this screen if you press CTRL + ALT + F1 in Linux. ( To get back to GUI press CTRL + ALT + F7 ) Linux spin off 8 TTY when it boots ( we can configure more or less ) Graphics in linux is handled by bunch of little programs. They are Display manager Display manger which is the key component for graphics in which mainly graphics servers lie in linux the X.org is the defacto of Display manger. which has two components X Server  X Client Here little twist server talks to the client ( o...

My First Open Source Contribution

It's my dream to contribute to open source. It is all started in 2 year of college i came to know the linux is so much awesome to work and learn about computer and know about github where you can find the open source software code. I thought, I need to learn more to contribute to the software community later i read lot of article about contributing to opensource. Every article say Learn a programming language you like. Find a project written in programming language you choose. Read it. Use it. Find any bug open a issue. Fix the issue Seems like simple as pie. I thought let's give it a try My scenario I use C to solve program in hackerrank and other coding sites and i know little bit about C++ and java at the time. when i searched for the project, all project are seems to be dinosaur huge, Finally i convinced myself and pick a project. I started reading the source code. It donot understand lot of code and i donot know where to find those answers. then frus...