Saturday, 9 December 2017

Control Statements Programs in C

Write a program to check whether an alphabet is vowel or consonant?
#include <stdio.h>
#include<conio.h>
void main()
{
char ch;
 clrscr();
  printf("Input a character\n");
  scanf("%c", &ch);
 if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' &&ch <= 'Z'))
 {
 If (ch=='a' || ch=='A' || ch=='e' || ch=='E' || ch=='i' || ch=='I' || ch=='o' || ch=='O' || ch== 'u' || ch=='U')
{
 printf("%c is a vowel.\n", ch);
}
   else{
   printf("%c is a consonant.\n", ch);
  }
}
  getch();
}
To check  a number is Odd or Even?

#include <stdio.h>
#include<conio.h>

void main()
{
int n;
 clrscr();
 printf("Enter an number\n");
   scanf("%d", &n);
 if (n%2 == 0){
   printf("Even\n");}
   else{
      printf("Odd\n");}
 getch();
}
To check  whether a year is  leap year or Not?

#include <stdio.h>
#include<conio.h>
void main()
{
clrscr();
  int year;
printf("Enter a year to check if it is a leap year\n");
  scanf("%d", &year);
 if ( year%4 == 0 )
   {
    printf("%d is a leap year.\n", year);
   }
   else
   {
    printf("%d is not a leap year.\n", year); 
 }
getch();
}
Calculate sum of digits of a number?
#include <stdio.h>
#include<conio.h>
void main()
{
  int num, t, sum = 0, remainder;
  clrscr();
   printf("Enter an Number\n");
   scanf("%d" ,&num);
   t = num;
  while (t != 0)
   {
      remainder = t % 10;
      sum = sum + remainder;
      t= t / 10;
   }
  printf("Sum of digits of %d\n", sum);
 getch();
}
Calculate Factorial Number?
#include <stdio.h>
#include<conio.h>
void main()
{
     int n;
   long fact = 1;
  clrscr();
  printf("Enter a number\n");
  scanf("%d", &n);
 while(n>0)
  {
      fact=fact*n;
      n--;
  }
 printf("Factorial of %d\n",fact);
 getch();
}
Swapping of two numbers

#include <stdio.h>
#include<conio.h>
void main()
{
 
  int x, y, temp;
  
  clrscr();
   
 printf("Enter the value of x and y\n");
  scanf("%d%d", &x, &y);

   printf("Before Swapping \nx=%d\n y = %d\n",x,y);

   temp = x;
   x = y;
   y = temp;

   printf("AfterSwapping\nx=%d\n y = %d\n",x,y);

getch();
}

 To find whether a number is Armstrong number or not?

#include <stdio.h>
#include<conio.h>

void  main()
 int n, sum = 0, t, remainder;
 clrscr();
 printf("\nPlease enter a number ");
  scanf("%d",&n);
  t = n;
  while( t != 0 )  {
      remainder = t%10;
      sum = sum + remainder*remainder*remainder;
      t = t/10;
   }
   if ( n == sum )
      printf("The number is an Armstrong number ");
   else
      printf("The number  is not an Armstrong number");
getch();
}
Find the Largest Number Among Three Numbers
#include <stdio.h>
#include<conio.h>

void main()
{
  int a, b, c;
 clrscr();
 printf("\nPlease enter 3 numbers:");
      scanf("%d%d%d", &a, &b, &c);
      if(a>=b && a>=c)
         printf("\nThe largest number is %d", a);
      else if(b>=a && b>=c)
         printf("\nThe largest number is %d", b);
      else
         printf("\nThe largest number is %d", c);
getch();
}
 Find square and cube of a given number.
 #include <stdio.h>
#include<conio.h>
void main()
{
int n;
 clrscr();
  printf("Enter a number");
    scanf("%d",&n);  
    printf("Square of the number %d is %d",n,n*n);
    printf("Cube of the number %d is %d",n,n*n*n);
getch();
}
 Print "Hello World" without using semi colon
#include <stdio.h>
#include<conio.h>
void main()
{
   clrscr();
   if(printf("Hello World"))
   {
 }
getch();
}
SWAPPING of 2 NUMBERS WITHOUT THIRD VARIABLE

#include <stdio.h>
#include<conio.h>
void main()
{
  int  X, Y;
 clrscr();
   printf("Please enter the 1st number : ");
   scanf("%d",&X);
   printf("\nPlease enter the 2nd number : ");
   scanf("%d",&Y);

   printf("Before swapping:\n");
   printf("X  is %d \n and Y is %d\n", X, Y);

   X = X - Y;
   Y = X + Y;
   X= Y - X;

  printf("After swapping:\n");
   printf("X is %d \n and Y is %d", X, Y);
   getch();
}

To Check Whether a Number is Prime or Not

#include <stdio.h>
#include<conio.h>
void main()
{
int i, num, pm = 0;
clrscr();
   printf("Please enter a number: \n");
   scanf("%d", &num);
   for(i=1; i<=num; i++)
   {
      if(num%i==0)
      {
         pm++;
      }
   }
   if(pm==2)
   {
      printf("Entered number is %d and it is a prime number.",num);
   }
   else
   {
      printf("Entered number is %d and it is not a prime number.",num);
   }
getch();
}
Write a program to find reverse of a number

#include<stdio.h>
#include<conio.h>
void main()
{

   int n, reverse = 0;

 clrscr();

   printf("Enter a number \n");

   scanf("%d",&n);

  while( n != 0 )

   {

      reverse = reverse * 10;

      reverse = reverse + n%10;

      n = n/10;

   }

printf("reverse no is:%d\n",reverse);
getch();
}
To check palindrome no or not?

#include<stdio.h>
#include<conio.h>
void main()

{

   int n, reverse = 0,temp;

 clrscr();

   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;

   }

 

 if ( n == reverse )

      printf("%d is a palindrome number.\n", n);

   else

      printf("%d is not a palindrome number.\n", n);

getch();



}

No comments:

Post a Comment

apply function in R

1) apply function: It takes 3 arguments matrix,margin and function.. Example: m<-matrix(c(1,2,3,4),nrow=2,ncol=2) m #1 indicates it is ap...