Posts

Showing posts from February 9, 2013

Display numbers using loops

C Programming  Loops - display numbers using for loop, while loop and do while loop The main difference between for loop, while loop, and do while loop is 1.While loop checks for the condition first. so it may not even enter into the loop, if the condition is false. 2.do while loop, execute the statements in the loop first before checks for the condition. At least one iteration takes places, even if the condition is false. 3.for loop is similar to while loop except that •initialization statement, usually the counter variable initialization •a statement that will be executed after each and every iteration in the loop, usually counter variable increment or decrement. The following sample code that can display numbers explains all 3 different loops: Source Code #include <stdio.h> int main() {       int num = 0;     printf( "Using while loop\n") ;     while (1)  

Fibonacci Series

Image
Fibonacci Series The logic for Fibonacci series very simple. It starts with 0 and 1. The next subsequent term is the sum of the previous two terms. Source Code // Program for Fibonacci Number #include <stdio.h> void main() {       int f1 = 0, f2 = 1, f3, n;       printf( "Program for Fibonacci Series\n" );       printf( "Enter the maximum number for Fibonacci Series: " );       scanf( "%d" , &n);       printf( "\nPrinting Fibonacci Series from 0 - %d\n" , n);       printf( "%d\n%d\n" , f1, f2);       while (1)       {             f3 = f1 + f2;             if (f3 > n)                   break ;             printf( "%d\n" , f3);             f1 = f2;             f2 = f3;       } }   Output Program for Fibonacci Series Enter the maximum number for Fibonacci Series: Printing Fibonacci Series from 0 - 1000 0 1 1 2 3 5 8 13 21 34

Finding Armstrong Number

Armstrong Numbers Program for finding Armstrong number of a three digits number. The condition for Armstrong number is, Sum of the cubes of its digits must equal to the number itself. For example, 407 is given as input. 4 * 4 * 4 + 0 * 0 * 0 + 7 * 7 * 7 = 407 is an Armstrong number. Source Code #include <stdio.h> int main() {       int i, a, b, c, d;       printf( "List of Armstrong Numbers between (100 - 999):\n" );       for (i = 100; i <= 999; i++)       {             a = i / 100;             b = (i - a * 100) / 10;             c = (i - a * 100 - b * 10);             d = a*a*a + b*b*b + c*c*c;             if (i == d)             {                   printf( "%d\n" , i);             }       }         return 0; } Output List of Armstrong Numbers between (100 - 999): 153 370 371 407

Calculating Slope of a Line

Calculating Slope of a Line Given Two End Points Program for Calculating Slope of a Line Given Two End Points It uses the following formula given points are (x1,y1) and (x2, y2) slope = (y2 - y1) / (x2 - x1) Source Code #include <stdio.h> #include <math.h> void main() {     float slope;     float x1, y1, x2, y2;     float dx, dy;     printf( "Program to find the slope of a line given two end points\n" );     printf( "Enter X1: " );     scanf( "%f" , &x1);     printf( "Enter Y1: " );     scanf( "%f" , &y1);     printf( "Enter X2: " );     scanf( "%f" , &x2);     printf( "Enter Y2: " );     scanf( "%f" , &y2);     dx = x2 - x1;     dy = y2 - y1;     slope = dy / dx;     printf( "Slope of the line with end points (%.4f

Finding the equation

Finding the equation of a Line Given Two End Points C program for finding the equation of a Line Given Two End Points (x1,y1) and (x2, y2) The equation for a line is Y = MX + C Where M = Slope of a Line and C = Intercept To Find the slope of a line slope = (y2 - y1) / (x2 - x1) To Find the intercept of the line, intercept = y1 - (slope) * x1 which would be same as, intercept = y2 - (slope) * x2 Source Code #include <stdio.h> #include <math.h> void main() {     float slope, intercept;     float x1, y1, x2, y2;     float dx, dy;     printf( "Program to find the equation of a line given two end points\n" );     printf( "Enter X1: " );     scanf( "%f" , &x1);     printf( "Enter Y1: " );     scanf( "%f" , &y1);     printf( "Enter X2: " );     scanf( "%f" , &x2);     printf( "Enter Y2:

Distance between the two points

C program for the Distance between the two points. It uses the following formula give points are (x1,y1) and (x2, y2) SQRT( (x2-x1) * (x2-x1) + (y2-y1) * (y2-y1) ) Source Code #include <stdio.h> #include <math.h> void main() {     float distance;     int x1, y1, x2, y2;     int dx, dy;     printf( "Program for distance between the two points\n" );     printf( "Enter X1: " );     scanf( "%d" , &x1);     printf( "Enter Y1: " );     scanf( "%d" , &y1);     printf( "Enter X2: " );     scanf( "%d" , &x2);     printf( "Enter Y2: " );     scanf( "%d" , &y2);     dx = x2 - x1;     dy = y2 - y1;     distance = sqrt(dx*dx + dy*dy);     printf( "%.4f" , distance); } Output Program for distance between the two points Ent

Sum of ODD Numbers

Sum of ODD Numbers in the Given Range   Source Code #include <stdio.h> void main() {     int index, begno, endno, sum = 0;     printf( "Program for sum of odd numbers in the given range" );     printf( "Enter Beg. No.: " );     scanf( "%d" , &begno);     printf( "Enter End. No.: " );     scanf( "%d" , &endno);     index = begno;     if ( (begno % 2) == 0) // If it even, then make it ODD         index = begno + 1;     for (; index <= endno; index += 2)     {         sum = sum + index;     }     printf( "The sum of odd numbers between %d and %d is: %d" , begno, endno, sum); } Output Program for sum of odd numbers in the given range Enter Beg. No.: 1 Enter End. No.: 100 The sum of odd numbers between 1 and 100 is: 2500

Sum of EVEN Numbers

Sum of EVEN Numbers in the Given Range Source Code #include <stdio.h> void main() {     int index, begno, endno, sum = 0;     printf( "Program for sum of even numbers in the given range\n" );     printf( "Enter Beg. No.: " );     scanf( "%d" , &begno);     printf( "Enter End. No.: " );     scanf( "%d" , &endno);     index = begno;     if ( (begno % 2) == 1) // If it ODD, then make it EVEN         index = begno + 1;     for (; index <= endno; index += 2)     {         sum = sum + index;     }     printf( "The sum of even numbers between %d and %d is: %d" , begno, endno, sum); } Output Program for sum of even numbers in the given range Enter Beg. No.: 1 Enter End. No.: 100 The sum of even numbers between 1 and 100 is: 2550  

Sum of ALL Numbers

Sum of ALL Numbers in the Given Range Source Code #include <stdio.h> void main() {     int index, begno, endno, sum = 0;     printf( "Program for sum of all numbers in the given range\n" );     printf( "Enter Beg. No.: " );     scanf( "%d" , &begno);     printf( "Enter End. No.: " );     scanf( "%d" , &endno);     index = begno;     for (; index <= endno; index ++)         sum = sum + index;         printf( "The sum of even numbers between %d and %d is: %d" , begno, endno, sum); } Output Program for sum of all numbers in the given range Enter Beg. No.: 1 Enter End. No.: 100 The sum of even numbers between 1 and 100 is: 5050  

Program for the sum of 0 to N

Program for the sum of 0 - N using the formula n (n+1) / 2. Source Code #include <stdio.h> void main() {     int N, sum = 0;     printf( "Program for sum of all numbers from 0 - N\n" );     printf( "Enter N: " );     scanf( "%d" , &N);         sum = N * (N+1) / 2;         printf( "The sum of all numbers between 0 and %d is: %d" , N, sum); } Output Program for sum of all numbers from 0 - N Enter N: 100 The sum of all numbers between 0 and 100 is: 5050