Social Icons

banner image

loops examples

FACTORIAL OF A NUMBER:

#include<conio.h>
#include<stdio.h>
int factorial(int);

 int main()
{
 //clrscr();
 int a,fact;
 
 printf("Enter the number:");
 scanf("%d",&a);
 fact=factorial(a);
 printf("\nFactorial of a number: %d",fact);
 getch();
 return 0;
}
 int factorial(int x)
 {
    int f=1,i;
    for(i=x;i>=1;i--)
     {
      f=f*i;
     }
    return(f);
 }

CALL BY VALUE AND REFERENCE:

#include<conio.h>
#include<stdio.h>
void swap(int *,int *);
main()
{
int a=10,b=20;
clrscr();
printf("Before function call, Value of a= %d and b= %d",a,b);

swap(&a,&b);

printf("\n\nAfter function call, Value of a= %d and b= %d",a,b);
getch();
}
void swap(int *x,int *y)
{
 int t;
 t=*x;
*x=*y;
 *y=t;
 printf("\n\nWithin the function, Value of x =%d and y= %d",*x,*y);

}

SWITCH CASE:
#include<conio.h>
#include<stdio.h>
main()
{
 int a;
 clrscr();
 printf("Enter a number:");
 scanf("%d",&a);
 switch(a)
 {
  case 1:
     printf("Day is monday");
     break;
  case 2:
  printf("Day is tuesday");
  break;
  case 3:
      printf("Wed");
      break;
  default:
     printf("not a valid option");
     }
     getch();
     }
PRIME NUMBER:
#include<conio.h>
#include<stdio.h>
main()
{
clrscr();
int p,flag=0;
printf("Enter a number:");
scanf("%d",&p);
for(int i=2;i<=p/2;i++)
  {
    if(p%2==0)
    {
     flag=1;
     break;
    }
 }
 if(flag==1)
  printf("Number is not a prime");
 else
   {
    printf("Number is prime");
   }
   getch();
   }
EXAMPLE 2; SWITCH CASE;
#include<conio.h>
#include<stdio.h>
main()
{
 int a,b,c,q;
 clrscr();
 printf("Enter the value of a and b:");
 scanf("%d%d",&a,&b);

 printf("1.Addition\n");
 printf("2.Subtraction\n");
 printf("3.Divide\n");
 printf("4. Multiplication\n");

 printf("Enter number...which you want to do:-");
 scanf("%d",&q);

 switch(q)
 {
  case 1:
c=a+b;
printf("Addition is: %d",c);
break;
  case 2:
c=a-b;
printf("\nSub is :%d",c);
break;
   case 3:
c=a/b;
printf("\nDivision is :%d",c);
break;
    case 4:
c=a*b;
printf("\nMultiplication is :%d",c);
break;
    default:
printf("Not a valid option") ;
    }

    getch();
    }
TO PRINT  ENGLISH ALBHABETS
#include<conio.h>
#include<stdio.h>
main()
{
int a;
clrscr();

printf("\nTo print the alphabets using ASCII values\n");
for(a=65;a<=90;a++)
 {
  printf("%c ",a);
 }
 getch();
 }
SUM OF DIGITS;
#include<conio.h>
#include<stdio.h>
main()
{
clrscr();
int a,n,sum=0;
printf("Enter a number:");
scanf("%d",&a);
while(a!=0)
{
 n=a%10;
 a=a/10;
 sum=sum+n;
}
printf("\nSum of the digits: %d",sum);
getch();
}
REVERSE OF A NUMBER
#include<conio.h>
#include<stdio.h>
main()
{
 int a,rev=0,n;
 clrscr();

 printf("Enter the value of a number:");
 scanf("%d",&a);
 while(a!=0)
 {
   n=a%10;
   a=a/10;
   rev=rev*10+n;
 }
 printf("Reverse Number is: %d",rev);
 getch();
 }
ARMSTRONG OF ANUMBER;
#include<conio.h>
#include<stdio.h>
#include<math.h>
main()
{
clrscr();
int a,n,b,sum=0;
printf("Enter a number:");
scanf("%d",&a);
b=a;
while(a!=0)
{
 n=a%10;
 n=pow(n,3);
 a=a/10;
 sum=n+sum;
 }
if(sum==b)
 printf("\nNumber is an armstrong number");
else
 printf("\nNumber is not an armstrong number");
getch();
}
MULTIPLICATION
#include<conio.h>
#include<stdio.h>
main()
{
clrscr();
int a,n,mul=1;

printf("Enter a number:");
scanf("%d",&a);
while(a!=0)
{
 n=a%10;
 a=a/10;
 mul=mul*n;
 }
 printf("\nMultipliaction: %d",mul);
 getch();
 }
FIBANOCI SERIES:
#include<conio.h>
#include<stdio.h>
main()
{
clrscr();
int a=0,b=1,c,n=2;
printf("%d  %d  ",a,b);
while(n<10)
{
 c=a+b;
 printf("%d  ",c);
 a=b;
 b=c;
 n++;
 }
 getch();
 }
loops examples loops examples Reviewed by Shobhit Goel on October 10, 2015 Rating: 5

No comments:

Airtel Hackaton 2017

Powered by Blogger.