Posts

Showing posts from October, 2016

C program to calculate the Sum of a series like 1+2+3+4+.....+N

#include <stdio.h> int main () { int sum, n, num; scanf("%d",&num); sum=0; for ( n=1 ;  n<= num ;    n++) sum=sum+n; printf("Result is %d", sum); }

C program for making pyramid containing N rows

#include <stdio.h> int main() {     int i, space, rows, k=0;     printf("Enter the number of rows: ");     scanf("%d",&rows);     for(i=1; i<=rows; ++i, k=0)     {         for(space=1; space<=rows-i; ++space)         {             printf("  ");         }         while(k != 2*i-1)         {             printf("* ");             ++k;         }         printf("\n");     }         return 0; }

C program for finding Fibonacci number up to N terms

//Program to Generate Fibonacci //Sequence Up to a Certain Number #include <stdio.h> int main() {     int t1=0, t2=1, nextTerm = 0, n;     printf("Enter a positive integer: ");     scanf("%d",&n);     // displays the first two terms which is always 0 and 1     printf("Fibonacci Series: %d, %d, ", t1, t2);     nextTerm = t1+t2;     while(nextTerm < n)     {         printf("%d, ",nextTerm);         t1 = t2;         t2 = nextTerm;         nextTerm = t1+t2;     }         return 0; }

C program for finding factors

#include <stdio.h> int main() {     int number,i;     printf("Enter a positive integer: ");     scanf("%d",&number);     printf("Factors of %d are: ", number);     for(i=1; i <= number; ++i)     {         if (number%i == 0)         {             printf("%d ",i);         }     }     return 0; }

C program to verify a year is leap year or not

#include <stdio.h> int main() {     int year;     printf("Enter a year: ");     scanf("%d",&year);     if(year%4 == 0)     {         if( year%100 == 0)         {             // year is divisible by 400, hence the year is a leap year             if ( year%400 == 0)                 printf("%d is a leap year.", year);             else                 printf("%d is not a leap year.", year);         }         else      ...

C program to verify a number is positive or not using else if.

#include <stdio.h> int main() {     double number;     printf("Enter a number: ");     scanf("%lf", &number);     // true if number is less than 0     if (number < 0.0)         printf("You entered a negative number.");     // true if number is greater than 0     else if ( number > 0.0)         printf("You entered a positive number.");     // if both test expression is evaluated to false     else         printf("You entered 0.");     return 0; }

C program for verifying a number is prime or not.

#include <stdio.h> int main() {     int n, i, is_prime;     printf("Enter a positive integer: ");     scanf("%d",&n); is_prime=1;     for(i=2; i<=n/2; i++)     {         // condition for nonprime number         if(n%i==0)         {             is_prime=0;         }             }     if (is_prime==1)         printf("%d is a prime number.",n);     else         printf("%d is not a prime number.",n);         return 0; }