Let's see the Top 15 Numbers program in c langauage.
Best Way To Learn C Programming free
Basic For Loop program in c language
Top 10 Basic C Programs For Beginners
1. Program to find average of N Numbers
Below is a program to calculate average of n numbers.
#include<stdio.h>
int main()
{
printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");
int n, i;
float sum = 0, x;
printf("Enter number of elements: ");
scanf("%d", &n);
printf("\n\n\nEnter %d elements\n\n", n);
for(i = 0; i < n; i++)
{
scanf("%f", &x);
sum += x;
}
printf("\n\n\nAverage of the entered numbers is = %f", (sum/n));
printf("\n\n\n\n\t\t\tCoding is Fun !\n\n\n");
return 0;
}
2.Program to find Armstrong Number between 1 to 500
An Armstrong number or Narcissistic number is a n digit number such that the sum of its digits raised to the nth power is equal to the number itself.
For example, Let's take an armstrong number: 153, which is 3 digit number, here 13 + 53 + 33 is 1 + 125 + 27 which is equal to 153.
Program to find armstrong numbers between 1 to 500.
#include<stdio.h>
#include<math.h>
int main()
{
printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");
int n,sum,i,t,a;
printf("\n\n\nThe Armstrong numbers in between 1 to 500 are : \n\n\n");
for(i = 1; i <= 500; i++)
{
t = i; // as we need to retain the original number
sum = 0;
while(t != 0)
{
a = t%10;
sum += a*a*a;
t = t/10;
}
if(sum == i)
printf("\n\t\t\t%d", i);
}
printf("\n\n\n\n\t\t\tCoding is Fun !\n\n\n");
return 0;
}
2. Checking for Odd and Even Numbers using Bitwise Operator
Below is a program to find whether a number is even or odd using bitwise operator.
x&1 returns true if the LSB(Least significant Bit) of binary representation of an integer x is 1. It returns false if the LSB or the Right most bit in a binary sequence is 0. In binary representation of an integer, if LSB is 1 then it is odd and if LSB is 0 then it is even.
#include<stdio.h>
int main()
{
printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");
int x;
for(x = 0; x <= 10; x++)
{
if(x&1) // if number is odd
printf("\t\t\t%d is odd\n",x);
else if(!(x&1)) // ! is used inside if to reverse the boolevalue
printf("\t\t\t%d is even\n",x);
}
printf("\n\n\n\n\t\t\tCoding is Fun !\n\n\n");
return 0;
}
3. Program to find Factors of a Number
Below is a program to find factors of a number.
#include<stdio.h>
int main()
{
printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");
int num, i;
printf("Enter the number to find the factors of : ");
scanf("%d",&num);
printf("\n\n\nFactors of %d are \n\n", num);
for(i = 1; i <= num/2; i++)
{
if(num%i == 0)
printf("\t\t\t%d\n", i);
}
printf("\n\n\n\n\t\t\tCoding is Fun !\n\n\n");
return 0;
}
4. Program to find Sum of N input Numbers in C
Below is a program on sum of n numbers.
#include<stdio.h>
int main()
{
printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");
int n,sum=0,c,value;
printf("\n\nEnter the number of integers you want to add: ");
scanf("%d", &n);
printf("Enter %d integers\n\n",n);
for(c = 1; c <= n; c++)
{
scanf("%d", &value);
/*
need to initialise sum before using otherwise
garbage value will get printed
*/
sum += value;
}
printf("\n\n\nsum of entered numbers = %d", sum);
printf("\n\n\t\t\tCoding is Fun !\n\n\n");
return 0;
}
5. Program to find first N Prime Numbers
program to find first n prime numbers using nested for loops, where the value of n is input by the user.
#include<stdio.h>
int main()
{
printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");
int n,i = 3, count, c;
printf("\nEnter the number of prime numbers required : ");
scanf("%d", &n);
if(n >= 1)
{
printf("\n\nFirst %d prime numbers are : ", n);
printf("2 ");
}
// iteration for n prime numbers
// i is the number to be checked in each iteration starting from 3
for(count = 2; count <= n; i++)
{
// iteration to check c is prime or not
for(c = 2; c < i; c++)
{
if(i%c == 0)
break;
}
if(c == i) // c is prime
{
printf("%d ", i);
count++; // increment the count of prime numbers
}
6. Program to find the Largest number among n input Numbers
program to find largest number among n user input numbers.
#include<stdio.h>
int main()
{
printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");
int n,i;
float c,big;
printf("\n\nEnter the number of elements you wish to find the greatest element of: ");
scanf("%d", &n);
printf("\n\nEnter %d numbers :\n", n);
printf("\n\n\t\t\tElement 1: ");
//Important step- always initialize big to the first element
scanf("%f", &big);
for(i = 2; i <= n; i++)
{
printf("\n\t\t\tElement %d : ", i);
scanf("%f", &c);
/*
if input number is larger than the
current largest number
*/
if(big < c)
big = c; // update big to the larger value
}
printf("\n\n\nThe largest of the %d numbers is %f ", n, big);
printf("\n\n\n\n\t\t\tCoding is Fun !\n\n\n");
return 0;
}
}
printf("\n\n\n\n\t\t\tCoding is Fun !\n\n\n");
return 0;
}
7. Program to find exponential without using pow() method
Below is a program to find exponential without using pow() method.
long long int is of double the size of long int.
%lld is the format specifier for long long int.
#include<stdio.h>
int main()
{
printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");
int n, exp, exp1;
long long int value = 1;
printf("Enter the number and its exponential:\n\n");
scanf("%d%d",&n, &exp);
exp1 = exp; // storing original value for future use
// same as while((--exp)!=-1)
while(exp-- > 0)
{
value *= n; // multiply n to itself exp times
}
printf("\n\n %d^%d = %lld\n\n", n, exp1, value);
printf("\n\n\n\n\t\t\tCoding is Fun !\n\n\n");
return 0;
}
8. Program to check if input Number is int or float
Below is a program to check whether the user input number is of integer or float datatype.
#include<stdio.h>
#include<conio.h>
#include<string.h>
int main()
{
printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");
char number[10];
int flag = 0;
int length, i = 0;
printf("\n\nEnter a number: ");
scanf("%s", number);
length = strlen(number);
// till string does not end
while(number[i++] != '\0') // same as while(length-->0)
{
if(number[i] == '.') // decimal point is present
{
flag = 1;
break;
}
}
// if(0) is same as if(false)
if(flag)
printf("\n\n\n\tEntered Number is a Floating point Number\n\n");
else
printf("\n\n\n\tEntered Number is a integer Number\n\n");
printf("\n\n\n\n\t\t\tCoding is Fun !\n\n\n");
return 0;
}
9. Program to print the Multiplication Table of any Number
Below is a program to print the multiplication table of any user input number.
#include<stdio.h>
int main()
{
printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");
int n,i;
printf("Enter an integer you need to print the table of: ");
scanf("%d", &n);
printf("\n\n\n");
for(i = 1; i <= 10; i++)
{
printf("\n\t\t\t%d * %d = %d \n", n, i, n*i);
}
printf("\n\n\n\n\t\t\tCoding is Fun !\n\n\n");
return 0;
}