Check vowel
#include < stdio.h >
int main()
{
char ch;
printf("Input a character\n");
scanf("%c", &ch);
switch(ch)
{
case 'a':
case 'A':
case 'e':
case 'E':
case 'i':
case 'I':
case 'o':
case 'O':
case 'u':
case 'U':
printf("%c is a vowel.\n", ch);
break;
/*if ch matches any case then it prints & breaks the execution */
default:
printf("%c is not a vowel.\n", ch);
/*if the ch is not from the cases then prints ch is not a vowel */
}
return 0;
}
Leap year
#include < stdio.h >
int main()
{
int year;
printf("Enter a year to check if it is a leap year\n");
scanf("%d", &year);
if ( year%400 == 0)
printf("%d is a leap year.\n", year);
else if ( year%100 == 0)
printf("%d is not a leap year.\n", year);
else if ( year%4 == 0 )
printf("%d is a leap year.\n", year);
else
printf("%d is not a leap year.\n", year);
return 0;
}
HCF and LCM
#include < stdio.h >
long gcd(long, long);
int main() {
long x, y, hcf, lcm;
printf("Enter two integers\n");
scanf("%ld%ld", &x, &y);
hcf = gcd(x, y);
lcm = (x*y)/hcf;
printf("Greatest common divisor of %ld and %ld = %ld\n", x, y, hcf);
printf("Least common multiple of %ld and %ld = %ld\n", x, y, lcm);
return 0;
}
/*if 1st no is 0 then 2nd no is gcd
make 2nd no 0 by subtracting smallest from largest and return 1st no as gcd*/
long gcd(long x, long y) {
if (x == 0) {
return y;
}
while (y != 0) {
if (x > y) {
x = x - y;
}
else {
y = y - x;
}
}
return x;
}
nCr and nPr
#include < stdio.h >
long factorial(int);
long find_ncr(int, int);
long find_npr(int, int);
main()
{
int n, r;
long ncr, npr;
printf("Enter the value of n and r\n");
scanf("%d%d",&n,&r);
ncr = find_ncr(n, r);
npr = find_npr(n, r);
printf("%dC%d = %ld\n", n, r, ncr);
printf("%dP%d = %ld\n", n, r, npr);
return 0;
}
long find_ncr(int n, int r)
{
long result;
result = factorial(n)/(factorial(r)*factorial(n-r));
return result;
}
long find_npr(int n, int r)
{
long result;
result = factorial(n)/factorial(n-r);
return result;
}
long factorial(int n)
{
int c;
long result = 1;
for( c = 1 ; c < = n ; c++ )
result = result*c;
return ( result );
}
Reverse number
#include < stdio.h >
int main()
{
int n, reverse = 0;
printf("Enter a number to reverse\n");
scanf("%d",&n);
while (n != 0)
{
reverse = reverse * 10;
reverse = reverse + n%10;
n = n/10;
}
/*taking unit place digit of no and moving to reverse
Dividing the no to discard unit place digit*/
printf("Reverse of entered number is = %d\n", reverse);
return 0;
}
Reverse number using array
#include < stdio.h >
void main()
{
int n, array[100], i;
printf("Enter no of digits in your number");
scanf("%d",&n);
printf("Enter no.")
for(i=0,i < n;i++)
scanf("%d",&a[i]);
/*storing each no in array*/
printf("Reverse no is : ");
for(i=n-1,i > =0;i--)
printf("%d",a[i]);
/*Printing array in reverse order*/
}
Palindrome number
#include < stdio.h >
int main()
{
int n, reverse = 0, temp;
printf("Enter a number to check if it is a palindrome or not\n");
scanf("%d",&n);
temp = n;
while( temp != 0 )
{
reverse = reverse * 10;
reverse = reverse + temp%10;
temp = temp/10;
}
/*Taking reverse of the given no
see reverse no program*/
if ( n == reverse )
/*if reverse is same as n*/
printf("%d is a palindrome number.\n", n);
else
printf("%d is not a palindrome number.\n", n);
return 0;
}
Prime numbers
#include < stdio.h >
int check_prime(int);
main()
{
int i, n, result;
printf("Enter the number of prime numbers required\n");
scanf("%d",&n);
printf("First %d prime numbers are :\n", n);
for(i=0; i < n; i++){
result = check_prime(i);
/*if i is prime then it will return 1*/
if ( result == 1 )
printf("%d \n", i);
}
return 0;
}
int check_prime(int a)
{
int c;
/*starting from 2, if no is completely divisible by any no then it is not prime*/
for ( c = 2 ; c <= a - 1 ; c++ )
{
if ( a%c == 0 )
return 0;
}
if ( c == a )
return 1;
}
Perfect Number
#include < stdio.h >
int main(){
int n,i=1,sum=0;
printf("Enter a number: ");
scanf("%d",&n);
while(i < n){
if(n%i==0)
sum=sum+i;
i++;
}
if(sum==n)
printf("%d is a perfect number",i);
else
printf("%d is not a perfect number",i);
return 0;
}
Find armstrong number
#include < stdio.h >
int main()
{
int number, sum = 0, temp, remainder;
printf("Enter an integer\n");
scanf("%d",&number);
temp = number;
/*if sum of cubes of each digit in a number is same as the number then it is called as armstrong no.*/
while( temp != 0 )
{
remainder = temp%10;
sum = sum + remainder*remainder*remainder;
temp = temp/10;
/*taking unit place digits cube and adding into sum*/
}
if ( number == sum )
printf("Entered number is an armstrong number.\n");
else
printf("Entered number is not an armstrong number.\n");
return 0;
}
These are the Concept of C programming codes We'll be glad if you share your valuable information with us regarding this topic, then it will be a golden opportunity for all of us to improve ourselves and know more. Comment below!!
Comments
Post a Comment