wisdomatrix.com

Avatar

C Program to Count Each Elements In Array

Below is a simple C program that counts the occurrences of each element in an array. This program takes an array as input from the user, counts the occurrences of each element, and then prints the count for each unique element in the array. It uses a nested loop to iterate through the array and count occurrences. Program : #include <stdio.h>int main(){int arr[100], freq[100];int size, i, j, count;printf(“Enter size of array: “);scanf(“%d”, &size);printf(“Enter elements in array: “);for(i=0; i<size; i++){scanf(“%d”, &arr[i]);freq[i] = -1;}for(i=0; i<size; i++){count = 1;for(j=i+1; j<size; j++){if(arr[i]==arr[j]){count++;freq[j] = 0;}}if(freq[i] != 0){freq[i] = count;}}printf(“\nFrequency of all elements of array : \n”);for(i=0; i<size; i++){if(freq[i] != 0){printf(“%d occurs %d times\n”, arr[i], freq[i]);}}return 0;} Output :

C Program to Count Each Elements In Array Read More »

C Program to Find the Duplicate Elements in an Array

To find duplicate elements in an array using a C program,you can use nested loops or hash sets. I’ll provide you with a simple example using nested loops. Here’s a sample program. Program : #include<stdio.h>#include<conio.h> void main(){int i,arr[20],j,no; printf(“Enter size of array: “);scanf(“%d”,&no);printf(“Enter any %d elements in array: “,no);for(i=0;i<no;i++){scanf(“%d”,&arr[i]);}printf(“Duplicate elements are: “);for(i=0; i<no; i++){for(j=i+1;j<no;j++){if(arr[i]==arr[j]){printf(“%d\n”,arr[i]);}}}getch();} Output :

C Program to Find the Duplicate Elements in an Array Read More »

C Program to Reverse an Array Elements

Reversing an array in C involves swapping the elements of the array in a way that the first element becomes the last, the second becomes the second-to-last, and so on. Here’s a simple C program that reverses an array Program : #include<stdio.h>void main(){int i,length;printf(“Enter the Length of array : “);scanf(“%d”,&length);int arr[length];printf(“Enter elements into the array:”);for(i=0;i<length;i++){printf(“arr[%d] :”,i);scanf(“%d”,&arr[i]);}printf(“\nOriginal array: \n”); for ( i = 0; i < length; i++) { printf(“%d “, arr[i]); } printf(“\nArray in reverse order: \n”); for ( i = length-1; i >= 0; i–) { printf(“%d “, arr[i]); } return 0; } Output :

C Program to Reverse an Array Elements Read More »

C Program to Convert a Given Number to Word

This program first takes a number from the user. Then, it uses a loop to convert the number to words. The loop iterates over the number, and for each digit, it prints the corresponding word from the words array. Finally, the program prints a newline character. Program : #include<stdio.h> #include<conio.h> int main(){ int n,sum=0,r; printf(“Enter the number = “); scanf(“%ld”,&n); while(n>0) { r=n%10; sum=sum*10+r; n=n/10; } n=sum; while(n>0) { r=n%10; switch(r) { case 1: printf(“one “); break; case 2: printf(“two “); break; case 3: printf(“three “); break; case 4: printf(“four “); break; case 5: printf(“five “); break; case 6: printf(“six “); break; case 7: printf(“seven “); break; case 8: printf(“eight “); break; case 9: printf(“nine “); break; case 0: printf(“zero “); break; default: printf(“tttt”); break; } n=n/10; } return 0; } Output :

C Program to Convert a Given Number to Word Read More »

C Program to Find Factorial of a Given Number

Below is a simple C program to calculate the factorial of a given number. The factorial of a non-negative integer n is the product of all positive integers less than or equal to n. The factorial is denoted by ‘n!‘. Program : #include<stdio.h> #include<conio.h> int main() { int i,n,fact=1; printf(“Enter a number : “); scanf(“%d”,&n); for(i=1;i<=n;i++) { fact=fact*i; } printf(“Factorial of %d is : %d\n”,n,fact);   } Output :

C Program to Find Factorial of a Given Number Read More »

C Program to Display Factor of a Given Number

You can run this program by compiling it using a C compiler. When you run the program, it will prompt you to enter a number, and then it will display the factors of that number. Factors are the numbers that divide the given number without leaving a remainder. Program : #include <stdio.h>#include<conio.h>int 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);}}return 0;} Output :

C Program to Display Factor of a Given Number Read More »

C Program to Sort Elements of an Array in Ascending and Descending Order

Sorting an array in ascending and descending order is a common task in programming. Below is a simple C program that sorts elements of an array in both ascending and descending order using the bubble sort algorithm. Program : #include <stdio.h> #include<conio.h>int main() {int a[100],n,i,j;printf(“Array size: “);scanf(“%d”,&n);printf(“Elements: “); for(i=0;i<n;i++){scanf(“%d”,&a[i]);}for ( i = 0; i < n; i++) {for ( j = 0; j < n; j++) {if (a[j] > a[i]) {int tmp = a[i]; a[i] = a[j]; a[j] = tmp; } }}printf(“\n\nAscending : “); for ( i = 0; i < n; i++) {printf(” %d “, a[i]);}for ( i = 0; i < n; i++) {for ( j = 0; j < n; j++) {if (a[j] < a[i]) {int tmp = a[i]; a[i] = a[j]; a[j] = tmp; }}}printf(“\n\nDescending : “); for ( i = 0; i < n; i++) {printf(” %d “, a[i]); }return 0; getch();} Output :

C Program to Sort Elements of an Array in Ascending and Descending Order Read More »

C Program to Create a Student Marksheet

Below is a simple example of a C program to create a student marksheet. This program takes input for the student’s name, roll number, and marks in three subjects, and then calculates the total marks and percentage. Program : #include<stdio.h>#include<conio.h>int main(){int a,b,c,d,e,total;float average;printf(“Enter the Marks of First Subject : “);scanf(“%d”,&a);printf(“Enter the Marks of Second Subject : “);scanf(“%d”,&b);printf(“Enter the Marks of Third Subject : “);scanf(“%d”,&c);printf(“Enter the Marks of Fourth Subject : “);scanf(“%d”,&d);printf(“Enter the Marks of Fifth Subject : “);scanf(“%d”,&e);total=a+b+c+d+e;printf(“Total marks of the Student : %d/500”,total);average=total/5;printf(“\nAverage : %f\n”,average);if((average>=90)&&(average<=100)){printf(“Grade = A++”);}else if((average>=85)&&(average<=89)){printf(“Grade = A+”);}else if((average>=80)&&(average<=84)){printf(“Grade = A”);}else if((average>=75)&&(average<=79)){printf(“Grade = B+”);}else if((average>=70)&&(average<=74)){printf(“Grade = B”);}else if((average>=65)&&(average<=69)){printf(“Grade = C+”);}else if((average>=60)&&(average<=64)){printf(“Grade = C”);}else if((average>=55)&&(average<=59)){printf(“Grade = D+”);}else if((average>=50)&&(average<=54)){printf(“Grade = D”);}else if((average>=45)&&(average<=49)){printf(“Grade = E+”);}else if((average>=30)&&(average<=44)){printf(“Grade = E”);}else {printf(“Grade = Fail”);}return 0;} Output :

C Program to Create a Student Marksheet Read More »

Shopping Basket
Verified by MonsterInsights