Subscribe for more such posts..

Dear Reader...
I hope u are like my posts. If so then please don't forget to like , share and follow my blog for more such posts ,also feel free to write suggestions in the comment box given over here.

Thank you...

Search This Blog

C program to convert a Decimal number to Binary number.


 #include <stdio.h>
int main()
{
	 int n , rem  , i = 1 , bn = 0 ;
	printf("Enter a positive decimal number : ");
	scanf("%d" , &n);
	
	printf("The binary number of decimal %d is : " , n);
	
	while (n > 0)
	
	{
		rem = n % 2 ;
     	bn +=  (i * rem);
		i = i * 10 ;
		n = n / 2;
		
	}
	
	 printf("%d\n",bn);
	
	return 0 ;
}



C program to find the GCD of two numbers using Recursion.



 #include <stdio.h>
int GCD(int a , int b)
{
	
	
	if (b != 0)
	{
	return GCD(b , a%b );
	
	}
	else
	{
		return a ;
	}
	
	
}
 
int main ()
{
	int n1 , n2 ;
	printf("Enter two integer values : ");
	scanf("%d %d" , &n1 , &n2);
	printf("The gcd of two numbers %d and %d = %d\n" , n1 , n2 , GCD(n1, n2));
	
return 0 ;
}


C program to find the sum of digits of a 5 digit number using recursion.

#include <stdio.h>
int fun(int n)
{
	int i = n % 10 ;
	if (n!=0)
	return i + fun(n/10);
	else 
	return 0 ;
}
int main()
{
	int num;
	printf("Enter any number to find sum of digits: ");
	scanf("%d" , &num);
	printf("Sum of digits of %d = %d" , num , fun(num));
	
	return 0 ; 
}



C program to find the Factorial of a given number using Recursion.



 #include <stdio.h>
int fact(int n )
{
	if(n >= 1)
	return n * fact(n - 1);
	else
	return 1 ;

}



int main()
{
	
	int num;
	printf("Enter an integer : ");
	scanf("%d" , &num);
	printf("Factorial of %d is : %d\n" , num , fact(num));
	
	return 0 ;
	
}

C program to find the sum of the fibonacci series using function.


 

#include <stdio.h>
long double func (int r)
{
	long double first = -1 , second = 1 , sum = 0 , third;
	
	for (int i = 0 ; i < r ; i ++)
	{
		third = first + second ;
		sum = sum + third ;
		first = second;
		second = third ;
	}
	
	return sum ;
	
}


int main()
{
	int range ;
	printf("Enter the range: ");
	scanf("%d" , &range);
	

	printf("Sum of Fibonacci series for given range is: %.0Lf\n" , func(range));
	
	return 0 ;
	
}

C program to find the Factorial of a given number using Functions.



 #include <stdio.h>
int fact(int n)
{
	int facto = 1 ;
	for ( int i = n ; i > 0 ; i--)
	{
	
		facto  = facto * i ;
		
	}
	return facto ;
}

int main ()
{
	int num ;
	printf("Enter a number : ");
	scanf("%d" , &num);
	
	printf("Factorial of a given number %d = %d\n" , num , fact(num) );
	
	return 0 ;
}


Programme in C using Function that returns the sum of all the odd digits.


 #include <stdio.h>

int sneha(int n)
{ int sum = 0 ; while(n != 0) { int rem = n % 10 ; if(rem % 2 != 0) { sum = sum + rem ; } n = n / 10 ; } return sum ; } int main() { int num ; printf("Enter any positive number: "); scanf("%d" , &num); printf("All odd digits sum is: %d\n" , sneha(num)); }


C Program to print the given pattern.

#include <stdio.h>
void main()
{
	int row , i , j;
	
	printf("Enter the number of rows: ");
	scanf("%d" , &row );
	
	for (i = 1 ; i <= row ; ++i )
	{ 
		for ( j = 1 ; j <= i ; ++j)
		{
		printf("* ");
		}
		printf("\n");
	}
	 }

USER OUTPUT :


C Program to check an armstrong number.



 #include <stdio.h>
int main()
{
	int  number , temp , remainder , i , power , digits = 0 , sum = 0 ;
	;
	printf("Enter a number : ");
	scanf("%d" , &number);
	temp = number ;
	while (temp != 0)
	
	{
		digits ++ ;
		temp = temp/10 ;
	}
	temp = number ;
	
	while (number != 0 ){
		remainder = number % 10 ;
		i = 1 ;
		power = 1 ;
		while (i <= digits){
			power = power * remainder ;
			i++ ;
		}
		sum = sum+power ;
		number = number /10;
	}
	if (sum == temp)
	{
		printf("The given number %d is an armstrong number\n" , temp);
		
	}
	else
	{
		printf("The given number %d is not an armstrong number\n" , temp);
	}
	
	return 0 ;
}


Print all the prime number, between 1 to 100 in C.


#include <stdio.h> int main() { int num1 , num2 , i , c = 0 ; printf("Enter range : "); scanf("%d %d" , &num1 , &num2); printf("Prime numbers in primes.txt file:\n"); while (num1 < num2) { c =0 ; if (num1 <= 1) { ++num1; continue ; } for (i = 2 ; i <= num1/2 ; ++i) { if (num1%i == 0) { c =1 ; break; } } if(c == 0 ) { printf("%d " , num1); } ++num1 ; } printf("\n"); return 0 ; }


C program to compute the Sum of the Series 1 + x + x2 + x3 + …………. +xn.


 

#include <stdio.h>
#include <math.h>
int main()
{
	int x , n , sum = 1 , value , i;
	printf("Enter x and n : ");
	scanf("%d %d" , &x , &n);

	
	if(x < 0 ||n < 0)
	{
		printf("Illegal values\n");
	}
		else
		{
			printf("Sum of series : ");
	
		for(i = 1 ; i <= n ; i++)
		{
			value = pow(x , i);
			sum = sum + value ;
		}
	
		
		printf("%d\n" , sum);
		
		}
	

	
	return 0;
}


C program to check whether a given number is Palindrome or not.


#include <stdio.h> int main() { int n , r , sum = 0 , temp ; printf("Enter an integer : "); scanf("%d" , &n); temp = n ; while (n > 0) { r = n % 10 ; sum = (sum * 10) + r ; n = n/10 ; } printf("The reverse of a given number : %d\n" , sum ); if (temp == sum ) { printf("%d is a palindrome\n" , temp ); } else { printf("%d is not a palindrome\n" , temp ); } return 0 ; }

C program to print Floyds Triangle.


#include <stdio.h> int main() { int n , j = 1, i = 1, value = 1 ; printf("Enter n value : "); scanf("%d" , &n); for (i = 1 ; i <= n ; i++) { for(j = 1 ; j <= i ; ++j) { printf("%d " , value); ++value ; } printf("\n"); } return 0 ; }

C program to find the reverse of the given number.


#include <stdio.h> int main() { int n , reverse = 0 , rem ; printf("Enter an integer : "); scanf("%d" , &n); while(n != 0) { rem = n % 10 ; reverse = reverse * 10 + rem ; n = n/10 ; } printf("The reverse number of a given number = %d", reverse); return 0 ; }



C Program to generate the sum of the series 1! + 2! + 3! +-------------- n!.


#include <stdio.h>
int main()
{
	
    int n , j, k = 1 , i = 1 , fact = 1 , sum1 = 0  , sum2 = 0;
	printf("Enter the last number of the series: ");
	scanf("%d" , &n);
	
	while (k < n )
	{
		printf("%d! + " , k );
		k++ ;
		
	}
	printf("%d! " , n);
	printf("=");
	
	
	for (i = 1 ; i <= n ; i++)
	{
		for (j = i ; j <= i ; j++ )
		{
	     fact = fact * i ;
		
		}
		sum1 = sum1 + fact ;
		
	}
	
       	sum2 = sum2 + sum1 ;
	
	
	printf(" %d\n" , sum2);
	
	
	
	
	
	return 0 ;
	
	
}

C Program to find the sum of following series 1 - X1/1! + X2/2! - ………… Xn/n!


#include <math.h> int fact(int x) { if(x ==1) return 1 ; else return x * fact(x - 1); } double series(double x , int n) { double sum = 1 ; for(int i = 1 ; i <= n ; i++) { if(i%2 != 0) { sum = sum - (pow(x,i)/fact(i)); } else { sum = sum + (pow(x , i )/fact(i)); } } return sum ; } int main() { double x ; int n; printf("Enter the value of x : "); scanf("%lf" , &x ); printf("Enter the value of n : "); scanf("%d" , &n); printf("Sum of the given series is : %.4lf\n" , series(x,n)); }

Program to find sum of a fibonacci series up to n terms in C.


#include <stdio.h>
#include <math.h>
int main()
{
   long	double n , first =-1  , second = 1 , sum = 0 , third ;
    int i = 0 ;
  	printf("Enter the range: ");
	scanf("%LF" , &n );
	
	
	for (int i = 0 ; i < n ; i++)
	
	{
		third = first + second ;
		sum = sum + third ;
		first = second ;
		second = third ;
	}
    
   
    
     printf("Sum of Fibonacci series for given range is: %.0Lf\n" , sum);
  
	
	return 0 ;
}

Program to print out all the Armstrong number between given intervals in C.


#include <stdio.h>
#include <math.h>
int main()
{
	int low , high , num , onum , rem , count = 0;
	int result = 0 ;
	printf("Enter the values of n1 and n2 separated by space : ");
	scanf("%d %d" , &low , &high);
	
	printf("Armstrong numbers between %d an %d are : " , low , high);
	 
	for (num = low  ; num <= high ; ++num )
	{
		
		
		onum = num ;
		
		while (onum != 0) 
		{
			onum = onum / 10 ;
			++count ;
		}
		
		onum = num ;
		
		while (onum != 0)
		{
			rem = onum % 10 ;
			result += pow(rem , count);
			onum /= 10 ;
		
		}
		
		if (result == num)
		{   
		    
			printf("%d " , num);
		  
			
		}
		
	
		count = 0 ;
		result = 0 ;
		

	
	}
	
     printf("\n");
	return 0 ;
	

	
	
}


Program to draw the calculator using the switch statements in c language.


 
#include <stdio.h> int main() { long long int f1 , f2 , f3 , n1 , n2 , f4 ; int choice; printf("-----------------Simple Calculator----------------------------------\n"); printf(" press 1 for Addition\n"); printf(" press 2 for Subtraction\n"); printf(" press 3 for Multiplication \n"); printf(" press 4 Division\n"); printf("Enter the choice: "); scanf("%d" , &choice); printf("Enter two number: "); scanf("%lld %lld" , &n1 , &n2); switch (choice){ case 1 : f1 = n1 + n2 ; printf("The sum of two number is = %lld\n" , f1); break; case 2 : f2 = n1 - n2 ; printf("The Subtraction of two number is = %lld\n" , f2); break; case 3 : f3 = n1 * n2 ; printf("The Multiplication of two number is = %lld\n" , f3); break; case 4 : f4 = n1 / n2 ; printf("The Division of two number is = %lld\n" , f4); break; } }


C program to check whether a character is an alphabet or not and if it is an alphabet then check whether it is a vowel or a consonant using if-else and switch.

#include <stdio.h>
int main()
{
	char c;
	
	
	
	printf("Enter a character : ");
	scanf("%c" , &c);
	
	if((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'))
	{
		
		if (c == 'a' || c == 'A' || c =='E' || c == 'e' || c == 'I' || c == 'i' ||  c ==  'O' ||  c == 'o'  || c == 'U' || c == 'u') 
			
		{	
			
			printf("%c is an alphabet and a vowel\n" , c);
			
		}
			
		else
		{
			 printf("%c is an alphabet and a consonant\n" , c);
		}
			
			
		
	}

	else 
	{
		printf("%c is not an alphabet\n" , c);
	}
	
	return 0;
	
}





C program to find whether the given year is a Leap year or not.


#include <stdio.h> void main() { int year ; printf("Enter a year : "); scanf("%d" , &year); if((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) { printf("%d is a leap year\n" , year); } else { printf("%d is not a leap year\n" , year); } }



C program to Total and Average of 5 subjects marks.


#include <stdio.h>
void main() { int sub1 , sub2 , sub3 , sub4 , sub5 ; float total , average ; printf("Enter 5 subjects marks : "); scanf("%d %d %d %d %d" , &sub1 , &sub2 , &sub3 , &sub4 , &sub5); total = sub1 + sub2 + sub3 + sub4 + sub5; average = (sub1 + sub2 + sub3 + sub4 + sub5)/(5.0) ; printf("Total marks : %f\n" , total); printf("Average marks : %f\n" , average); }


Program to convert a Lower case character into an Upper case character in C.


#include <stdio.h>
void main()
{
	char LC , UC ; 
	
	printf("enter lowercase ch: ");
	
	scanf("%c" , &LC);
	
	UC = LC - 32 ; 
	
	printf("uppercase : %c\n" , UC);
	
}



Program to print ASCII value of a given character in C.


#include <stdio.h>
void main() { char c ; printf("enter ch : "); scanf("%c" , &c); printf("ch : %c\n" , c); printf("ASCII value : %d\n" , c); }


Program to find the area and circumference of a circle in C.

#include<stdio.h>
void main()
{
     
	float r , area , circumference ;
	
	printf("radius : ");
	scanf("%f" , &r);
	
	area = 3.14 * r * r;
	circumference = 2 * 3.14 * r ;
	
	printf("area = %f\n" , area);
	printf
("circumference = %f\n" , circumference); }


Program to find the sum of n natural numbers and sum of squares of n natural numbers in C.


#include<stdio.h>
void main()
{
	int n ;
	int sum = 0 ;
	int sos = 0 ;
	
	
	printf("n : ");
	
	scanf("%d" , &n);
	
	for (int i = 1 ; i <= n ; ++i){
		sum += i ;
		sos += (i * i);
		
		
	}
	
	printf("sum : %d\n" , sum );
	printf("sum of squares : %d\n" , sos);
	
	
}


Swapping - without a third variable in C.


#include <stdio.h>

void main() {
	int a, b;
	printf("Enter two integer values : ");
	scanf("%d %d", &a, &b);
	printf("Before swapping the values are a = %d, b = %d\n", a, b);
    a = a + b ;
    b  = a - b ;
    a = a - b ;
    
	printf("After swapping the values are a = %d, b = %d\n", a, b);
}


Program to find the area of a triangle using Heron's formula in C.


#include <stdio.h> #include <math.h> void main(){ float area , a , b , c ,p ; printf("enter sides : "); scanf("%f %f %f" , &a ,&b , &c); p = (a + b + c)/2 ; area = sqrt(p*(p-a)*(p-b)*(p-c)); printf ("area : %f\n" , area); }