C Program to Find Percentage of given Subjects Marks using Array

Ex: Write a C program to find percentage of given subjects marks using array. How to write a C program to print percentages of given marks. C program to find percentage of given subjects marks.


Input from user:


Enter limit of subjects: 5


Enter marks of subject[1]:71

Enter marks of subject[2]:65
Enter marks of subject[3]:61
Enter marks of subject[4]:81
Enter marks of subject[5]:77

Expected output:


Percentage= 71.0000

C Program to Print all Negative Elements in Array

Ex: Write a C program to print all negative elements in array. How to write a C program which print all negative elements in array. C program to print all negative elements in array.


Input from user:

Enter the limit: 5

Enter the numbers:
10
-3
12
-9
-2

Expected output:

Negative elements are:
-3
-9
-2

C Program to print Sum of All Array Elements

Ex: Write a C program to print sum of all array elements. How to write a C program which print sum of all array elements. C program to print sum of all array elements.


Input from user:

Enter how many elements you are going to enter: 5

Enter the elements:
10 
20 
30 
40
50

Expected output:

Sum of all array elements= 150





Array in C Definition, Syntax, Working, Advantages & Disadvantages

Definition: Array is collection of similar data types. That means using array we can store similar type of elements.


In short array is  a variable which can store multiple values. For example, if you want to store 100 and more intigers, then you need to create an array for it.



Syntax:

C Program for Swap Two Numbers using Functions

Ex: Write a C program for swap two numbers using function. How to write a C program for swap two numbers using function. C program for swap two numbers using function.

Input from user:


Enter two numbers: 10 20


Expected output:


Numbers before swap:

number1=10
number2=20

Numbers after swaping:

number1=20
number2=10

C Program to print Square of given Number using Function

Ex: Write a C program to print square of given number using function. How to write a C program to print square of given number using function. C program to print square of given number using function.

Input from user:

Enter the number: 4

Expected output:

Square of given number is= 16.0000





  Step by Step logic of the given program:



1. First declare function give it meaningful name square().

2. Inside the main() accept input (number) from user declare varible say no.

3. Call the square() inside the printf() directly (It will print returned value means square of given number).

4. In the body of square() use simple logic of square which is-
Square=no*no;

5. After that return square.




  C Program to print Square of given Number using Function:


#include<stdio.h>
float square(float);

int main()
{
float no;
printf("Enter the number: ");
scanf("%f",&no);

printf("Square of given number is= %f",square(no));
}
float square(float no)
{
float square=no*no;
return square;
}


Above program shows the following output:


C Program to print Square of given Number using Function

C program to check given number is prime or not using function

Ex: Write a C program to check given number is prime or not using function. How to write a C program to check given number is prime or not using function. C program to check given number is prime or not using function.


Input from user:

Enter the number: 5

Expected output:

Given number is prime number.





  Step by Step logic of the given program:



1. First declare function give it meaningful name prime().

2. Inside the main() accept input (number) from user declare varible say no.

3. Using if statement call prime function- if(prime(no)). after that inside the if statement print given number is prime otherwise print number is not prime. (If returned value is 1(true) then it will print number is prime otherwise it will execute else block

4. After that inside the prime() we will use following prime numbers logic-
for(i=1;i<=no;i++)
{
if(no%i==0)
{
count++;
}
}

Here, count will be 2 only if number is prime.


5. In the last check count is 2 or not using- if(count==2) 
If it's true return 1 otherwise return 0;





  C program to check given number is prime or not using function:


#include<stdio.h>
int prime(int);
int main()
{
int no;

printf("Enter the number\n");
scanf("%d",&no);

if(prime(no))
    {
    printf("Given number is prime number");
    }
    else
    {
    printf("Given number is not prime number");
    }
}
int prime(int no)
{
int count=0,i;

for(i=1;i<=no;i++)
{
if(no%i==0)
{
count++;
}
}
if(count==2)
{
return 1;
}
else
{
return 0;
}
}


Above program shows the following output:


C program to check given number is prime or not using function



Feel freee to drop comments in the comment section given below...


    You May Like:

C program to print sum of two numbers using functions

Ex: Write a C program to print sum of two numbers using function. How to write a C program to print sum of two numbers using function. C program to print sum of two numbers using function.

Input from user:


Enter two numbers: 35 15


Expected output:


Sum = 50





  Step by step logic of the given program:


1. First declare function give it meaningful name add().


2. Inside the main() accept input (two numbers) from user, declare two varibles say no1 & no2 .

3. inside the sum pass no1 and no2 to the function using: sum= add(no1, no2);

4. After that print sum.


5. In the body of add() add n1 and n2 and store it some varible say sum.


After that return sum.







  C program to print sum of two numbers using functions:

#include<stdio.h>
int add(int n1,int n2); /*function declaration*/

int main()
{
    /*Variable declaration*/
    int no1,no2, sum;

    /*Input two numbers from user*/
    printf("Enter two numbers: ");
    scanf("%d%d", &no1,&no2);

    
    sum=add(no1, no2);

    /*Print value of sum*/
    printf("Sum = %d",sum);

    return 0;
}


int add(int n1,int n2)
{
    int sum= n1+n2;

    /*Return value of sum to the main*/
    return sum;
}


Above program shows the following output:


C program to print sum of two numbers using functions


C program to print factorial of given number using function

Ex: write a C program to print factorial of given number using function. How to write a C program to print factorial of the given number using function. C program to print factorial of given number using function.

Input from user:

Enter the number: 5

Expected output: 

Factorial of given number is 120.

C program to print given number is even or odd using function

Ex: write a C program to print given number is even or odd using function. How to write a C program to print given number is even or odd using function. C program to print given number is even or odd using function.


Input from user:

Enter the number: 24

Expected output:

Given number is even




  Step by step logic of the given program:


1. First declare function give meaningful name say even().

2.  Inside the main() accept input(number) from user, declare varible say no.

3.  Call function and store returned value in variable r.

4.  If  returned value is 0 if(r==0) print is even otherwise print is odd.

5. inside the function check number is even or odd using if(no%2==0) return 0 else return 1.





  C program to print given number is even or odd using function:


#include<stdio.h>
int even(int);

int main()

{
int no,r=0;
printf("Enter the number\n");
scanf("%d",&no);

r=even(no);

if(r==0)
printf("Given number is even");
else
printf("Given number is odd");
}
int even(int no)
{
if(no%2==0)
{
return 0;
}
else
{
return 1;
}
}



Above program shows the following output:


C program to print given number is even or odd using function



If you like this article, then don't forget to share it with your friends and colleagues. If you have any doubts and feedback then feel free to use below comment section.