C Programming

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 »

C Program to Find a Number in an Array

Below is a simple C program that searches for a given number in an array. The program prompts the user to enter the size of the array, the elements of the array, and the number to be searched. It then prints whether the number is found in the array or not. Program : #include <stdio.h> #include <conio.h>     int main() {     int a[10000],i,n,key;         printf(“Enter size of the  array : “);     scanf(“%d”, &n);     printf(“Enter elements in array : “);     for(i=0; i<n; i++)     {         scanf(“%d”,&a[i]);     }      printf(“Enter the key : “);     scanf(“%d”, &key);           for(i=0; i<n; i++)     {         if(a[i]==key)         { printf(“element found “);             return 0;           }       }      printf(“element  not  found”); } Output :

C Program to Find a Number in an Array Read More »

C Program to the Summation of the Numbers in an Array

This program initializes an array ‘arr’ with some values, calculates the number of elements in the array, and then uses a loop to iterate through the array, adding each element to the sum variable. Finally, it prints the sum to the console. You can modify the values in the ‘aar’ array or use a different array to test the program with other sets of numbers. Program : #include<stdio.h>int main(){int arr[100], size, i, sum = 0;printf(“Enter array size\n”);scanf(“%d”,&size);printf(“Enter array elements\n”);for(i = 0; i < size; i++)scanf(“%d”,&arr[i]);for(i = 0; i < size; i++)sum = sum + arr[i];printf(“Sum of the array = %d\n”,sum);return 0;} Output :

C Program to the Summation of the Numbers in an Array Read More »

C Program to Find Out the Duplicate Elements in Array

This program takes the size of the array as input and then takes input for each element of the array. The findDuplicates function is responsible for finding and printing the duplicate elements. It uses nested loops to compare each element with every other element in the array and prints the duplicates.Keep in mind that this program assumes that the array has integers as its elements. Additionally, it finds and prints duplicates without considering the order of appearance in the array. If you need a more specific solution, you may need to adjust the code accordingly. 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 Out the Duplicate Elements in Array Read More »

C Program to Sort a String by User Input

Sorting a string in C involves arranging its characters in either ascending or descending order based on their ASCII values. Here’s a simple program to sort a string in ascending order Program : #include <stdio.h>#include <string.h>int main (){char str[100], chTemp;char temp;int i, j,len;printf(“C Program to sort character in descending order\n”);printf(“Please enter the string : “);scanf(“%s”,str);len = strlen(str);for(i=0; i<len; i++){for(j=0; j<(len-1); j++){if(str[j]>str[j+1]){chTemp = str[j];str[j] = str[j+1];str[j+1] = chTemp;}}}printf(“\nSame string in ascending order: %s\n”, str); for (i = 0; i < len-1; i++) {for (j = i+1; j < len; j++) {if (str[i]< str[j]) {temp = str[j];str[j] = str[i];str[i] = temp;}}}printf(“After sorting character in descending order : %s”, str);return 0;} Output :

C Program to Sort a String by User Input Read More »

Shopping Basket
Verified by MonsterInsights