Showing posts with label program. Show all posts
Showing posts with label program. Show all posts

Wednesday, May 16, 2018

C program to find whether the given alphabet is a vowel or consonant.

Program code
#include <stdio.h>
#include <conio.h>
void main()
{
char letter;
clrscr();
printf("Please input an alphabet.\n");
scanf("%c",&letter);
if ((letter=='a')||(letter=='e')||(letter=='i')||(letter=='o')||(letter=='u'))
printf("This alphabet is a vowel.\n");
else
printf("This alphabet is a consonant.\n");
getch();
}

Example of output
Please input an alphabet.
n
This alphabet is a consonant.

C program to check whether the given letter is small letter or capital letter.

Program code
#include <stdio.h>
#include <conio.h>
void main()
{
char letter;
clrscr();
printf("Please input an alphabet.\n");
scanf("%c",&letter);
switch(letter)
{
case 'a':printf("You have typed small letter.\n");
break;
case 'b':printf("You have typed small letter.\n");
break;
case 'c':printf("You have typed small letter.\n");
break;
case 'd':printf("You have typed small letter.\n");
break;
case 'e':printf("You have typed small letter.\n");
break;
case 'f':printf("You have typed small letter.\n");
break;
case 'g':printf("You have typed small letter.\n");
break;
case 'h':printf("You have typed small letter.\n");
break;
case 'i':printf("You have typed small letter.\n");
break;
case 'j':printf("You have typed small letter.\n");
break;
case 'k':printf("You have typed small letter.\n");
break;
case 'l':printf("You have typed small letter.\n");
break;
case 'm':printf("You have typed small letter.\n");
break;
case 'n':printf("You have typed small letter.\n");
break;
case 'o':printf("You have typed small letter.\n");
break;
case 'p':printf("You have typed small letter.\n");
break;
case 'q':printf("You have typed small letter.\n");
break;
case 'r':printf("You have typed small letter.\n");
break;
case 's':printf("You have typed small letter.\n");
break;
case 't':printf("You have typed small letter.\n");
break;
case 'u':printf("You have typed small letter.\n");
break;
case 'v':printf("You have typed small letter.\n");
break;
case 'w':printf("You have typed small letter.\n");
break;
case 'x':printf("You have typed small letter.\n");
break;
case 'y':printf("You have typed small letter.\n");
break;
case 'z':printf("You have typed small letter.\n");
break;
case 'A':printf("You have typed capital letter.\n");
break;
case 'B':printf("You have typed capital letter.\n");
break;
case 'C':printf("You have typed capital letter.\n");
break;
case 'D':printf("You have typed capital letter.\n");
break;
case 'E':printf("You have typed capital letter.\n");
break;
case 'F':printf("You have typed capital letter.\n");
break;
case 'G':printf("You have typed capital letter.\n");
break;
case 'H':printf("You have typed capital letter.\n");
break;
case 'I':printf("You have typed capital letter.\n");
break;
case 'J':printf("You have typed capital letter.\n");
break;
case 'K':printf("You have typed capital letter.\n");
break;
case 'L':printf("You have typed capital letter.\n");
break;
case 'M':printf("You have typed capital letter.\n");
break;
case 'N':printf("You have typed capital letter.\n");
break;
case 'O':printf("You have typed capital letter.\n");
break;
case 'P':printf("You have typed capital letter.\n");
break;
case 'Q':printf("You have typed capital letter.\n");
break;
case 'R':printf("You have typed capital letter.\n");
break;
case 'S':printf("You have typed capital letter.\n");
break;
case 'T':printf("You have typed capital letter.\n");
break;
case 'U':printf("You have typed capital letter.\n");
break;
case 'V':printf("You have typed capital letter.\n");
break;
case 'W':printf("You have typed capital letter.\n");
break;
case 'X':printf("You have typed capital letter.\n");
break;
case 'Y':printf("You have typed capital letter.\n");
break;
case 'Z':printf("You have typed capital letter.\n");
break;
default:printf("Invalid alphabet.\n");
break;
}
getch();
}

Example of output
Please input an alphabet.
a
You have typed small letter.

C program to print a fractional number after rounding it to two decimal places.

Program code
#include <stdio.h>
#include <conio.h>
void main()
{
float a;
clrscr();
printf("Please insert a number.\n");
scanf("%f",&a);
printf("The number you have entered is %.2f",a);
getch();
}

Example of output
Please insert a number.
7.7777
The number you have entered is 7.78

C program to swap two numbers.

Program code
#include <stdio.h>
#include <conio.h>
void main()
{
int a=0, b=0, temp=0;
clrscr();
printf(“Please input a number to store in a.\n”);
scanf(“%d”,&a);
printf(“Please input a number to store in b.\n”);
scanf(“%d”,&b);
printf(“Before swapping.\na=%d\nb=%d\n”, a,b);
/*displays the values of a and b before swapping.*/
/* swapping starts */
temp=a;
a=b;
b=temp;
/*swapping ends*/
printf(“After swapping.\na=%d\nb=%d\n”, a,b);
/*displays the values of a and b after swapping.*/
getch();
}

Example of output
Please input a number to store in a.
98
Please input a number to store in b.
34
Before swapping.
a=98
b=34
After swapping.
a=34
b=98

C program to display the reverse of the given number.


Program code
#include <stdio.h>
#include <conio.h>
void main()
{
long int temp=0, number=0, reverse=0;
clrscr();
printf(“Please input a whole number to get its reverse.\n”);
scanf(“%ld”,&number);
for (temp=number;temp>0;temp=temp/10)
reverse=(reverse*10)+(temp%10);
printf(“The reverse of %ld is %ld\n”, number, reverse);
getch();
}

Example of output
Please input a whole number to get its reverse.
6754
The reverse of 6754 is 4576

C program to find whether the given number is prime or composite


Program code
#include <stdio.h>
#include <conio.h>
void main()
{
int question=0, i=2;
clrscr();
printf(“Please input a positive integer to find out whether it is a prime number or composite number.\n”);
scanf(“%d”,&question);
if (question==1)
{
printf(“%d is neither prime nor composite.\n”, question);
goto stop;
}
if (question==2)
{
printf(“%d is a prime number.\n%d is the only even prime number.\n”,question,question);
goto stop;
}
do
{
if (question%i==0)
{
printf(“%d is a composite number.\n”,question);
goto stop;
}
i++;
}
while(i<question);
printf(“%d is a prime number.\n”,question);
stop:
getch();
}

Example of output
Please input a positive integer to find out whether it is a prime number or composite number.
3
3 is a prime number.

C program to check whether the given number is palindrome.

Program code
#include <stdio.h>
#include <conio.h>
void main()
{
long int temp=0, number=0, reverse=0;
clrscr();
printf(“Please input a whole number to find out whether it is a palindrome.\n”);
scanf(“%ld”,&number);
for (temp=number;temp>0;temp=temp/10)
reverse=(reverse*10)+(temp%10);
if (number==reverse)
printf(“%ld is palindrome.\n”,number);
else
printf(“%ld is not a palindrome.\n”,number);
getch();
}

Example of output
Please input a whole number to find out whether it is a palindrome.
1221
1221 is palindrome.

C program to display integers from one to ten.

Program code
#include <stdio.h>
#include <conio.h>
void main()
{
int i=1;
clrscr();
while (i<=10)
{
printf("%d\n",i);
i++;
}
getch();
}

Output
1
2
3
4
5
6
7
8
9
10

C program to check whether the given number is odd or even.


Program code
#include <stdio.h>
#include <conio.h>
void main()
{
int a;
clrscr();
printf("Please input a number to find whether it is odd or even.\n");
scanf("%d",&a);
if ((a%2)==0)
printf("%d is even.\n",a);
else
printf("%d is odd.\n",a);
getch();
}

Example of output
Please input a number to find whether it is odd or even.
700
700 is even.

C program to display the following pattern.


*
*    *
*    *    *
*    *    *    *
*    *    *    *    *
*    *    *    *    *    *
*    *    *    *    *    *    *

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=1;outer<=rows;outer++)
{
for (inner=1;inner<=outer;inner++)
printf(“*\t”);
printf(“\n\n”);
}
getch();
}

Example of output
How many rows do you want to see?
7
The pattern is displayed below.
*
*    *
*    *    *
*    *    *    *
*    *    *    *    *
*    *    *    *    *    *
*    *    *    *    *    *    *

C program to display the following pattern of numbers.


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

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=1;outer<=rows;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
1    2
1    2    3
1    2    3    4
1    2    3    4    5
1    2    3    4    5    6
1    2    3    4    5    6    7

C program to find the greatest among three numbers.


Program code
#include <stdio.h>
#include <conio.h>
void main()
{
int a,b,c;
clrscr();
printf("Please input three numbers to find the greatest among them.\n");
scanf("%d%d%d",&a,&b,&c);
if ((a>b)&&(a>c))
printf("%d is the greatest among the given numbers.\n",a);
else if ((b>a)&&(b>c))
printf("%d is the greatest among the given numbers.\n",b);
else if ((c>a)&&(c>b))
printf("%d is the greatest among the given numbers.\n",c);
else
printf("All numbers are equal.\n");
getch();
}

Example of Output
Please input three numbers to find the greatest among them.
777
40
95
777 is the the greatest among the given numbers.