Skip to main content

Posts

Showing posts from October, 2017

C programs QnA

Q. If sum of two integer is 14 and if one of them is 8 then find other integer. Ans: Well, this is very simple. Simply, one integer is 8 and sum of two integers is 14. So, we need to subtract the number 8 from their sum of 14 to get the second number. So, the second number is: 14 - 8 = 6 C-Program of this task Code---> #include<stdio.h> int main () { int num1 = 8 , num2 , sum = 14 ; num2 = sum - num1 ; printf ( "The second number is: %d" , num2 ); return 0 ; }

C program to check a number is Perfect number or not

In this C -program we will check a number is Perfect number or not. The number will be taken from the user. input: The number . output: The number will be Perfect number or not. CODE----> #include<stdio.h> #include<conio.h> main() { int num,n,i,sum=0; printf(" Please,Enter a number \(to check whether it is Perfect number or Not\) : " ); scanf(" %d ",&num); n=num; for ( i=1 ; i<=(n-1) ; i++ ) { if ( n%i==0 ) sum=sum+i; } if ( sum==num ) printf(" \n %d is a perfect number. ",num); else printf(" \n %d is not a perfect number. ",num); getch(); } Don't just read, write it, run it..... RESULT :

C-program to print A to Z using loop

In this C -program we will print the alphabets A to Z , using loop. input: Null. output: print A to Z CODE----> #include<stdio.h> #include<conio.h> main() { char char_end='Z', char_initial ; for ( char_initial='A' ; char_initial<=char_end ; char_initial++ ) printf(" %c ",char_initial); getch(); } Don't just read, write it, run it..... RESULT :

C-program of calculate the sum of natural numbers up to N

In this C -program we will calculate the sum of natural numbers up to N. The number(N) will be taken from the user. input: The number.(i.e : 5,10,15 etc.) output: The sum up to N. CODE----> #include<stdio.h> #include<conio.h> main() { int num,sum=0,i ; printf(" Please, Enter a positive number : "); scanf(" %d ",&num); if ( num<0 ) printf(" \nYou have Entered a negative number. Please, try again. "); else { for( i=1 ; i ; i++ ) sum=sum+i; printf(" \nThe sum of upto %d is : %d ",num,sum); } getch(); } Don't just read, write it, run it..... RESULT :

C-program to calculate the LCM of two numbers.

In this C -program we will calculate the LCM of two numbers. This is the easiest way to calculate LCM for both Negative and Positive numbers. The numbers will be taken from the user. input: The numbers.(i.e : 20,25,40 etc.) output: The LCM of two numbers given by the user. CODE----> #include<stdio.h> #include<conio.h> main() { int copy_num_1,copy_num_2,num_1,num_2,gcd,lcm=0,i ; printf(" Please, Enter two numbers : "); scanf(" %d%d ",&num_1,&num_2); copy_num_1=num_1; copy_num_2=num_2; //copying numbers , (entered by user) for future use. if( num_1 ) num_1=(num_1)*(-1); if( num_2 ) num_2=(num_2)*(-1); //here, we are converting negative numbers(if any, entered by user), so that, we can calculate the lcm for both positive and negative numbers. for( i=1 ; i && i ; i++ ) { if( num_1%i==0 && num_2%i==0 ) gcd=i; } lcm=num_1*num_2/gcd; //relation between lcm and gcd. so, we can get the lcm us...

C-program to calculate the GCD of two numbers.

In this C -program we will calculate the GCD of two numbers. This is the easiest way to calculate GCD for both Negative and Positive numbers. [This is almost the same program to calculate the LCM of two numbers.(We will calculate the LCM of two numbers in our next program.)] The numbers will be taken from the user. input: The numbers.(i.e : 20,25,40 etc.) output: The GCD of two numbers given by the user. CODE----> #include<stdio.h> #include<conio.h> main() { int copy_num_1,copy_num_2,num_1,num_2,gcd,i ; printf(" Please, Enter two numbers : "); scanf(" %d%d ",&num_1,&num_2); copy_num_1=num_1; copy_num_2=num_2; //copying numbers , (entered by user) for future use. if( num_1 ) num_1=(num_1)*(-1); if( num_2 ) num_2=(num_2)*(-1); //here, we are converting negative numbers(if any, entered by user), so that, we can calculate the gcd for both positive and negative numbers. for( i=1 ; i && i ; i++ ) { if(...

C-program to Convert Binary Number to Decimal Number and Vice-Versa

In this C -program we will Convert the Binary Number into Decimal number and Decimal Number into Binary Number. The number will be taken from the user. input: The number.(i.e : 5,6,9[Decimal Number] or 1100,1010,1111[Binary Number] etc.) output: Binary number will be converted into Decimal number OR Decimal number will be converted into Binary number. CODE----> #include<stdio.h> #include<conio.h> #include<math.h> main() { long long bin_num,bin; int dec_num,dec,remainder,i,n; printf(" ***************Welcome to Converter*************** "); printf(" \n\n\n1.Binary to Decimal\n\n2.Decimal to Binary\n\n Enter Your choice : "); scanf(" %d ",&n); switch( n ) { case 1 : printf(" \nPlease, Enter the Binary number : "); scanf(" %lld ",&bin_num); bin=bin_num; i=0; dec=0; while( bin_num!=0 ) { remainder=bin_num%10; dec=dec+remainder*pow( 2 , i ); i++; bin_num=bin_num/10; } pri...

C-program to calculate power of a number

In this C -program we will calculate the power of a number and print the result. The number will be taken from the user. We can calculate the power of a number in two ways. In the first way , we will use a loop (while loop) and calculate it. In the second way , we will calculate it with the help of a function , called " pow() ". First way: Calculate with loop input: The number(i.e : base) and the power(i.e : exponent)(i.e : 5,6,9 etc.) output: The result of it's calculation. CODE----> #include<stdio.h> #include<conio.h> void main() { int a,b,r=1,n; clrscr(); printf(" Please, Enter a number (base): "); scanf(" %d ",&n); printf(" \nPlease, Enter it\'s power (exponent): "); scanf(" %d ",&a); b=a; while( a!=0 ) { r=r*n; a--; } printf(" \nThe result of %d^%d = %d ",n,b,r); getch(); } Don't just read, write it, run it..... RESULT : *Note : The above p...

C-program for Multiplication Table

In this C -program we will print the Multiplication-Table of a Number. The number will be taken from the user. input: The number.(i.e : 5,6,9 etc.) output: The Multiplication table. CODE----> #include<stdio.h> #include<conio.h> void main() { int n,i; clrscr(); printf(" Enter a number: "); scanf(" %d ",&n); printf(" \n "); for( i=1 ; i ; i++ ) printf(" %d*%d = %d\n ",n,i,n*i); getch(); } Don't just read, write it, run it..... RESULT :

C program to make a calculator

In this C -program we will make a calculator. input: Your choice of calculation(i.e : multiplication,division etc.) and the numbers you want to calculate. output: The Calculator made by you with the result you calculated. CODE----> #include<stdio.h> #include<stdlib.h> int main () { float a [ 50 ], b , d , sum = 0.0 , sub , mul = 1.0 , div ; int k , n , i ; printf ( "***********Welcome to calculator************\n\n" ); printf ( "Please, choose any operation:\n" ); printf ( "1.Sum\n2.Subtraction\n3.Multiplication\n4.Division\n5.Exit\nYour choice: " ); scanf ( "%d" ,& n ); switch ( n ) { case 1 : printf ( "\nPlease, Enter how many numbers you want to add: " ); scanf ( "%d" ,& k ); printf ( "\nPlease, Enter numbers:\n" ); for ( i = 1 ; i <= k ; i ++) { scanf ( "%f" ,& a [ i ]); sum = sum + a [ i ]; } printf ( "\nThe...

C-program to find the factors of a number

In this C program we will find the factors of a number. The number will be taken from the user. input: The number(i.e: 5,6,9 etc.) output: The factors of the given number, by the user. CODE----> #include<stdio.h> int main () { int n , i ; printf ( "Enter a number: " ); scanf ( "%d" ,& n ); if ( n < 0 ) { printf ( "\n%d is a negative number. Factors of a negative number doesn't exist." , n ); } if ( n == 0 ) { printf ( "\nThe factors of 0 is = 0" ); } if ( n > 0 ) { printf ( "\nThe factors of %d are: " , n ); for ( i = 1 ; i <= n ; i ++) { if ( n % i == 0 ) printf ( "%d " , i ); } } return 0 ; } Download the C-Program file of this Program. Don't just read, run on your pc !!! RESULT : Enter a number: 100 The factors of 100 are: 1 2 4 5 10 20 25 50 100 -------------------------------- Process exited after 4.536 seconds with ret...

C-program to check a number is KRISHNAMURTHY number or not

In this C -program we will check the number is Krishnamurthy number (i.e: krishnamurthy number is a number which is equal to the sum of the factorials of it's digits, example:145=1!+4!+5!) or not. The number will be taken from the user. input: The number(i.e : 567,610,7899 etc.) output: The number will be Krishnamurthy or not CODE----> #include<stdio.h> #include<stdlib.h> int main () { int i , n , a , r , c = 0 , s ; printf ( " Please,Enter a number : " ); scanf ( "%d" ,& n ); if ( n == 0 ) { printf ( "\n %d is not a Krishnamurthy Number." , n ); exit ( 0 ); } a = n ; while ( n > 0 ) { r = n % 10 ; s = 1 ; for ( i = r ; i >= 1 ; i --) s = s * i ; c = c + s ; n = n / 10 ; } if ( c == a ) printf ( "\n %d is a Krishnamurthy Number." , a ); else printf ( "\n %d is not a Krishnamurthy Number." , a ); return 0 ; } Download the C-Program file o...

C-program to check a number is ARMSTRONG number or not

In this C -program we will check the number is Armstrong number or not. The number will be taken from the user. input: The number(i.e : 567,610,7899 etc.) output: The number will be Armstrong or not CODE----> #include<stdio.h> #include<math.h> int main () { int c = 0 , b , a = 0 , r = 0 , n ; printf ( " Please,Enter a number : " ); scanf ( "%d" ,& n ); b = n ; while ( n != 0 ) { n = n / 10 ; ++ c ; } n = b ; while ( n != 0 ) { r = n % 10 ; a = a + pow ( r , c ); n = n / 10 ; } if ( b == a ) printf ( "\n %d is a Armstrong number." , b ); else printf ( "\n %d is not a Armstrong number." , b ); return 0 ; } Download the C-Program file of this Program. Don't just read, run on your pc !!! RESULT : Please,Enter a number : 153 153 is a Armstrong number. -------------------------------- Process exited after 2.701 seconds with return value 0 Press any key to c...