Here is the collection of the most important C programs to increase your learning. This collection is also based on Grade XI and XII computer science courses and might help you a lot in your performance.
1. Program to find out the sum of the first 50 natural numbers.
#include <stdio.h>
void main()
{
int i,sum=0;
for(i=1;i<=50;i++)
{
sum=sum + i;
}
printf("The sum of 50 natural numbers is %i.",sum);
}
2. Program to enter a number and print its multiplication table up to 10 multiples.
We can do it in different ways:
Using do while loop :
#include <stdio.h>
void main()
{
int a,b=1,c;
printf("Enter the number to print its multiplication : ");
scanf("%d",&a);
do
{
c = a*b;
b++;
printf("%d\n",c);
}
while(b<=10);
}
Using for loop:
#include <stdio.h>
void main()
{
int n, i;
printf("Enter an Number to print its multiple: ");
scanf("%d", &n);
for (i = 1; i <= 10; i++)
{
printf("%d\n",i*n);
}
}
Using IF and GOTO satement:
#include <stdio.h>
void main()
{
int a,b=1,c;
printf("Enter a number to print its multiplication: ");
scanf("%d",&a);
result:
c=a*b;
printf("%d \n",c);
b++;
if(b<=10)
goto result;
}
3. Program to enter any number and find whether that number is prime or composite.
void main()
{
int i,num,c=0;
printf("Enter a number: ");
scanf("%d",&num);
for(i=1;i<=num;i++)
{
if(num % i==0)
c=c+1;
}
if(c==2)
printf("Entered number is prime.");
else
printf("Entered number is composite." );
}
4. Program using WHILE loop to print the first 10 even numbers.
#include <stdio.h>
void main()
{
int i=2;
//You can use scanf to get number and print upto that num.
printf("Even numbers upto 20:\n");
while(i<=20)
{
printf("%d \n",i);
i=i+2;
}
}
5. Program to find the factorial of an entered number. [Most Important]
Using for loop:
#include <stdio.h>
void main()
{
int i,f=1,num;
printf("Enter a number to find factorial: ");
scanf("%d",&num);
for (i=num;i>=1;i--)
{
f = f*i;
}
printf("Factorial of %d is : %d",num,f);
}
6. Program to enter any number and print its factors.
Note : Factors and Factorial are different things.
#include <stdio.h>
void main()
{
int num,i=1;
printf("Enter a number: ");
scanf("%d",&num);
printf("Factors of %d are : \n",num);
while(i<=num)
{
if(num% i ==0)
{
printf("%d \n",i);
}
i++;
}
}
7. Program to display the sum of ‘n’ terms of even numbers.
Example : if n value is 6 (2+4+6=12)
#include <stdio.h>
void main()
{
int n,i,s=0;
printf("Enter the number : ");
scanf("%d",&n);
for(i=2;i<=n;i=i+2)
{
s=s+i;
}
printf("Sum of n term even numbers upto given num is : %d",s);
getch();
}
8. Program to print Fibonacci series i.e. 0,1,1,2,3.......up to n terms. [Most Important]
#include <stdio.h>
void main()
{
int a=0,b=1,c,i,n;
// To start series from 1 simply put a = 1
scanf("%d",&n);
for(i=1; i<=n; i++)
{
printf("%d ",a);
c = a+b;
a = b;
b = c;
}
}
Problems Based on Nested Loop
9. Program to print the multiplication table of 5 to 10 up to 10 multiples using a Nested loop.
void main()
{
int i,j;
for(i=5;i<=10;i++)
{
for(j=1;j<=10;j++)
{
printf("%d ",i*j);
}
printf("\n");
}
getch();
}
10. Program to print all prime numbers between 2 to 50. (Nested Loop)
void main()
{
int i,j,f;
for(i=2;i<=50;i++)
{
for(j=1;j<=1;j++)
{
if(i%j==0)
f++;
}
if(f==2)
{
printf("%d ",i);
f=0;
}
}
}
Problems based on Arrays [Most Important]
11. Program to store 10 different constant variables in an array and print out the greatest number.
#include <stdio.h>
void main()
{
int a[10], g,i;
printf("Enter any ten numbers : \n");
for(i=1;i<=10;i++)
scanf("%d", &a[i]);
g=a[0];
for(i=1;i<=10;i++)
{
if(a[i]>g)
g=a[i];
}
printf("\nThe greatest number is %d.",g);
}
12. Program to input 'n' numbers and find out the greatest and smallest number.
void main()
{
int n,a[100],i,g,s;
printf("How many numbers from which you want to find greatest and smallest from: ");
scanf("%d",&n);
printf("Ok Enter those %d numbers : \n",n);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
g=a[0];
s=a[0];
for(i=1;i<n;i++)
{
if(a[i]>g) g=a[i];
if(a[i]<s) s=a[i];
}
printf("\nThe Greatest number is :%d",g);
printf("\nThe Smallest number is :%d",s);
}
13. Program to sort an array of 'n' elements in ascending order.
void main()
{
int num[50],i,j,temp,n;
printf("Enter the number for n term : ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%d",&num[i]); //num[0]=28,num[1]=50
}
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(num[i]>num[j])
{
temp=num[i];
num[i]=num[j];
num[j]=temp;
}
}
}
printf("\nPlacing Numbers in Ascending Order :\n");
for(i=0;i<n;i++)
{
printf("\n%d",num[i]);
}
getch();
}
14. Program to sort an array of 'n' elements in descending order.
void main()
{
int num[50],i,j,temp,n;
printf("Enter the number for n term : ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%d",&num[i]);
}
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(num[i]<num[j])
{
temp=num[i];
num[i]=num[j];
num[j]=temp;
}
}
}
printf("\nPlacing the numbers in descending order : \n");
for(i=0;i<n;i++)
{
printf("\n%d",num[i]);
}
}
Note: If you are using a Turbo C++ compiler, then don't forget to include conio at first and getch() at the end of the main function.
댓글