├── Switch Case Statements ├── gets-puts ├── Half Pyramid of * ├── Half Pyramid of Numbers ├── Inverted half pyramid of * ├── C Program To Check A Number Is Negative, Positive Or Zero ├── Floyd's Triangle. ├── Inverted half pyramid of numbers ├── C Data Types ├── Half Pyramid of Alphabets ├── Full Pyramid of * ├── PRIME NUMBERS BTWN 1-100 ├── Inverted full pyramid of * ├── Switch Case Example ├── Pascal's Triangle ├── Continue statement ├── Break statement ├── Convert a binary to a decimal using for loop and without using array ├── Print the string after replacing all occurrences of the character with another character in the string. ├── Full Pyramid of Numbers ├── GO-TO STATEMENT ├── reverse the elements of an array ├── Find largest and smallest element in array ├── Using Function ├── Write a program in C to convert a decimal number into binary without using an array ├── 1D array using the dynamic memory allocation └── Program to Find Roots of a Quadratic Equation /Switch Case Statements: -------------------------------------------------------------------------------- 1 | switch ( integer expression ) 2 | { 3 | case {value 1} : 4 | do this ; 5 | 6 | case {value 2} : 7 | do this ; 8 | 9 | case {value 3} : 10 | do this ; 11 | 12 | default : 13 | do this ; 14 | } 15 | -------------------------------------------------------------------------------- /gets-puts: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | char name[30]; 6 | printf("Enter name: "); 7 | gets(name); //Function to read string from user. 8 | printf("Name: "); 9 | puts(name); //Function to display string. 10 | return 0; 11 | } 12 | -------------------------------------------------------------------------------- /Half Pyramid of *: -------------------------------------------------------------------------------- 1 | #include 2 | int main() { 3 | int i, j, rows; 4 | printf("Enter the number of rows: "); 5 | scanf("%d", &rows); 6 | for (i = 1; i <= rows; ++i) { 7 | for (j = 1; j <= i; ++j) { 8 | printf("* "); 9 | } 10 | printf("\n"); 11 | } 12 | return 0; 13 | } 14 | -------------------------------------------------------------------------------- /Half Pyramid of Numbers: -------------------------------------------------------------------------------- 1 | #include 2 | int main() { 3 | int i, j, rows; 4 | printf("Enter the number of rows: "); 5 | scanf("%d", &rows); 6 | for (i = 1; i <= rows; ++i) { 7 | for (j = 1; j <= i; ++j) { 8 | printf("%d ", j); 9 | } 10 | printf("\n"); 11 | } 12 | return 0; 13 | } 14 | -------------------------------------------------------------------------------- /Inverted half pyramid of *: -------------------------------------------------------------------------------- 1 | #include 2 | int main() { 3 | int i, j, rows; 4 | printf("Enter the number of rows: "); 5 | scanf("%d", &rows); 6 | for (i = rows; i >= 1; --i) { 7 | for (j = 1; j <= i; ++j) { 8 | printf("* "); 9 | } 10 | printf("\n"); 11 | } 12 | return 0; 13 | } 14 | -------------------------------------------------------------------------------- /C Program To Check A Number Is Negative, Positive Or Zero: -------------------------------------------------------------------------------- 1 | #include 2 | int main() 3 | { 4 | int n; 5 | printf("enter n value:\n"); 6 | scanf("%d",&n); 7 | if(n>0) 8 | printf("it is positive number\n"); 9 | else if(n<0) 10 | printf("it is negative number\n"); 11 | else 12 | printf("it is zero\n"); 13 | 14 | } 15 | -------------------------------------------------------------------------------- /Floyd's Triangle.: -------------------------------------------------------------------------------- 1 | #include 2 | int main() { 3 | int rows, i, j, number = 1; 4 | printf("Enter the number of rows: "); 5 | scanf("%d", &rows); 6 | for (i = 1; i <= rows; i++) { 7 | for (j = 1; j <= i; ++j) { 8 | printf("%d ", number); 9 | ++number; 10 | } 11 | printf("\n"); 12 | } 13 | return 0; 14 | } 15 | -------------------------------------------------------------------------------- /Inverted half pyramid of numbers: -------------------------------------------------------------------------------- 1 | 1 2 3 4 5 2 | 1 2 3 4 3 | 1 2 3 4 | 1 2 5 | 1 6 | 7 | #include 8 | int main() { 9 | int i, j, rows; 10 | printf("Enter the number of rows: "); 11 | scanf("%d", &rows); 12 | for (i = rows; i >= 1; --i) { 13 | for (j = 1; j <= i; ++j) { 14 | printf("%d ", j); 15 | } 16 | printf("\n"); 17 | } 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /C Data Types: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | // Creating variables having different data types 6 | int integer = 26; 7 | float floating = 39.32; 8 | char character = 'A'; 9 | 10 | // Printing variables with the help of their respective format specifiers 11 | printf("%d\n", integer); 12 | printf("%f\n", floating); 13 | printf("%c\n", character); 14 | } 15 | -------------------------------------------------------------------------------- /Half Pyramid of Alphabets: -------------------------------------------------------------------------------- 1 | #include 2 | int main() { 3 | int i, j; 4 | char input, alphabet = 'A'; 5 | printf("Enter an uppercase character you want to print in the last row: "); 6 | scanf("%c", &input); 7 | for (i = 1; i <= (input - 'A' + 1); ++i) { 8 | for (j = 1; j <= i; ++j) { 9 | printf("%c ", alphabet); 10 | } 11 | ++alphabet; 12 | printf("\n"); 13 | } 14 | return 0; 15 | } 16 | -------------------------------------------------------------------------------- /Full Pyramid of *: -------------------------------------------------------------------------------- 1 | #include 2 | int main() { 3 | int i, space, rows, k = 0; 4 | printf("Enter the number of rows: "); 5 | scanf("%d", &rows); 6 | for (i = 1; i <= rows; ++i, k = 0) { 7 | for (space = 1; space <= rows - i; ++space) { 8 | printf(" "); 9 | } 10 | while (k != 2 * i - 1) { 11 | printf("* "); 12 | ++k; 13 | } 14 | printf("\n"); 15 | } 16 | return 0; 17 | } 18 | -------------------------------------------------------------------------------- /PRIME NUMBERS BTWN 1-100: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int i, a = 1, count; 6 | 7 | 8 | while(a <= 100) 9 | { 10 | count = 0; 11 | i = 2; 12 | while(i <= a/2) 13 | { 14 | if(a%i == 0) 15 | { 16 | count++; 17 | break; 18 | } 19 | i++; 20 | } 21 | if(count == 0 && a != 1 ) 22 | { 23 | printf(" %d ", a); 24 | } 25 | a++; 26 | } 27 | return 0; 28 | } 29 | -------------------------------------------------------------------------------- /Inverted full pyramid of *: -------------------------------------------------------------------------------- 1 | #include 2 | int main() { 3 | int rows, i, j, space; 4 | printf("Enter the number of rows: "); 5 | scanf("%d", &rows); 6 | for (i = rows; i >= 1; --i) { 7 | for (space = 0; space < rows - i; ++space) 8 | printf(" "); 9 | for (j = i; j <= 2 * i - 1; ++j) 10 | printf("* "); 11 | for (j = 0; j < i - 1; ++j) 12 | printf("* "); 13 | printf("\n"); 14 | } 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /Switch Case Example: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int i = 2; 6 | 7 | switch (i) 8 | { 9 | case 1: 10 | printf("Statement 1"); 11 | break; 12 | 13 | case 2: 14 | printf("Statement 2"); 15 | break; 16 | 17 | case 3: 18 | printf("Statement 3"); 19 | break; 20 | 21 | default: 22 | printf("No valid i to switch to."); 23 | break; 24 | } 25 | return 0; 26 | } 27 | -------------------------------------------------------------------------------- /Pascal's Triangle: -------------------------------------------------------------------------------- 1 | #include 2 | int main() { 3 | int rows, coef = 1, space, i, j; 4 | printf("Enter the number of rows: "); 5 | scanf("%d", &rows); 6 | for (i = 0; i < rows; i++) { 7 | for (space = 1; space <= rows - i; space++) 8 | printf(" "); 9 | for (j = 0; j <= i; j++) { 10 | if (j == 0 || i == 0) 11 | coef = 1; 12 | else 13 | coef = coef * (i - j + 1) / j; 14 | printf("%4d", coef); 15 | } 16 | printf("\n"); 17 | } 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /Continue statement: -------------------------------------------------------------------------------- 1 | // Program to calculate the sum of numbers (10 numbers max) 2 | // If the user enters a negative number, it's not added to the result 3 | 4 | #include 5 | int main() { 6 | int i; 7 | double number, sum = 0.0; 8 | 9 | for (i = 1; i <= 10; ++i) { 10 | printf("Enter a n%d: ", i); 11 | scanf("%lf", &number); 12 | 13 | if (number < 0.0) { 14 | continue; 15 | } 16 | 17 | sum += number; // sum = sum + number; 18 | } 19 | 20 | printf("Sum = %.2lf", sum); 21 | 22 | return 0; 23 | } 24 | -------------------------------------------------------------------------------- /Break statement: -------------------------------------------------------------------------------- 1 | // Program to calculate the sum of numbers (10 numbers max) 2 | // If the user enters a negative number, the loop terminates 3 | 4 | #include 5 | 6 | int main() { 7 | int i; 8 | double number, sum = 0.0; 9 | 10 | for (i = 1; i <= 10; ++i) { 11 | printf("Enter n%d: ", i); 12 | scanf("%lf", &number); 13 | 14 | // if the user enters a negative number, break the loop 15 | if (number < 0.0) { 16 | break; 17 | } 18 | 19 | sum += number; // sum = sum + number; 20 | } 21 | 22 | printf("Sum = %.2lf", sum); 23 | 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /Convert a binary to a decimal using for loop and without using array: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void main() 4 | { int n1, n,p=1; 5 | int dec=0,i=1,j,d; 6 | 7 | printf("\n\n Convert Binary to Decimal:\n "); 8 | printf("-------------------------\n"); 9 | 10 | printf("Input a binary number :"); 11 | scanf("%d",&n); 12 | n1=n; 13 | for (j=n;j>0;j=j/10) 14 | { 15 | d = j % 10; 16 | if(i==1) 17 | p=p*1; 18 | else 19 | p=p*2; 20 | 21 | dec=dec+(d*p); 22 | i++; 23 | } 24 | printf("\nThe Binary Number : %d\nThe equivalent Decimal Number : %d \n\n",n1,dec); 25 | } 26 | -------------------------------------------------------------------------------- /Print the string after replacing all occurrences of the character with another character in the string.: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | char s[1000],c1,c2; 6 | int i; 7 | 8 | printf("Enter the string : "); 9 | gets(s); 10 | 11 | printf("Enter a character replace: "); 12 | c1=getchar(); 13 | getchar(); 14 | printf("\nEnter character to replace with %c : ",c1); 15 | c2=getchar(); 16 | printf("\n before replace:%s",s); 17 | 18 | for(i=0;s[i];i++) 19 | { 20 | if(s[i]==c1) 21 | { 22 | s[i]=c2; 23 | 24 | } 25 | 26 | } 27 | 28 | printf("\nafter replace:%s",s); 29 | 30 | 31 | return 0; 32 | } 33 | -------------------------------------------------------------------------------- /Full Pyramid of Numbers: -------------------------------------------------------------------------------- 1 | #include 2 | int main() { 3 | int i, space, rows, k = 0, count = 0, count1 = 0; 4 | printf("Enter the number of rows: "); 5 | scanf("%d", &rows); 6 | for (i = 1; i <= rows; ++i) { 7 | for (space = 1; space <= rows - i; ++space) { 8 | printf(" "); 9 | ++count; 10 | } 11 | while (k != 2 * i - 1) { 12 | if (count <= rows - 1) { 13 | printf("%d ", i + k); 14 | ++count; 15 | } else { 16 | ++count1; 17 | printf("%d ", (i + k - 2 * count1)); 18 | } 19 | ++k; 20 | } 21 | count1 = count = k = 0; 22 | printf("\n"); 23 | } 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /GO-TO STATEMENT: -------------------------------------------------------------------------------- 1 | #include 2 | int main() 3 | { 4 | int num,i=1; 5 | printf("Enter the number whose table you want to print?"); 6 | scanf("%d",&num); 7 | table: 8 | printf("%d x %d = %d\n",num,i,num*i); 9 | i++; 10 | if(i<=10) 11 | goto table; 12 | } 13 | 14 | 15 | #include 16 | int main() 17 | { 18 | int i, j, k; 19 | for(i=0;i<10;i++) 20 | { 21 | for(j=0;j<5;j++) 22 | { 23 | for(k=0;k<3;k++) 24 | { 25 | printf("%d %d %d\n",i,j,k); 26 | if(j == 3) 27 | { 28 | goto out; 29 | } 30 | } 31 | } 32 | } 33 | out: 34 | printf("came out of the loop"); 35 | } 36 | -------------------------------------------------------------------------------- /reverse the elements of an array: -------------------------------------------------------------------------------- 1 | #include 2 | //Calculate array size 3 | #define ARRAY_SIZE(a) sizeof(a)/sizeof(a[0]) 4 | int main() 5 | { 6 | int arr[] = {1,2,3,4,5,6}; 7 | // length of the array 8 | int N = ARRAY_SIZE(arr); 9 | // assign the 0 valid index 10 | int start = 0; 11 | // assign the last valid index 12 | int end = (N - 1); 13 | int tmp, i; 14 | while(start < end) 15 | { 16 | // swap the elements 17 | tmp = arr[start]; 18 | arr[start] = arr[end]; 19 | arr[end] = tmp; 20 | start++; 21 | end--; 22 | } 23 | // print the reversed array 24 | for( i = 0; i < N; i++) 25 | { 26 | printf("%d ", arr[i]); 27 | } 28 | return 0; 29 | } 30 | -------------------------------------------------------------------------------- /Find largest and smallest element in array: -------------------------------------------------------------------------------- 1 | #include 2 | //Calculate array size 3 | #define ARRAY_SIZE(a) sizeof(a)/sizeof(a[0]) 4 | int main() 5 | { 6 | int arr[] = {3, 18, 10, 4, 2, 22, 150}; 7 | int i, small, large; 8 | const int N = ARRAY_SIZE(arr); 9 | small = arr[0];//Assume first element is smallest 10 | large = arr[0];//Assume first element is largest 11 | //iterate through the array 12 | for (i = 1; i < N; i++) 13 | { 14 | if (arr[i] < small) 15 | { 16 | small = arr[i]; 17 | } 18 | if (arr[i] > large) 19 | { 20 | large = arr[i]; 21 | } 22 | } 23 | printf("Largest element is : %d\n", large); 24 | printf("Smallest element is : %d\n", small); 25 | return 0; 26 | } 27 | -------------------------------------------------------------------------------- /Using Function: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void replacechar(char *s,char c1,char c2) 4 | { 5 | int i=0; 6 | 7 | 8 | for(i=0;s[i];i++) 9 | { 10 | if(s[i]==c1) 11 | { 12 | s[i]=c2; 13 | 14 | } 15 | 16 | } 17 | 18 | 19 | } 20 | 21 | 22 | int main() 23 | { 24 | 25 | char s[1000],c1,c2; 26 | 27 | printf("Enter the string : "); 28 | gets(s); 29 | 30 | printf("Enter a character replace: "); 31 | c1=getchar(); 32 | getchar(); 33 | printf("\nEnter character to replace with %c : ",c1); 34 | c2=getchar(); 35 | printf("\n before replace:%s",s); 36 | replacechar(s,c1,c2); 37 | 38 | 39 | printf("\nafter replace:%s",s); 40 | 41 | 42 | return 0; 43 | 44 | 45 | 46 | } 47 | -------------------------------------------------------------------------------- /Write a program in C to convert a decimal number into binary without using an array: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | char *decimal_to_binary(int); 4 | char *decimal_to_binary(int dn) 5 | { 6 | int i, j, temp; 7 | char *ptr; 8 | temp = 0; 9 | ptr = (char*)malloc(32+1); 10 | for (i = 31 ; i >= 0 ; i--) 11 | { 12 | j = dn >> i; 13 | if (j & 1) 14 | *(ptr+temp) = 1 + '0'; 15 | else 16 | *(ptr+temp) = 0 + '0'; 17 | temp++; 18 | } 19 | *(ptr+temp) = '\0'; 20 | return ptr; 21 | } 22 | int main() 23 | { 24 | int dn; 25 | char *ptr; 26 | printf("Input a decimal number: "); 27 | scanf("%d", &dn); 28 | ptr = decimal_to_binary(dn); 29 | printf("Binary number equivalent to said decimal number is: %s", ptr); 30 | free(ptr); 31 | return 0; 32 | } 33 | 34 | -------------------------------------------------------------------------------- /1D array using the dynamic memory allocation: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #define FAIL 1 4 | #define TRUE 0 5 | int main(int argc, char *argv[]) 6 | { 7 | int *piBuffer = NULL; //pointer to integer 8 | int nBlock = 0; //Variable store number of block 9 | int iLoop = 0; //Variable for looping 10 | printf("\nEnter the number of block = "); 11 | scanf("%d",&nBlock); //Get input for number of block 12 | piBuffer = (int *)malloc(nBlock * sizeof(int)); 13 | //Check memory validity 14 | if(piBuffer == NULL) 15 | { 16 | return FAIL; 17 | } 18 | //copy iLoop to each block of 1D Array 19 | for (iLoop =0; iLoop < nBlock; iLoop++) 20 | { 21 | piBuffer[iLoop] = iLoop; 22 | } 23 | //Print the copy data 24 | for (iLoop =0; iLoop < nBlock; iLoop++) 25 | { 26 | printf("\npcBuffer[%d] = %d\n", iLoop,piBuffer[iLoop]); 27 | } 28 | // free allocated memory 29 | free(piBuffer); 30 | return TRUE; 31 | } 32 | -------------------------------------------------------------------------------- /Program to Find Roots of a Quadratic Equation: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | int main() { 4 | double a, b, c, discriminant, root1, root2, realPart, imagPart; 5 | printf("Enter coefficients a, b and c: "); 6 | scanf("%lf %lf %lf", &a, &b, &c); 7 | 8 | discriminant = b * b - 4 * a * c; 9 | 10 | // condition for real and different roots 11 | if (discriminant > 0) { 12 | root1 = (-b + sqrt(discriminant)) / (2 * a); 13 | root2 = (-b - sqrt(discriminant)) / (2 * a); 14 | printf("root1 = %.2lf and root2 = %.2lf", root1, root2); 15 | } 16 | 17 | // condition for real and equal roots 18 | else if (discriminant == 0) { 19 | root1 = root2 = -b / (2 * a); 20 | printf("root1 = root2 = %.2lf;", root1); 21 | } 22 | 23 | // if roots are not real 24 | else { 25 | realPart = -b / (2 * a); 26 | imagPart = sqrt(-discriminant) / (2 * a); 27 | printf("root1 = %.2lf+%.2lfi and root2 = %.2f-%.2fi", realPart, imagPart, realPart, imagPart); 28 | } 29 | 30 | return 0; 31 | } 32 | --------------------------------------------------------------------------------