Skip to main content

Posts

Showing posts from December, 2018

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 ; }

Print Binary Equivalent of a Decimal Number Using Bitwise Operator in C-Language

In this C program we will calculate the Binary Equivalent of a Decimal Number Using Bitwise Operator and then we will print the result on the screen. The Number will be taken from the user. N:B This Program is created for upto 8-bit binary number ( i.e: Range = 0 to 2 8 -1 = 0 to 255 ). input: The Number (integers) (i.e. 15,10,128 etc.) output: The binary equivalent of the decimal number, will be printed on the screen. CODE----> #include<stdio.h> int main () { int n , i , x ; printf ( " Please, Enter a Number : " ); scanf ( "%d" ,& n ); printf ( "\n " ); for ( i = 7 ; i >= 0 ; i --) { x = n &( 1 << i ); if ( x == 0 ) printf ( "0" ); else printf ( "1" ); } return 0 ; } Don't just read, write it, run it..... RESULT :

Calculate the Size of a File in C-Language

In this C program we will calculate the Size of a text-file(.txt). Then, we will print the Size of the File in bytes. input: NULL output: The Size of the text-file(.txt) will be printed on the screen in bytes. CODE----> #include<stdio.h> int main () { FILE * fp ; long int p ; fp = fopen ( "My File.txt" , "r" ); fseek ( fp , 0 , SEEK_END ); p = ftell ( fp ); printf ( " The Size of the File is : %ld bytes." , p ); return 0 ; } N.B. : In this C-Program the used text-file(.txt) and the C-Program file was stored in the same directory or path. So, if you want to open the text-file(.txt) stored in any other directory, path or folder, then, you have to declare the whole path or directory in the program.(i.e : fp=fopen("C:\\Users\\ADMIN\Desktop\\...the directory...\\file_name.txt") ). The directory of a text-file(.txt) can be found by it's Property . Don't just read, write it, run it..... RESULT :

Swap Two Numbers Using Bitwise Operator in C-Language

In this C program we will Swap Two numbers using Bitwise Operators and then we will print the result on the screen. The Numbers will be taken from the user. input: The Numbers (integers) (i.e. 5,45,75 etc.) output: After Swapping the Numbers will be printed on the screen. CODE----> #include<stdio.h> #include<conio.h> int main () { //swap two numbers using bitwise operator int x , y ; printf ( " Please, Enter the 1st Number : " ); scanf ( "%d" ,& x ); printf ( "\n Please, Enter the 2nd Number : " ); scanf ( "%d" ,& y ); //swapping -------->> x = x ^ y ; y = x ^ y ; x = x ^ y ; printf ( "\n Now, the Numbers are : %d and %d" , x , y ); return 0 ; } Don't just read, write it, run it..... RESULT : If the program is not running, you can remove the second line from the code(i.e: #include<conio.h> )

Odd or Even Number Checking using Bitwise Operator in C-Language

In this C program we will check if a number is Odd or Even Number using Bitwise Operator and then we will print the result on the screen. The Number will be taken from the user. input: The Number (integers) (i.e. 5,10,7 etc.) output: The Number is Odd or Even, will be printed on the screen. CODE----> #include<stdio.h> #include<conio.h> int main () { //checking a number is odd or even using bitwise operator int x , y ; printf ( " Please, Enter the Number : " ); scanf ( "%d" ,& x ); y = x & 1 ; if ( y == 1 ) printf ( "\n %d is a Odd Number." , x ); else printf ( "\n %d is Even Number." , x ); return 0 ; } Don't just read, write it, run it..... RESULT :

Write into a text-file(.txt) and again read the content(in Run-Time) using C-Language

In this C program we will Write some content into a text-file(.txt). Then, we will again open the text-file(.txt) in this same program and read the content entered by the user. input: The String or Content entered by the User. output: The content will be stored into the specified text-file(.txt) and the content of the text-file(.txt) will be printed on the screen. CODE----> #include<stdio.h> #include<stdlib.h> int main () { FILE * fp ,* fr ; char s [ 80 ], ch ; int i = 0 ; fp = fopen ( "New File.txt" , "w" ); if ( fp == NULL ) printf ( "Unable to Open File." ); else { printf ( "Please, Enter a String : " ); fgets ( s , sizeof ( s ), stdin ); while ( s [ i ]!= '\0' ) { fputc ( s [ i ], fp ); i ++; } printf ( "\n Your File has been saved." ); fclose ( fp ); } printf ( "\n\n" ); fr = fopen ( "New File.txt" , "r" ); if ( fr == NULL

Write into a text-file(.txt) in C-Language

In this C program we will Write some content into a text-file(.txt). Then, we can see the content of text-file(.txt) by opening in externally for varification if the Content entered by the user and the content stored to the text-file(.txt) are same or not. input: The String or Content entered by the User. output: The content will be stored into the specified text-file(.txt). CODE----> #include<stdio.h> #include<stdlib.h> int main () { FILE * fp ; char s [ 80 ]; int i = 0 ; fp = fopen ( "New File.txt" , "w" ); if ( fp == NULL ) printf ( "Unable to Open File." ); else { printf ( "Please, Enter a String : " ); fgets ( s , sizeof ( s ), stdin ); while ( s [ i ]!= '\0' ) { fputc ( s [ i ], fp ); i ++; } printf ( "\n Your File has been saved." ); fclose ( fp ); } return 0 ; } N.B. : In this C-Program the used text-file(.txt) and the C-Program file was stored in t

Reading From a File in C-Language

In this C program we will Read the content of a text-file(.txt) by printing the content of the text-file(.txt)in the screen. input: NULL. output: The content of the text-file(.txt) will be printed on the screen and then we can read the content. CODE----> #include<stdio.h> #include<stdlib.h> int main () { FILE * fp ; char ch ; fp = fopen ( "hello.txt" , "r" ); if ( fp == NULL ) { printf ( " Unable to open File." ); } else { ch = fgetc ( fp ); while ( ch != EOF ) { printf ( "%c" , ch ); ch = fgetc ( fp ); } } fclose ( fp ); return 0 ; } N.B. : In this C-Program the used text-file(.txt) and the C-Program file was stored in the same directory or path. So, if you want to open the text-file(.txt) stored in any other directory, path or folder, then, you have to declare the whole path or directory in the program.(i.e : fp=fopen("C:\\Users\\ADMIN\Desktop\\...the directory...\\file_