Example : A program in c to find out simple interest of given values using while/do-while loop with yes-no option.

#include<stdio.h>
#include<conio.h> 
int main()
{
	int p,r,t,si;
	char ch,x=1;
	while(x)
	{
		printf("\nEnter the value of P,r,t: \n");
		scanf("%d%d%d",&p,&r,&t);
		si=(p*r*t)/100;
		printf("The SI is = %d",si);

		printf("\n\nDo You want to enter another value,press Y/N = ");
		ch=getche();
		if(ch=='Y'||ch=='y')
		{
		   x=1;	
		} 
		else
		{
		   x=0;
		}
	}
return 0;
}
/*-----------------------------------  OR ----------------------------------*/

#include<stdio.h>
#include<conio.h> 
int main()
{
	int p,r,t,si;
	char ch,x=1;
	do
	{
		printf("\nEnter the value of P,r,t: \n");
		scanf("%d%d%d",&p,&r,&t);
		si=(p*r*t)/100;
		printf("The SI is = %d",si);

		printf("\n\nDo You want to enter antoher value,press Y/N = ");
		ch=getche();		

		if(ch=='Y'||ch=='y')
		{
		   x=1;	
		} 
		else
		{
		  x=0;
		}
	}while(x);
return 0;
}

Example : Write a program in c to display all the even numbers in between two numbers.

#include<stdio.h>
#include<conio.h>
void main()
{
    int i, num1, num2;    
    printf("Enter first number : ");
    scanf("%d", &num1);

    printf("Enter second number : ");
    scanf("%d", &num2);
    
    for(i=num1;i<=num2;i++)
    {       
        if(i%2==0)
        {
            printf("%d\t", i);
        }
    }    
    getch();
}

Example : Write a program in c to display all the odd numbers in between two numbers.

#include<stdio.h>
#include<conio.h>
void main()
{
     int i, num1, num2;  
    
    printf("Enter first number : ");
    scanf("%d", &num1);

    printf("Enter second number : ");
    scanf("%d", &num2);
    
    for(i=num1;i<=num2;i++)
    {       
        if(i%2!=0)
        {
            printf("%d\t", i);
        }
    }    
    getch();
}

Example : Write a program in c to count the total no. of odd and even no. present in a given range of values .

#include<stdio.h>
#include<conio.h>
void main()
{
	int min,max,i,counte=0,counto=0;
	clrscr();
	printf("Ente two no., first min value and second max value :-");
	scanf("%d%d",&min,&max);
	
	for(i=min;i<=max;i++)
	{
		if(i%2==0)
		{
		//	counte=counte+1;
			counte++;
		}
		if(i%2!=0)
		{
		//	counto=counto+1;
		    counto++;
		}
		
	}
	      printf("The total even no is %d\t",counte);
	      printf("\nThe total odd no is %d\t",counto);
		
	getch();
}

Example : Write a program in c to print the sum of natural no. up to a given value .

#include<stdio.h>
#include<conio.h>
void main()
{
    int num, i, sum = 0;
    clrscr();
    printf("Enter a positive integer: ");
    scanf("%d",&num);    
    for(i=1; i<=num; i++)
    {
        sum= sum+i;
    }
    printf("Sum of total given natural no. are = %d", sum);
    getch();
}

NB: Natural numbers are positive integers started from 1. eg- 1,2,3,4,5...

Example : Write a program in c to print the sum total between given range of values .

#include<stdio.h> 
#include<conio.h> 
void main() 
{ 
      int num1,num2,i,sum=0; 
      clrscr(); 
      printf("Enter first value :"); 
      scanf("%d",&num1);
      printf("Enter second value :"); 
      scanf("%d",&num2);
  
      for(i=num1+1;i<num2;i++) 
      { 
        sum=sum+i; 
      } 
      printf("Sum of between value is =%d",sum); 
      getch(); 
}

Example : Write a program in c to print the power/exponent result of a given base value without using function .

#include <stdio.h>
#include <conio.h>
int main()
{
    int base, exp;
    double res=1;

    printf("Enter a base number: ");
    scanf("%d", &base);

    printf("Enter a power/exponent value: ");
    scanf("%d", &exp);

    while(exp!=0)
    {
        res=res*base;
        exp--;
    }
    printf("The result is = %lf", res);

    return 0;
}

Example : Write a program in c to print the table of a given value .

#include<stdio.h>
#include<conio.h>
void main()
{
    int i, table;    
    printf("Enter a number to print table: ");
    scanf("%d", &table);
    for(i=1; i<=10; i++)
    {
        printf("%d x %d = %d\n", table, i, (table*i));
    }    
    getch();
}

Example : Write a program in c to print all the table values from a given range of values .

#include<stdio.h>
#include<conio.h>
void main()
{
   int min,max,i,j;
   clrscr();
   	printf("Enter first table value:-");
	scanf("%d",&min);
	printf("Enter second table value:-");
	scanf("%d",&max);
	
  for(i=1;i<=10;i++)
   {
     for(j=min;j<=max;j++)
          {	  
		printf("%d",i*j);
	  }
     	  printf("\n");
   }
   getch();
}

Example : Write a program in c to print the factorial of a given value .

#include<stdio.h> 
#include<conio.h> 
void main() 
{ 
     int i,num; 
     long int fact=1; 
     clrscr(); 
     printf("Enter a value for factorial:"); 
     scanf("%d",&num); 
     for(i=1;i<=num;i++) 
     { 
           fact=fact*i; 
     } 
     printf("Factorial of %d is %ld",num,fact); 
     getch(); 
} 

Example : Write a program in c to print Fibonacci series up to given number using for loop.

#include<stdio.h>
#include<conio.h>
void main()
{
	int a=0,b=1,c=0,i,n;
	clrscr();
	printf("Enter a number for fibonacci series:-");
	scanf("%d",&n);
	
	printf("%d\t%d\t",a,b);
	for(;c<=n;)
	{
		c=a+b;
		if(c<=n)
		{
		   printf("%d\t",c);
		   a=b;
		   b=c;	
		}		
	}
	getch();
}

Example : Write a program in c to print Fibonacci series up to given number using while loop.

#include<stdio.h>
#include<conio.h>
void main()
{
	int n,a=0,b=1,c=0,i;
	clrscr();
	printf("Enter a no up to which you want the series:- ");
	scanf("%d",&n);
	printf("%d\t%d",a,b);
	while(c<=n)
	{
	   c=a+b;
	   if(c<=n)
	      {
		printf("%d\t",c);
		a=b;
		b=c;	
	      }		
	}
	getch();
}

Example : Write a program in c to check that the user given single value is prime number or not.

#include<stdio.h>
#include<conio.h>
void main()
{
   int n,i,count=0;
   clrscr();
   printf("Enter a value:- ");
   scanf("%d",&n);

   for(i=1;i<=n;i++)
	{
	   if(n%i==0)
	     {
		count++;
	     }
	}

	if(count==2)
	  {
	    printf("prime no");
	  }
	else
	  {
	    printf("not a prime no");
	  }

    getch();
}

Example : Write a program in c to check that the user given single value is perfect number or not.

#include<stdio.h>
#include<conio.h>
void main()
{
	int n,i,sum=0;
        clrscr();
	printf("Enter a value:- ");
	scanf("%d",&n);

	for(i=1;i<n;i++)
	{
	   if(n%i==0)
	     {
		sum=sum+i;
	     }		
	}

	if(n==sum)
	  {
	     printf("Given no. is prefect no.");
	  }
	else
	  {
	     printf("Given no. is not perfect no.");
	  }
	
	getch();
}

Example : Write a program in c to check that the user given single value is Palindrome number or not.

#include<stdio.h>
#include<conio.h>
void main()
{
	int n,r,sum=0,temp;
        clrscr();
	printf("Enter a value:- ");
	scanf("%d",&n);
	temp=n;

	while(n>0)
	  {
	    r=n%10;	
	    sum=(sum*10)+r;
	    n=n/10;
	  }
	
	if(temp==sum)
	  {	
	    printf("The given value is Palindrome no.");
          }
        else
          {
            printf("The given value is not a Palindrome no.");
          }

        getch();
}

Example : Write a program in c to display the reverse of user given single numeric value.

#include<stdio.h>
#include<conio.h>
void main()
{
	int n,rem,sum=0;
        clrscr();
	printf("Enter a no:- ");
	scanf("%d",&n);

	while(n>0)
	{
	   rem=n%10;
	   n=n/10;
	   sum=(sum*10)+rem;		
	}

	printf("%d",sum);
        getch();
}

Example : Write a program in c to display all the Lowercase/Small letters from a to z.

#include<stdio.h>
#include<conio.h>
void main()
{
    int i;    
    printf("Small Letters from a to z are: \n");    
    for(i=97;i<=122;i++)
    {
        printf("%c\t", i);
    }     
    getch();
}

NB: Using ASCII concept.
/* -------------------------------------  OR  -----------------------------------------  */

#include<stdio.h>
#include<conio.h>
void main()
{
    char ch;
    
    printf("Lowercase Letters from a to z are: \n");
    for(ch='a'; ch<='z'; ch++)
    {
        printf("%c\t", ch);
    }
    getch();
}

Example : Write a program in c to display all the Uppercase/Capital letters from A to Z.

#include<stdio.h>
#include<conio.h>
void main()
{
    int i;    
    printf("Capital Letters from A to Z are: \n");    
    for(i=65;i<=90;i++)
    {
        printf("%c\t", i);
    }    
    getch();
}

/*-----------------------------------  OR  -------------------------------------------  */

#include<stdio.h>
#include<conio.h>
void main()
{
    char ch;    
    printf("Capital Letters from A to Z are: \n");
    for(ch='A'; ch<='Z'; ch++)
    {
        printf("%c\t", ch);
    }
    getch();
}

Example : Write a program in c to display the largest and smallest number from all the input numbers.

#include<stdio.h>
#include<conio.h>
void main()
{
    int x,i,max, min, input; 
	printf("Enter value for total entry :"); 
	scanf("%d",&x);
	printf("Enter %d values to find max and min :",x);
	scanf("%d",&input); 
	max=input;
	min=input;
	
	for(i=2;i<=x;i++) 
	{ 
		scanf("%d",&input);
		if(input>max) 
		{
			max=input;
		} 
		if(input<min)
		{
			min=input; 
		}
	} 
		printf("The Largest no. is %d\t",max);
		printf("\nThe Smallest no. is %d\t",min);
    
    getch();
}

Example : Write a program in c to display the ASCII values of all characters.

#include<stdio.h>
#include<conio.h>
void main()
{
    int i;
    clrscr();    
    for(i=0;i<=255;i++)
    {
        printf("ASCII value of %c = %d\n", i, i);
    }    
    getch();
}

Example : Write a program in c to display the sum of each digit of a given number.

#include<stdio.h>
#include<conio.h>
void main()
{
   int num, sum;
    
    printf("Enter a number: ");
    scanf("%d",&num);
    
    for(sum=0; num>0; num=num/10)
        sum=sum+(num%10);
		    
    	printf("Sum of each digit in the given no. is : %d", sum);
    
    getch();
}

Example : Write a program in c to count the no. of  digit in an accepted number with/without loop.

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

void main()
{   
    int num,count = 0;    
    printf("Enter a number: ");
    scanf("%d", &num);   
    while(num>0)
    {
        count++;        
        num = num/10;
    }
    printf("The total  digits in the given no is : %d ", count);

    getch();
}

/* -------------------------------------  OR ---------------------------------------  */
#include <stdio.h>
#include <stdio.h>
void main()
{
    int num,num1;
    int count=0;
    clrscr();
    printf("Enter an integer value: ");
    scanf("%d",&num);
    num1=num;
    while(num!=0)
    {
        num=num/10;
        count++;
    }
    printf("Total number of digits in %d is %d",num1,count);
    getch();
}
/* -------------------------------------  OR ---------------------------------------  */

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

void main()
{   
    int num,count = 0;    
    printf("Enter a number: ");
    scanf("%d", &num);
    count = log10(num)+1;    
    printf("The total  digits in the given no is : %d ", count);

    getch();
}

NB: The log10() function computes the base 10 logarithm of an argument.

Example : Write a program in c to check and display the accepted number is Armstrong or not.

#include <stdio.h>
#include <conio.h>
int main()
{
    int num,num1,rem,result=0;

    printf("Enter an integer value: ");
    scanf("%d",&num);
    num1=num;
    
    while(num1!=0)
    {
        rem=num1%10;
        result=result+(rem*rem*rem);
        num1=num1/10;
    }
    if(result==num)
        printf("%d is an Armstrong number",num);
    else
        printf("%d is not an Armstrong number",num);
    return 0;
}

NB : Armstrong number is the number whose sum of cube of each digit is similar/equal to the original number. eg-153((1*1*1)+(5*5*5)+(3*3*3))= 153 

 1,235 total views,  1 views today

Categories: C

0 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.