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: Advertisements Continue reading C++: Learning the Basics – Coding the Fibonacci Series

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 that a newbie can get a better understanding. Situation #1: … Continue reading C++: Learning the Basics – Introduction to DO WHILE LOOPS

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 Enter a number: “; cin>>large; while (i<9) { cout<<“\n\t Enter … Continue reading C++: Learning the Basics – Playing around with the WHILE LOOP.

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 displays the sum. #include #include void main() { clrscr(); int … Continue reading C++: Learning the Basics – WHILE LOOP

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.   Situation 2: User types two numbers and the program … Continue reading C++: Learning the Basics – Introduction to IF LOOP

C++: Learning the Basics – Multiplying three numbers

Situation: User can insert three numbers and the program displays their product! Code: #include #include void main() {          clrscr(); int num1, num2, num3, product; cout<>num1 cout<>num2 cout<>num3 product=num1*num2*num3; cout<<”\n\t Product of the three numbers: “ <<product; } Interactive video to learn the code and see how it runs: Continue reading C++: Learning the Basics – Multiplying three numbers

C++: Learning the Basics – Adding numbers

Situation #1: User types any two numbers and the program generates the sum of the two.

Here is the code:
This code is annotated for your reference. The final code (without the notes) is at the bottom of this post.

#include  #Input output stream
#include 
void main() #Entry of execution of pgm

{ #Start the code

clrscr(); #Clear the screen

int num1, num2, sum; #int referes to integer

cout<<”\n\t Enter first number: “; #Instructions for user; allows the user to insert data
cin>>num1 #Accepts values from user

cout<<”\n\t Enter second number: “; #\n\t represents new line of code
cin>>num2

sum=num1+num2 

cout<<”\n\t Sum of the two numbers: " <<sum;

}

Final code:

#include 
#include  
void main()

{   
      
clrscr();

int num1, num2, sum;

cout<<”\n\t Enter first number: “; 
cin>>num1

cout<<”\n\t Enter second number; “; 
cin>>num2

sum=num1+num2

cout<<”\n\t Sum of the two numbers: ‘ <<sum;

{

Continue reading “C++: Learning the Basics – Adding numbers”

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 for Python: 3-Body Simulation: from visual import* R= 6.378e6 G=6.67e-11 … Continue reading N-body Simulation