Publicidad

Basic c programs updated on 31.8.2020

vrgokila
31 de Aug de 2020
Publicidad

Más contenido relacionado

Publicidad
Publicidad

Basic c programs updated on 31.8.2020

  1. INTRODUCTION TO C PROGRAMMING Basic c programs Updated on 31.8.2020 #include<stdio.h> int main() { int n1=0,n2=1,n3,i,number; printf("Enter the number of elements:"); scanf("%d",&number); printf("n%d %d",n1,n2);//printing 0 and 1 for(i=2;i<number;++i)/*loop starts from 2 because 0 and 1 are alr eady printed*/ { n3=n1+n2; printf(" %d",n3); n1=n2; n2=n3; } return 0; }
  2. #inlcude<stdio.h> #inlcude<conio.h> void main() { int r=5; float area; area=3.14*r*r; printf("area:%d",area); getch(); } BASIC C PROGRAMS 1.Write a program to print the position of the smallestnumber of n numbers using arrays. #include <stdio.h> #include <conio.h> int main() { int i, n, arr[20], small, pos; clrscr(); printf("n Enter the number of elements in the array : "); scanf("%d", &n); printf("n Enter the elements : "); for(i=0;i<n;i++) scanf("%d",&arr[i]);
  3. small = arr[0] pos =0; for(i=1;i<n;i++) { if(arr[i]<small) { small = arr[i]; pos = i; } } printf("n The smallest element is : %d", small); printf("n The position of the smallest element in the array is : %d", pos); return 0; } 2. Write a program to insert a number at a given locationin an array. #include <stdio.h> #include <conio.h> int main() { int i, n, num, pos, arr[10]; clrscr(); printf("n Enter the number of elements in the array : "); scanf("%d", &n); for(i=0;i<n;i++) { printf("n arr[%d] = ", i); scanf("%d", &arr[i]); } printf("n Enter the number to be inserted : "); scanf("%d", &num); printf("n Enter the position at which the number has to be added : "); scanf("%d", &pos); for(i=n–1;i>=pos;i––) arr[i+1] = arr[i]; arr[pos] = num; n = n+1; printf("n The array after insertion of %d is : ", num); for(i=0;i<n;i++) printf("n arr[%d] = %d", i, arr[i]); getch(); return 0; } Output Enter the number of elements in the array : 5 arr[0] = 1 arr[1] = 2 arr[2] = 3 arr[3] = 4 arr[4] = 5 Enter the number to be inserted : 0 Enter the position at which the number has to be added : 3 The array after insertion of 0 is : arr[0] = 1 arr[1] = 2 arr[2] = 3 arr[3] = 0 arr[4] = 4
  4. arr[5] = 5 3.Write a program to insert a number in an array that is already sortedin ascending order. #include <stdio.h> #include <conio.h> int main() { int i, n, j, num, arr[10]; clrscr(); printf("n Enter the number of elements in the array : "); scanf("%d", &n); for(i=0;i<n;i++) { printf("n arr[%d] = ", i); scanf("%d", &arr[i]); } printf("n Enter the numberto be inserted : "); scanf("%d", &num); for(i=0;i<n;i++) { if(arr[i] > num) { for(j = n–1; j>=i; j––) arr[j+1] = arr[j]; arr[i] = num; break; } } n = n+1; printf("n The array after insertion of %d is : ", num); for(i=0;i<n;i++) printf("n arr[%d] = %d", i, arr[i]); getch(); return 0; } Output Enter the number of elements in the array : 5 arr[0] = 1 arr[1] = 2 arr[2] = 4 arr[3] = 5 arr[4] = 6 Enter the number to be inserted : 3 The array after insertion of 3 is : arr[0] = 1 arr[1] = 2 arr[2] = 3 arr[3] = 4 arr[4] = 5 arr[5] = 6 4.Write a program to delete a number from a given locationin an array. #include <stdio.h> #include <conio.h> int main() { int i, n, pos, arr[10]; clrscr(); printf("n Enter the number of elements in the array : "); scanf("%d", &n);
  5. for(i=0;i<n;i++) { printf("n arr[%d] = ", i); scanf("%d", &arr[i]); } printf("nEnter the position from which the number has to be deleted : "); scanf("%d", &pos); for(i=pos; i<n–1;i++) arr[i] = arr[i+1]; n––; printf("n The array after deletion is : "); for(i=0;i<n;i++) printf("n arr[%d] = %d", i, arr[i]); getch(); return 0; } Output Enter the number of elements in the array : 5 arr[0] = 1 arr[1] = 2 arr[2] = 3 arr[3] = 4 arr[4] = 5 Enter the position from which the number has to be deleted : 3 The array after deletion is : arr[0] = 1 arr[1] = 2 arr[2] = 3 arr[3] = 5 5. Write a program to merge two unsorted arrays. #include <stdio.h> #include <conio.h> int main() if(arr1[index_first]<arr2[index_second]) { arr3[index] = arr1[index_first]; index_first++; } else { arr3[index] = arr2[index_second]; index_second++; } index++; } // if elements of the first array are over and the second array has some elements if(index_first == n1) { while(index_second<n2) { arr3[index] = arr2[index_second]; index_second++; index++; } } // if elements of the second array are over and the first array has some elements else if(index_second == n2) { while(index_first<n1) { arr3[index] = arr1[index_first];
  6. index_first++; index++; } } printf("nn The merged array is"); for(i=0;i<m;i++) printf("n arr[%d] = %d", i, arr3[i]); getch(); return 0; } Output Enter the number of elements in array1 : 3 Enter the elements of the first array arr1[0] = 1 arr1[1] = 3 arr1[2] = 5 Enter the number of elements in array2 : 3 Enter the elements of the second array arr2[0] = 2 arr2[1] = 4 arr2[2] = 6 //Program to Printan Integer #include <stdio.h> int main() { int number; printf("Enter an integer: "); // reads and stores input scanf("%d", &number); // displays output printf("You entered: %d", number); return 0; } //Program to Add TwoIntegers #include <stdio.h> int main() { int number1, number2, sum; printf("Enter two integers: "); scanf("%d %d", &number1, &number2); // calculating sum sum = number1 + number2; printf("%d + %d = %d", number1, number2, sum); return 0; } //Program to Multiply Two Numbers #include <stdio.h> int main() { double a, b, product; printf("Enter two numbers: ");
  7. scanf("%lf %lf", &a, &b); // Calculating product product= a * b; // Result up to 2 decimal point is displayed using %.2lf printf("Product = %.2lf", product); return 0; } //Swap Numbers Using Temporary Variable #include<stdio.h> int main() { double first, second, temp; printf("Enter first number: "); scanf("%lf", &first); printf("Enter second number: "); scanf("%lf", &second); // Value of first is assigned to temp temp = first; // Value of second is assigned to first first = second; // Value of temp (initial value of first) is assigned to second second = temp; printf("nAfter swapping, firstNumber = %.2lfn", first);printf("After swapping, secondNumber = %.2lf", second); return 0; } Programto Check Vowelor consonant #include <stdio.h> int main() { char c; int lowercase_vowel, uppercase_vowel; printf("Enter an alphabet: "); scanf("%c", &c); // evaluates to 1 if variable c is a lowercase vowel lowercase_vowel = (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u'); // evaluates to 1 if variable c is a uppercasevowel uppercase_vowel= (c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U'); // evaluates to 1 (true) if c is a vowel if (lowercase_vowel || uppercase_vowel) printf("%c is a vowel.", c); else printf("%c is a consonant.", c); return 0;
  8. } Check Armstrong Number of three digits //153 = 1*1*1 + 5*5*5 + 3*3*3 #include <stdio.h> int main() { int n, i; printf("Enter an integer: "); scanf("%d", &n); for (i = 1; i <= 10; ++i) { printf("%d * %d = %d n", n, i, n * i); } return 0; } #include <stdio.h> int main() { int num, originalNum, remainder, result = 0; printf("Enter a three-digit integer: "); scanf("%d", &num); originalNum = num; while (originalNum != 0) { // remainder contains the last digit remainder = originalNum % 10; result += remainder * remainder * remainder; // removing last digit from the orignal number originalNum /= 10;} if (result == num) printf("%d is an Armstrong number.", num); else printf("%d is not an Armstrong number.", num); return 0; } Programto Check Palindrome #include <stdio.h> int main() { int n, reversedN = 0, remainder, originalN; printf("Enter an integer: "); scanf("%d", &n); originalN = n; // reversed integer is stored in reversedN while (n != 0) { remainder = n % 10; reversedN = reversedN * 10 + remainder; n /= 10; } // palindrome if originalN and reversedN are equal if (originalN == reversedN) printf("%d is a palindrome.", originalN); else
  9. printf("%d is not a palindrome.", originalN); return 0; } //ARRAY //Example 2: Sum of two matrices // C program to find the sum of two matrices of order 2*2 #include <stdio.h> int main() { float a[2][2], b[2][2], result[2][2]; // Taking input using nested for loop printf("Enter elements of 1st matrixn"); for (int row = 0; row < 2; ++row) for (int col= 0; col< 2; ++col) { printf("Enter a%d%d:", row + 1, col+ 1); scanf("%f", &a[row][col]); } // Taking input using nested for loop printf("Enter elements of 2nd matrixn"); for (int row = 0; row < 2; ++row) for (int col= 0; col< 2; ++col) { printf("Enter b%d%d:", row + 1, col+ 1); scanf("%f", &b[row][col]); } // adding correspondingelements of two arrays for (int row = 0; row < 2; ++row) for (int col= 0; col< 2; ++col) { result[row][col] = a[row][col] + b[row][col]; } // Displaying the sum printf("nSum Of Matrix:"); for (int row = 0; row < 2; ++row) for (int col= 0; col< 2; ++col) { printf("%.1ft", result[row][col]); if (col == 1) printf("n"); } return 0; }
  10. Program to Find the Transpose of a Matrix #include <stdio.h> int main() { int a[10][10], transpose[10][10], r, c, i, j; printf("Enter rows and columns: "); scanf("%d %d", &r, &c); // Assigning elements to the matrix printf("nEnter matrix elements:n"); for (i = 0; i < r; ++i) for (j = 0; j < c; ++j) { printf("Enter element a%d%d: ", i + 1, j + 1); scanf("%d", &a[i][j]); } // Displaying the matrix a[][] printf("nEntered matrix: n"); for (i = 0; i < r; ++i) for (j = 0; j < c; ++j) { printf("%d ", a[i][j]); if (j == c - 1) printf("n"); } // Finding the transpose of matrix a for (i = 0; i < r; ++i) for (j = 0; j < c; ++j) {transpose[j][i] = a[i][j]; } // Displaying the transpose of matrix a printf("nTranspose of the matrix:n"); for (i = 0; i < c; ++i) for (j = 0; j < r; ++j) { printf("%d ", transpose[i][j]); if (j == r - 1) printf("n"); } return 0; }
Publicidad