Posts

Showing posts with the label programs

Pattern #4

Image
Program: #include <stdio.h> void main() {     int i, j, k;          for(i=4;i>=0;i--)     {         for(k=1;k<=i;k++)         {             printf(" ");         }         for(j=k;j<=5;j++)         {             printf("*");         }         printf("\n");     } } Output:     *    **   ***  **** ***** Drafted on 🌏 by, Jal ☮ peace

Pattern #3

Image
Program: #include <stdio.h> void main() {     int i, j;          for(i=1;i<=5;i++)     {         for(j=1;j<=i;j++)         {             printf("%d",j);         }         printf("\n");     } } Output: 1 12 123 1234 12345 Drafted on 🌏 by, Jal ☮ peace

Pattern #2

Image
Program: #include <stdio.h> void main() {     int rows, i, j, number = 1;     printf("Enter the number of rows: ");     scanf("%d", &rows);     for (i = 1; i <= rows; i++)     {         for (j = 1; j <= i; ++j)         {             printf("%d ", number);             ++number;         }         printf("\n");     } } Output: 1 23 456 78910 Drafted on 🌏 by, Jal ☮ peace

Pattern #1

Image
Program: #include <stdio.h> void main() {     int i,j,k=5;          for(i=0;i<5;i++)     {         for(j=0;j<=i;j++)         {             printf("%d",k);             k--;         }         printf("\n");         k=5;     } } Output: 5 54 543 5432 54321 Drafted on 🌏 by, Jal ☮ peace

C program to write a string in the file

Program: #include<stdio.h> void main() {     char str[20]; FILE *out; out=fopen("file1.txt","w"); printf("Enter string: "); gets(str); fputs(str,out); fclose(out); } Output: Enter string: Hello World! Drafted on 🌏 by, Jal ☮ peace

C program to read and display information of N students using an array of structure

Program: #include<stdio.h> struct student {     char name[20];     int rollno;     float cpi; }; void main( ) {     int i,n;     printf("Enter how many records u want to store : ");     scanf("%d",&n);     struct student sarr[n];          for(i=0; i<n; i++)     {         printf("\nEnter %d record : \n",i+1);         printf("Enter Name : ");         scanf("%s",sarr[i].name);         printf("Enter RollNo. : ");         scanf("%d",&sarr[i].rollno);         printf("Enter CPI : ");         scanf("%f",&sarr[i].cpi);     }     printf("\n\tName\t\tRollNo\t\tMarks\t\n");          for(i=0; i<n; i++)     {         printf("\t%s\t\t%d\t\t%.2f\t\n", sarr[i].name,...

C program to read and display book information using structure

Program: #include<stdio.h> struct book {     char title[40];     int pages;     float price;     int ratings; }; int main() {     struct book book1;     printf("Enter Book Title: ");     gets(book1.title);     printf("Enter Book Pages:");     scanf("%d", &book1.pages);     printf("Enter Book Price:");     scanf("%f", &book1.price);     printf("Enter Book Ratings:");     scanf("%d", &book1.ratings);          printf("\nBook using simple structure variable.\n");     printf("Book Title: %s\n", book1.title);     printf("Book Pages: %d\n", book1.pages);     printf("Book Price: %f\n", book1.price);     printf("Book Ratings: %i\n", book1.ratings);  } Output: Enter Book Title: Life of pi Enter Book Pages:401 Enter Book Price:320 Enter Book Ratings:4 Book using simple structure vari...

C Program to display factors of a number

Program: #include <stdio.h> void main() {     int num, i;          printf("Enter a positive integer: ");     scanf("%d", &num);     printf("Factors of %d are: ", num);          for (i = 1; i <= num; ++i)     {         if (num % i == 0)         {         printf("%d ", i);         }     } } Output: Enter a positive integer: 10 Factors of 10 are: 1 2 5 10  Drafted on 🌏 by, Jal ☮ peace

C program to print the variable, the address of the pointer variable, and a pointer to pointer variable

Program: #include <stdio.h> void main() {     int var;     int *ptr;     int **pptr;          var=3000;     ptr=&var;     pptr=&ptr;          printf("Value of var = %d\n",var);     printf("-----printing values using pointer-----\n");     printf("Value available at *ptr = %d\n",*ptr);     printf("Value available at **pptr = %d\n\n",**pptr);          printf("Address of var = %d\n",&var);     printf("-----printing addresses using pointer-----\n");     printf("Address of var = %d\n",ptr);     printf("Address of var = %d\n",*pptr); } Output: Value of var = 3000 -----printing values using pointer----- Value available at *ptr = 3000 Value available at **pptr = 3000 Address of var = -1209757212 -----printing addresses using pointer----- Address of var = -1209757212 Address of var = -1209757212 D...

C program to find factorial using recursion

Program: #include <stdio.h> int fact(int n); void main() {     int factorial, num=5;          factorial = fact(num);          printf("factorial = %d",factorial); } int fact(int n) {     if(n==0 || n==1)         return 1;     else         return n*fact(n-1); } Output: factorial = 120 Drafted on 🌏 by, Jal ☮ peace

C program to find maximum value out of 2 values

Program: #include <stdio.h> int maximum(int x, int y); void main() {     int a=10, b=20, max=0;          max=maximum(a,b);          printf("Maximum value is: %d",max); } int maximum(int x, int y) {      if(x>y)          return x;     else         return y; } Output: Maximum value is: 20 Drafted on 🌏 by, Jal ☮ peace

Count positive, negative and zero values in an array

Program: #include <stdio.h> void main() {     int num[10],i,pos=0,neg=0,zero=0;     for(i=0;i<10;i++)     {         printf("Enter array element: ");         scanf("%d",&num[i]);         if(num[i]>0)             pos=pos+1;         else             if(num[i]<0)                 neg=neg+1;             else                 zero=zero+1;     }     printf("Positive=%d, Negative=%d, Zero=%d",pos,neg,zero); } Output: Enter array element: -40 Enter array element: -30 Enter array element: -20 Enter array element: -10 Enter array element: 0 Enter array element: 10 Enter array element: 20 Enter array element: 30 Enter array element: 40 Enter array element: 50 Positive=5, Negative=4, Zero...

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.