C++: Learning the Basics – Coding the Fibonacci Series

Situation: User types in the number of integers he/she wants to see in the Fibonacci Series. Code: #include<iostream.h> #include<conio.h> void main() { clrscr(); int prev=0, curr=1, next, series; cout<<"\n\t Enter series: "; cin>>series; cout<<prev<<endl; cout<<curr<<endl; while (series>0) { next=prev+curr; cout<<next<<endl; series=series-1; prev=curr; curr=next; } getch; } Check out this video for more:

Posted in C++

C++: Learning the Basics – Introduction to DO WHILE LOOPS

The Do-While loop is essentially an inversion of the while loop. Why is the Do-While loop important? It executes the loop statements unconditionally the first time. It then evaluates the conditional expression specified before executing the statements again. A sample code for the Do-While loop is attached below. I have also annotated the code so… Continue reading C++: Learning the Basics – Introduction to DO WHILE LOOPS

Posted in C++

C++: Learning the Basics – Playing around with the WHILE LOOP.

In the last post, I had left you with a challenge: user enters ten numbers and the program displays the largest. Here is the code for the program: Situation 4: User enters ten numbers and the program displays the largest of the lot. #include <iostream.h> #include <conio.h> void main() { clrscr(); int large=0, i=0, num; cout<<"\n\t… Continue reading C++: Learning the Basics – Playing around with the WHILE LOOP.

Posted in C++

C++: Learning the Basics – WHILE LOOP

Situation #1: The program gives the sum of the first ten numbers. You can change the number of numbers to add. #include #include void main() { clrscr(); int sum1=0, i=0; while(i<=10) #change the number of numbers to add { //cout//i//endl; sum1=sum1+i; i++; } cout<<"sum= "<<sum1; getch(); } Situation #2: User enters any ten numbers and the program… Continue reading C++: Learning the Basics – WHILE LOOP

Posted in C++

C++: Learning the Basics – Introduction to IF LOOP

Situation #1: User types in a number and the program displays whether it is odd or even. Code: #include <iostream.h> #include <conio.h> void main() { clrscr(); int num; cout<<"\n\t Enter number: "; cin>>num; if (num%2==0) { cout<<"\n\t Even "; } else { cout<<"n\t\ Odd"; } } Important notes: If and else conditions have their own brackets.… Continue reading C++: Learning the Basics – Introduction to IF LOOP

Posted in C++

N-body Simulation

What are N-body simulations? According to physics.princeton.edu, an N-body simulation replicates the motion of particles that interact with one another through some type of physical forces. These particles can range from celestial bodies to individual atoms or molecules, but for our code, we will define the particles as physical celestial bodies. Here is the code… Continue reading N-body Simulation