Posts

Showing posts from December, 2021

How does a C program to find largest number using dynamic memory allocation work?

 Courtesy: Programiz.com In the program, we have asked the user to enter the total number of elements which is stored in the variable  n . Then, we have allocated memory for  n  number of  double  values. // Allocating memory for n double values data = ( double *) calloc (n, sizeof ( double )); Then, we used a  for  loop to take  n  number of data from the user. // Storing elements for ( int i = 0 ; i < n; ++i) { printf ( "Enter Number%d: " , i + 1 ); scanf ( "%lf" , data + i); } Finally, we used another  for  loop to compute the largest number. // Computing the largest number for ( int i = 1 ; i < n; ++i) { if (*data < *(data + i)) *data = *(data + i); } } Note:  Instead of  calloc() , it's also possible to solve this problem using the  malloc()  function.

How does a C program to swap elements using call by reference work?

Courtesy: Programiz.com   Here, the three numbers entered by the user are stored in variables  a ,  b  and  c  respectively. The addresses of these numbers are passed to the  cyclicSwap()  function. cyclicSwap(&a, &b, &c); In the function definition of  cyclicSwap() , we have assigned these addresses to pointers. cyclicSwap( int *n1, int *n2, int *n3) { ... } When  n1 ,  n2  and  n3  inside  cyclicSwap()  are changed, the values of  a ,  b  and  c  inside  main()  are also changed. Note:  The  cyclicSwap()  function is not returning anything.

How does a C program to store numbers and calculate average using arrays work?

 Courtesy: Programiz.com Here, the user is first asked to enter the number of elements. This number is assigned to  n . If the user entered integer is greater less than 1 or greater than 100, the user is asked to enter the number again. This is done using a  while  loop. Then, we have iterated a  for  loop from  i = 0  to  i . In each iteration of the loop, the user is asked to enter numbers to calculate the average. These numbers are stored in the  num[]  array. scanf ( "%f" , &num[i]); And, the sum of each entered element is computed. sum += num[i]; Once the  for  loop is completed, the average is calculated and printed on the screen.

How does a C program to convert decimal number to binary work?

Courtesy: Programiz.com   Suppose  n = 13 . Let's see how the  while  loop in the  convert()  function works. n != 0 rem = n % 2 n /= 2 i bin += rem * i i * = 10 13 != 0 13 % 2 = 1 13 / 2 = 6 1 0 + 1 * 1 = 1 1 * 10 = 10 6 != 0 6 % 2 = 0 6 / 2 = 3 10 1 + 0 * 10 = 1 10 * 10 = 100 3 != 0 3 % 2 = 1 3 / 2 = 1 100 1 + 1 * 100 = 101 100 * 10 = 1000 1 != 0 1 % 2 = 1 1 / 2 = 0 1000 101 + 1 * 1000 = 1101 1000 * 10 = 10000 0 != 0 - - - Loop terminates Thus,  13  in decimal is  1101  in binary.

How does a C program to convert binary number to decimal work?

 Courtesy: Programiz.com In the program, we have included the header file  math.h  to perform mathematical operations in the program. We ask the user to enter a binary number and pass it to the  convert()  function to convert it decimal. Suppose  n = 1101 . Let's see how the  while  loop in the  convert()  function works. n != 0 rem = n % 10 n /= 10 i dec += rem * pow(2, i) 1101 != 0 1101 % 10 = 1 1101 / 10 = 110 0 0 + 1 * pow (2, 0) = 1 110 != 0 110 % 10 = 0 110 / 10 = 11 1 1 + 0 * pow (2, 1) = 1 10 != 0 11 % 10 = 1 11 /10 = 1 2 1 + 1 * pow (2, 2) = 5 1 != 0 1 % 10 = 1 1 / 10 = 0 3 5 + 1 * pow (2, 3) = 13 0 != 0 - - - Loop terminates So,  1101  in binary is  13  in decimal.

How does a C program to display prime numbers between intervals using function work?

Courtesy: Programiz.com 1. In this program, we print all the prime numbers between  n1  and  n2 . If  n1  is greater than  n2 , we  swap their values : if (n1 > n2) { n1 = n1 + n2; n2 = n1 - n2; n1 = n1 - n2; } 2. Then, we run a  for  loop from  i = n1 + 1  to  i = n2 - 1 . In each iteration of the loop, we check if  i  is a prime number using the  checkPrimeNumber()  function. If  i  is prime, we print it. for (i = n1 + 1 ; i < n2; ++i) { flag = checkPrimeNumber(i); if (flag == 1 ) printf ( "%d " , i); } } 3. The  checkPrimeNumber()  function contains the code to  check whether a number is prime or not . int checkPrimeNumber ( int n) { int j, flag = 1 ; for (j = 2 ; j <= n / 2 ; ++j) { if (n % j == 0 ) { flag = 0 ; break ; } } return flag; }

How does a C program to demonstrate the working of keyword long work?

 Courtesy: Programiz.com In this program, the  sizeof  operator is used to find the size of  int ,  long ,  long long ,  double  and  long double  variables. As you can see, the size of  long int  and  long double  variables are larger than  int  and  double  variables, respectively. By the way, the  sizeof  operator returns  size_t  (unsigned integral type). The  size_t  data type is used to represent the size of an object. The format specifier used for  size_t  is  %zu . Note:  The  long  keyword cannot be used with  float  and  char  types.

Program to print multipliaction table using exit control loop

Program: #include<stdio.h> #include<conio.h> void main() {     int i=1, number;      clrscr();     printf("Enter a number: ");     scanf("%d",&number);     do{         printf("%d * %d = %d\n",number,i,number*i);         i++;     }while(i<=10);     getch(); } Output: Enter a number: 5 5 * 1 = 5 5 * 2 = 10 5 * 3 = 15 5 * 4 = 20 5 * 5 = 25 5 * 6 = 30 5 * 7 = 35 5 * 8 = 40 5 * 9 = 45 5 * 10 = 50

The Continue Keyword (Program)

Image
Program: #include<stdio.h> #include<conio.h> void main() {     for(int i=1;i<=10;i++)     {         if(i==5)         {             continue;         }         else         {             printf("%d\n",i);         }     } } Output: 1 2 3 4 6 7 8 9 10

The Break Keyword (Program)

Image
Program: #include<stdio.h> #include<conio.h> void main() {     int i=1;      clrscr();     while(1)     {         if(i>10)         {             break;         }         else         {             printf("%d\n",i);             i++;         }     }      getch(); } Output: 1 2 3 4 5 6 7 8 9 10

Some escape sequences and usage of it in a C program.

 List of the escape sequence Escape Sequence Usage \a Alarm or Beep \b Backspace \f Formfeed \n New Line \r Carriage Return \t Tab (Horizontal) \v Vertical Tab \\ Backslash \' Single Quote \" Double Quote \? Question Mark \ooo Octal Number \xhh Hexadecimal Number \0 Null Program: #include<stdio.h> #include<conio.h> void main() {     clrscr();     printf("Demonstrating use of \\n \\t \\r and \\b\n\n");          printf("Use of \\n: \\n is used to add a new line.\n");     printf("line1.\nline2.\n\n");          printf("Use of \\t: \\t is used to add the horizontal ta

C program to demonstrate the use of all assignment operators.

Program: #include<stdio.h> #include<conio.h> void main() {     int a,b,c;      clrscr();     printf("a = ");     scanf("%d",&a);     printf("b = ");     scanf("%d",&b);     c=a;     printf("after c=a a= %d and c= %d\n",a,c);     a+=b;     printf("after a+=b a=%d and b=%d\n",a,b);     a=c;     a-=b;     printf("after a-=b a=%d and b=%d\n",a,b);     a=c;     a*=b;     printf("after a*=b a=%d and b=%d\n",a,b);     a=c;     a/=b;     printf("after a/=b a=%d and b=%d\n",a,b);     a=c;     a%=b;     printf("after a%%=b a=%d and b=%d\n",a,b);     getch(); } Output: a = 5 b = 2 after c=a a= 5 and c= 5 after a+=b a=7 and b=2 after a-=b a=3 and b=2 after a*=b a=10 and b=2 after a/=b a=2 and b=2 after a%=b a=1 and b=2

C program to demonstrate the use of all arithmetic operators.

Program: #include<stdio.h> #include<conio.h> void main() {     int a,b,c;      clrscr();     printf("a = ");     scanf("%d",&a);     printf("b = ");     scanf("%d",&b);     c=a+b;     printf("%d + %d = %d //Addition\n",a,b,c);     c=a-b;     printf("%d - %d = %d //Substraction\n",a,b,c);     c=a*b;     printf("%d * %d = %d //Multiplication\n",a,b,c);     c=a/b;     printf("%d / %d = %d //Division\n",a,b,c);     c=a%b;     printf("%d %% %d = %d //Modulus\n",a,b,c);     getch(); } Output: a = 5 b = 2 5 + 2 = 7 //Addition 5 - 2 = 3 //Substraction 5 * 2 = 10 //Multiplication 5 / 2 = 2 //Division 5 % 2 = 1 //Modulus

C program to find last digit (unit digit) of a number.

Image
Extract the last digit from a number Program: #include<stdio.h> #include<math.h> #include<conio.h> void main() {     int num,last_digit;     clrscr();     printf("Enter a number: ");     scanf("%d",&num);     last_digit=num%10;     printf("the last digit is: %d",abs(last_digit));     getch(); } Output 1: Enter a number: 203 the last digit is: 3 Output 2: Enter a number: -607 the last digit is: 7

C program to print 1 to 20 using exit control loop

Program: #include<stdio.h> #include<conio.h> void main() {     int i=1;     clrscr();     do     {         printf("%d\n",i);         i++;     }     while(i<=20);     getch(); } Output: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20

Rules for identifiers in C

The first character must be an alphabet  or underscore . Must consist of only letters , digits , or underscore . Only the first 31 characters are significant . Cannot use a keyword . Must not contain white space . Example:   Variable name Valid/Invalid Why? abc Valid No rules were violated. XYZ Valid No rules were violated. 1st Invalid The first character is a digit. (Violated 1st rule) _health Valid No rules were violated. #dontspoiltheendgame Invalid Starts with the special symbol.(Violated 1st & 2nd rule). max number Invalid Contains whitespace. (Violated 5th rule) max_number Valid No rules were violated. float Invalid Makes use of a reserved keyword. (Violated 4th rule) Price$1

Definitions

Image
Computer:     The computer is an electronic device that takes data and instructions as input from the user, processes the data , and generates useful information as an output . Data:      Data is a collection of raw facts and statistics, then operations or manipulations are performed on these facts or statistics for reference or analysis . Input Devices:     Input devices accept the data from the end-users on which the operations are to be performed. Output Devices:     Output devices are used for providing the output of a program that is obtained after programming the operations specified in a program. Information:     Information can be defined as the processed form of data . Compiler:     The compiler is a computer program that translates the source code written in a high-level language into the corresponding object-code of the low-level language. Interpreter:     The interpreter is a translation program that converts each high-level program statement into the corresponding