Posts

Showing posts with the label C programming Examples

Write A C Program To Delete A Single Element From An Array

#include <stdio.h> int main () {     int arr [ 10 ], n, i, position;     printf ( "Enter the size of array: \n " );     scanf ( " %d " , &n);     printf ( "Enter the elements of the array: \n " );     for (i= 0 ;i<n;i++)     {         scanf ( " %d " , & arr [i]);     }     printf ( "Enter the position which  element is delete: \n " );     scanf ( " %d " , &position);     if (position< 1 || position>n)     {         printf ( "Position is Unavailabe! \n " );         return 0 ;     } for (i=position- 1 ; i<n- 1 ;i++) { arr [i]= arr [i+ 1 ]; } n--; printf ( "Updated array is: \n " ); for (i= 0 ; i<n;i++){     printf ( " %d " , arr [i]); } printf ( " \n " ); return 0 ; } OUTPUT- Enter the size of array: 5 Enter the el...

Write a c program to find factorial of a number

 //c program to find factorial of a number #include <stdio.h> int main() { int n, i; unsigned long long fact =1; printf("Enter an integer: "); scanf("%d", &n); //show arror if the user enters a negative integer if(n<0) printf("Error ! factorial of a negative number doesnt exist."); else{ for(i=1; i<=n; ++i){ fact *= i; } printf("Factorial of %d= %llu", n, fact); } return 0; } This program is only use for educational purpose ,it can be helpful for Computer science  background and these Computer science programs are Runs in lab.

C program to calculate area and circumfrances of circle

 //C program to calculate area and circumfrances of circle #include <stdio.h> void main() { float radius, area, cf; printf("Enter radius of the circle: \n"); scanf("%f", &radius); //value of pi is 3.14 area=3.14*radius*radius; printf("The area of the circle is %f", area); cf=2*3.14*radius; printf("\nThe Circumfrances of circle is %f", cf); }

C program to find sum and average of n numbers using for loop

 //C program to find sum and average of n numbers using for loop #include <stdio.h> int main() { int i,n,sum=0,numbers; float average; printf("\nPlease Enter how many numbers you want?\n"); scanf("%d",&n); printf("\nPlease Enter the elements one by one\n"); for(i=0;i<n;++i) { scanf("%d",&numbers); sum=sum+numbers; } average=sum/n; printf("\nSum of the %d Numbers=%d",n,sum); printf("\nAverage of the %d Numbers=%.2f",n,average); return 0; }

Write a program in C to print Pass Addresses to functions

 //Pass Addresses to functions #include <stdio.h> void swap(int *n1, int *n2); int main() { int num1=5, num2=10; //address of num1 and num2 is passed swap(&num1, &num2); printf("num1=%d\n", num1); printf("num2=%d\n", num2); return 0; } void swap(int* n1, int* n2) { int temp; temp=*n1; *n1=*n2; *n2=temp; }

C program to count occurances of A Character in string C program

 //C program to count occurances of A Character in string C program #include <stdio.h> int main() { char s[1000],c; int i,count=0; printf("Enter the string: "); gets(s); printf("Enter character to be searched: "); c=getchar(); for(i=0;s[i];i++) { if(s[i]==c) { count++; } } printf("character '%c' occurs %d times \n",c,count); return 0; }

write a program in c to print pyramid in star in C

 //write a program in c to print pyramid in star #include <stdio.h> int main() { int i, space, rows, k=0; printf("Enter the number of rows: "); scanf("%d", &rows); for(i=1;i<=rows;++i,k=0) { for (space=1; space <= rows-i; ++space) { printf(" "); } while (k !=2*i-1){ printf("*"); ++k; } printf("\n"); } return 0; }

write a program to print all alphabets in C

 //write a program to print all alphabets #include <Stdio.h> int main() { char c; for(c = 'A';c <= 'Z'; ++c) printf("%c ", c); return 0; }

write a program to find factor of the given number in C

 //write a program to find factor of the given number #include <stdio.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; }

write a program to compile the sum of the first n terms of the following series s=1-2+3-4+5-6.....

 //write a program to compile the sum of the first n terms of the following series s=1-2+3-4+5-6..... #include <stdio.h> int main() { int n; int sum=0,i; printf("Enter the range of number."); scanf("%d",&n); for(i=1;i<=n;i++) { if(i%2==0) sum-=i; else sum+=i; } printf("The sum of the series=%d",sum); }

write a program to check given string is palindrome or not in C

 //write a program to check given string is palindrome or not #include <stdio.h> #include <string.h> int main() { char string[20]; int i, length, temp=0; int flag=0; printf("Enter a string: "); scanf("%s", string); length=strlen(string); for(i=0;i<length;i++) { if(string[i] !=string[length-i-1]){ temp = 1; break; } } if (temp==0) { printf("string is a palindrome"); } else{ printf("string is not a palindrome"); } return 0; }

write a program to print the sum of product of an integer in C

 //write a program to print the sum of product of an integer #include <stdio.h> int main() { int n, digit, sum, product; printf("\nEnter an integer number :"); scanf("%d", &n); /* Calculating sum and product */ sum=0; product=1; while(n>0) { digit=n%10; /*get digit*/ sum+=digit; product*=digit; n=n/10; } printf("\nSum of all digit is: %d", sum); printf("\nProduct of all digit: %d", product); return 0; }

write a program to all arithmetic operation using switch case in C

 //write a program to all arithmetic operation using switch case #include <stdio.h> int main() { int a,b; int op; printf(" 1.Addition\n 2.Substraction\n 3.Multiplication\n 4.Division\n"); printf("Enter the value of a & b: "); scanf("%d %d", &a, &b); printf("Enter your choice: "); scanf("%d", &op); switch(op) { case 1 : printf("Sum of %d and %d is: %d",a, b, a+b); break; case 2 : printf("Difference of %d and %d is: %d", a, b, a-b); break; case 3 : printf("Multiplication of %d and %d is: %d", a, b, a*b); break; case 4: printf("Division of two number is %d: ",a/b); break; default: printf("Enter your correct choice." ); break; } return 0; }

write a program in C to print reverse a number

  #include <stdio.h> int main() { int n, reverse=0, remainder; printf("Enter an integer: "); scanf("%d", &n); while(n !=0){ remainder = n%10; reverse=reverse*10+remainder; n /= 10; } printf("Reversed number = %d", reverse); return 0; } 

write a program in c to point the elements of an array in reverse order

 //write a program in c to point the elements of an array in reverse order  #include <stdio.h> int main() { int n, i; printf("Enter the size of an array: "); scanf("%d", &n); int arr[n]; // to take a input in array for(i = 0; i < n; i++); { printf("Please give value for index %d: ",i); scanf("%d", &arr[i]); } printf("array printing in reverse order is: \n"); //for loop to print array in reverse order for(i=n-1;i>=0;i--) { printf("%d\n",arr[i]); } return 0; }

Write program in C to make your own power function

 //make your own power function #include <stdio.h> int main() { int base, exp, result; printf("Enter a base number: "); scanf("%d", &base); printf("Enter an exponent: "); scanf("%d", &exp); result = pow (base,exp); printf("%d to the power of %d is=%d", base, exp, result); return 0; }

write a function to print hot or cold weather depending on the users enter in C

 // write a function to print hot or cold weather depending on the users enter #include <stdio.h> void main() { int temperature; printf("Input days temperature: "); scanf("%d", &temperature); if(temperature<0) printf("Freezing weather.\n"); else if(temperature<20) printf("Very cold weather.\n"); else if(temperature<30) printf("cold weather.\n"); else  printf("its very hot.\n"); }

Write a Program for calculate interest in C

 #include <stdio.h> int main() { float amount,rate,time,interest; printf("\n Enter amount is:"); scanf("%f", &amount); printf("\n Enter rate of interest:"); scanf("%f", &rate); printf("\n Enter time period:"); scanf("%f", &time); interest = (amount*rate*time)/100; printf("interest is: %f", interest); return 0;  }

Write a program in C to Find the area of triangle

 // Find the area of triangle #include <stdio.h> int main() { float B,H,area; printf("Enter Height of Triangle: \n"); printf("Enter Base of Triangle: \n"); scanf("%f", &H); scanf("%f", &B); area = B*H/2; printf("area of the triangle is= %f", area); return 0; } 

Write A function to find sum of digit of a number in c (without used of +)

 // Write A function to find sum of digit of a number in c (without used of +) #include <stdio.h> int main() { int n,sum=0,m; printf("Enter a number: "); scanf("%d", &n); while(n>0) { m=n%10; sum=sum+m; n=n/10; } printf("sum is =%d", sum); return 0; }