Category: Programming Examples in C

Search first occurrence of element in an array 9

Search first occurrence of element in an array

Write a program in C language that will accepts n of integer values from the user, accepts the integer element to be searched and displays the position of the element in the array. #include #include #include void main() { int arr[100],i,element,no; clrscr(); printf(“\nEnter the no of Elements: “); scanf(“%d”, &no); for(i=0;i

Addition of array elements in C 5

Addition of Array elements

Write a program in C that accepts number of array elements from the user, accepts array element values and calculates the sum/addition of all the array elements. The program should display the value of addition. #include<stdio.h> int main() { int arr[100], i, no,sum=0; clrscr(); printf(“\nEnter the no. of elements: “); scanf(“%d”,&no); for(i=0;i<no;i++) { printf(“\nEnter element %d”,i+1); scanf(“%d”, &arr[i]); } for(i=0;i<no;i++) { sum=sum+arr[i]; } printf(“\nsum of array element: %d”,sum); return 0; } Output: Enter the no. of elements: 5 Enter element 1 : 1 Enter element 2 : 2 Enter element 3 : 3 Enter element 4 : 4 Enter element...

Arithmetic calculator using switch statement 9

Arithmetic calculator using switch statement

#include #include void main() { int choice, no1, no2, result; clrscr(); printf(“\n\n*********************”); printf(“\nArithmetic Calculator”); printf(“\n*********************”); printf(“\n\n1. Addition\n2. Subtraction”); printf(“\n3. Multiplication\n4. Division”); printf(“\n\nEnter your choice: “); scanf(“%d”,&choice); switch(choice){ case 1: printf(“\nEnter the two number:”); scanf(“%d %d”, &no1, &no2); result = no1 + no2; printf(“\nThe Addition of %d and %d is %d”, no1, no2, result); break; case 2: printf(“\nEnter the two number:”); scanf(“%d %d”, &no1, &no2); result = no1 – no2; printf(“\nThe Subtaction of %d and %d is %d”, no1, no2, result); break; case 3: printf(“\nEnter the two number:”); scanf(“%d %d”, &no1, &no2); result = no1 * no2; printf(“\nThe Multiplication of %d...

Program to generate Fibonacci series 3

Program to generate Fibonacci series

#include #include #include void main() { clrscr(); //Declare an initialize the variables int firstNum = 0, secondNum = 1, fibNum, temp; //Accept a number from the user printf(“\nEnter the number: “); scanf(“%d”, &fibNum); /*Exit if value is less than 2*/ if(fibNum

Program to calculate Factorial of a given number 4

Program to calculate Factorial of a given number

#include<stdio.h> #include<stdlib.h> int main() { int num, fact = 1; /*Accept a number from the user*/ printf(“\nEnter the number: “); scanf(“%d”, &num); /*Calculate the factorial*/ for(int i=1; i<=num; i++) { fact = fact * i; } /*Display the result*/ printf(“The factorial of %d is %d”, num, fact); return 0; } Output: Enter the number: 5 The factorial of 5 is 120

FREE C Cheatsheet - Speed Up Your C Programming.

FREE C Cheatsheet - Speed Up Your C Programming.

Download a 7-page free cheat sheet for easy and quick access to C Concepts, Snippets, and Syntax.

Thank you! Check you inbox and access your cheat-sheet