Posts

Showing posts from February, 2022

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, sarr[i].rollno, sarr[i].cpi);      } } Output: Enter how many records u want to store : 2 Enter 1 record :  Enter Name : Steve Enter RollNo. : 10 Enter CPI : 9 Enter 2 record :  Enter Name : Peter Enter RollN

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 variable. Book Title: Life of pi Book Pages: 401 Book Price: 320.000000 Book Rating

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 Drafted on 🌏 by, Jal ☮ peace

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

Maximum value from array

Program: #include <stdio.h> void main() {     int arr[10]={10,20,30,40,50,60,70,80,90,100};     int i,max;          max=arr[0];          for(i=1;i<10;i++)     {         if(arr[i]>max)             max=arr[i];     }          printf("Maximum number is: %d",max); } Output: Maximum number is: 100 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=1 Drafted on 🌏 by, Jal ☮ peace

Syntax

Image
Simple if if( Condition ) {     //code if the above condition is true. } if...else if( Condition ) {     //code if the above condition is true. } else {     //code if the above condition is false. } if...else ladder if( Condition 1 ) {     //code if the above condition 1 is true. } else {      if( Condition 2 )      {          //code if the above condition 2 is true.      }      else      {          // some code if the above condition 2 is false.      } } Nested if...else if( Condition 1 ) {      if( Condition 2 )     {         //code if the above condition 2 is true.     }     else     {         //code if the above condition 2 is false.     } } else {      if( Condition 3)     {         //code if the above condition 3 is true.     }     else     {         //code if the above condition 3 is false.     } } switch...case switch ( expression ) {     case constant1:          //statements          break;     case constant2:          //statements          break;     .     .     .     case c

Answers

Image
  Currently, I’m working on gathering answers. As soon I complete this task, a PDF with answers will be uploaded on the Discord server, and all the answers will also be uploaded on this post. I regret your inconvenience. 😞