Wednesday, May 16, 2018

C program to display Fibonacci series


C program to display Fibonacci series
Program code
#include <stdio.h>
#include <conio.h>
void main()
{
long int i, a1=0, a2=1, a3=1, n=1;
clrscr();
printf(“We are going to display a Fibonacci series.\nPlease input the number of terms you want to see in the Fibonacci series.\n”);
scanf(“%ld”, &n);
printf(“The Fibonacci series is displayed below:\n”);
if (n>=1)
printf(“%ld\,\t”,a1);
if (n>=2)
printf(“%ld\,\t”,a2);
if (n>2)
for (i=3;i<=n;i++)
{
 a3=a1+a2;
printf(“%ld\,\t”,a3);
a1=a2;
a2=a3;
}
getch();
}

Example of output
We are going to display a Fibonacci series.
Please input the number of terms you want to see in the Fibonacci series.
15
The Fibonacci series is displayed below:
0,         1,         1,         2,         3,         5,         8,         13,       21,       34,       55,       89,       144,           233,           377

C program to display all even numbers between one and ten.


Program code
#include <stdio.h>
#include <conio.h>
void main()
{
int i;
clrscr();
printf(“The even numbers between one and 10\n”);
for (i=2;i<=10;i=i+2)
printf(“%d\n”,i);
getch();
}

Output
The even numbers between one and 10
2
4
6
8
10

C program to display all even numbers from one to the given number.


Program code
#include <stdio.h>
#include <conio.h>
void main()
{
int n=0, i;
clrscr();
printf(“If you input an integer, we will display all even numbers from one to that number.\n”);
scanf(“%d”,&n);
printf(“All even numbers from one to %d are given below.\n”,n);
for (i=1;i<=n;i++)
if ((i%2)==0)
printf(“%d\n”,i);
getch();
}

Example of output
If you input an integer, we will display all even numbers from one to that number.
22
All even numbers from one to 22 are given below.
2
4
6
8
10
12
14
16
18
20
22







C program to display the following pattern of numbers.


1    2    3    4    5    6    7
1    2    3    4    5    6
1    2    3    4    5
1    2    3    4
1    2    3
1    2
1

Program code
#include <stdio.h>
#include <conio.h>
void main()
{
int inner, outer, rows=1;
clrscr();
printf(“How many rows do you want to see? \n”);
scanf(“%d”, &rows);
printf(“The pattern is displayed below.\n”);
for (outer=rows;outer>=1;outer--)
{
for (inner=1;inner<=outer;inner++)
printf(“%d\t”,inner);
printf(“\n\n”);
}
getch();
}

Example of output
How many rows do you want to see?
7
The pattern is displayed below.
1    2    3    4    5    6    7
1    2    3    4    5    6
1    2    3    4    5
1    2    3    4
1    2    3
1    2
1








C program to check whether a given number is Armstrong number or not.


If  the sum of, each digit raised to the power of number of digits in the number, is equal to the number itself, then the number is called Armstrong number.
For example, 153=(13)+(53)+(33) . So 153 is an Armstrong number.

Program code
#include <stdio.h>
#include <conio.h>
#include <math.h>
void main()
{
/*To check whether a number is Armstrong number.*/
long int number=0, check=0, temp=0, power=0, temp1, temp2;
clrscr();
printf("Please input a whole number to find out whether it is Armstrong number.\n");
scanf("%ld",&number);
temp=number;
/* Steps to find the number of digits start*/
temp1=0;
temp2=10;
while (temp2<pow(10,7))
{
power++;
if ((temp>temp1)&&(temp<temp2))
break;
if (temp1==0)
temp1=temp1+10;
else temp1=temp1*10;
temp2=temp2*10;
}
/* Steps to find the number of digits ends*/
/*Steps to calculate check start*/
for (temp=number;temp>0;temp=temp/10)
check=check+pow((temp%10),power);
/*Steps to calculate check ends*/
if (check==number)
printf("%ld is an Armstrong number.\n", number);
else
printf("%ld is not an Armstrong number.\n", number);
getch();
}

Example of output
Please input a whole number to find out whether it is Armstrong number.
153
153 is an Armstrong number.

Saturday, April 28, 2018

C program to find the factorial of a number


Program code
#include <stdio.h>
#include <conio.h>
void main()
{
long int question, factorial=1, i;
clrscr();
printf(“Please input an integer to get its factorial.\n”);
scanf(“%ld”,&question);
for (i=question;i>=1;i--)
factorial=factorial*i;
printf(“The factorial of %ld is %ld.\n”,question,factorial);
getch(); 
}

Example of output
Please input an integer to get its factorial.
13
The factorial of 13 is 1932053504.

C program to display the sum of all integers from one to 100

Program code
#include <stdio.h>
#include <conio.h>
void main()
{
int i, sum=0;
clrscr();
for (i=1;i<=100;i++)
sum=sum+i;
printf(“The sum of all integers from one to 100 is %d.\n”,sum);
getch();
}

Output
The sum of all integers from one to 100 is 5050.

Friday, April 27, 2018

C program to perform mathematical operations


Program code
#include <stdio.h>
#include <conio.h>
void main()
{
 int num1, num2, option;
 clrscr();
 printf(“Please input two integers.\n”);
 scanf(“%d\n%d”,&num1,&num2);
 printf(“1. Add +\n2. Subtract -\n3. Multiply X\n4. Divide /\nPlease enter the number that is given near the operation you want to perform, in the above menu.\n”);
scanf(“%d”,&option);
switch(option)
{
case 1:printf(“%d + %d = %d”,a,b,a+b);
break;
case 2:printf(“%d - %d = %d”,a,b,a-b);
break;
case 3:printf(“%d X %d = %d”,a,b,a*b);
break;
case 4:printf(“%d / %d = %d”,a,b,a/b);
break;
default:
printf(“Invalid option”);
break;
}
getch();
}

Example of output
Please input two numbers.
67
78
1. Add +
2. Subtract –
3. Multiply X
4. Divide /
Please enter the number that is given near the operation you want to perform, in the above menu.
1
67 + 78 = 145

Thursday, April 26, 2018

C program to check whether a number is positive, negative or zero.

Program code
#include <stdio.h>
#include <conio.h>
void main()
{
  int num;
  clrscr();
  printf("Please input a number.\n");
  scanf("%d", &num);
  if (num= = 0)
  printf("%d is zero.",num);
  else if (num>0)
  printf("%d is positive.",num);
  else
  printf("%d is zero.",num);
  getch();
}

Example of Output
Please input a number.
13
13 is positive.



C program to display "Hello, World!"

Program code
#include <conio.h>
#include <stdio.h>
void main()
{
 clrscr();
 printf("Hello, World!");
 getch();
}

Output
Hello, World!