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:
Month: July 2017
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
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.
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
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
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:
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… 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… Continue reading N-body Simulation