Level : Easy:
1. Write a C program to calculate average of the given set of numbers
Sample Code:
#include <stdio.h>
int main() {
int marks[5]={50,62,70,76,78};
int i, sum = 0;
float average;
for(i=0; i < 5; ++i)
{
sum = sum + marks[i];
}
average = (float) sum / 5;
printf("Average = %2.2f", average);
return 0;
}
Sample Output :
Average : 67.20
2. Write a C Program to print only distinct elements in an array
Sample Code:
#include <stdio.h>
int main()
{
int num[8]={1,2,2,1,3,4,3,2};
int i, j;
for (i=0; i<8; i++)
{
for (j=0; j<i; j++)
{
if (num[i] == num[j])
break;
}
if (i == j)
printf("%d ", num[i]);
}
}
Sample Output:
1234
3. Write a program in C to display n terms of natural number and their
sum
Sample Code:
#include <stdio.h>
void main()
{
int i,n,sum=0;
printf("Input Value of terms : ");
scanf("%d",&n);
printf("\nThe first %d natural numbers are:\n",n);
for(i=1;i<=n;i++)
{
printf("%d ",i);
sum+=i;
}
printf("\nThe Sum of natural numbers upto %d terms : %d \n",n,sum);
}
Sample Output:
4.Write a program in C to display the cube of the number upto given an
integer
Sample Code:
#include <stdio.h>
void main()
{
int i,ctr;
printf("Input number of terms : ");
scanf("%d", &ctr);
for(i=1;i<=ctr;i++)
{
printf("Number is : %d and cube of the %d is :%d \n",i,i, (i*i*i));
}
}
Sample Output:
5. Write a program in C to display the n terms of odd natural number and
their sum
Sample Code:
#include <stdio.h>
void main()
{
int i,n,sum=0;
printf("Input number of terms : ");
scanf("%d",&n);
printf("\nThe odd numbers are :");
for(i=1;i<=n;i++)
{
printf("%d ",2*i-1);
sum+=2*i-1;
}
printf("\nThe Sum of odd Natural Number upto %d terms : %d \n",n,sum);
}
Sample Output:
6. Write a program in C to print largest element in an array
Sample Code:
#include <stdio.h>
int main() {
int array[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0};
int loop, largest;
largest = array[0];
for(loop = 1; loop < 10; loop++) {
if( largest < array[loop] )
largest = array[loop];
}
printf("Largest element of the given array is: %d", largest);
return 0;
}
Sample Output:
Largest element of the given array is: 9
7.Write a C program to read any day in integer and display the day name in
words.
#include <stdio.h>
#include <stdio.h>
void main()
{
int dayno;
printf("Input Day No : " );
scanf("%d",&dayno);
switch(dayno)
{
case 1:
printf("Monday \n");
break;
case 2:
printf("Tuesday \n");
break;
case 3:
printf("Wednesday \n");
break;
case 4:
printf("Thursday \n");
break;
case 5:
printf("Friday \n");
break;
case 6:
printf("Saturday \n");
break;
case 7:
printf("Sunday \n");
break;
default:
printf("Invalid day number. \nPlease try again");
break;
}
}
Sample Output
8. Write a program in C to find LCM of any two numbers
#include <stdio.h>
int main() {
int n1, n2, max;
printf("Enter two positive integers: ");
scanf("%d %d", &n1, &n2);
max = (n1 > n2) ? n1 : n2;
while (1) {
if ((max % n1 == 0) && (max % n2 == 0)) {
printf("The LCM of %d and %d is %d.", n1, n2, max);
break;
}
++max;
}
return 0;
}
Output
Enter two positive integers: 72
120
The LCM of 72 and 120 is 360.
9.Write C Program to check entered number is ZERO, POSITIVE or NEGATIVE
until user does not want to quit.
Sample Code:
#include <stdio.h>
int main()
{
int num;
char choice;
do {
printf("Enter an integer number :");
scanf("%d", &num);
if (num == 0)
printf("Number is ZERO.");
else if (num > 0)
printf("Number is POSITIVE.");
else
printf("Number is NEGATIVE.");
printf("\n\nWant to check again (press Y/y for 'yes') :");
fflush(stdin);
scanf(" %c", &choice);
} while (choice == 'Y' || choice == 'y');
return 0;
}
Sample Output
Enter an integer number :0
Number is ZERO.
Want to check again (press Y/y for 'yes') :Y
Enter an integer number :1234
Number is POSITIVE.
Want to check again (press Y/y for 'yes') :Y
Enter an integer number :-345
Number is NEGATIVE.
Want to check again (press Y/y for 'yes') :y
Enter an integer number :45
Number is POSITIVE.
Want to check again (press Y/y for 'yes') :N
10. Write a C program to find the product of the digits of a given number
E.g 1978 =1x9x7x8= 504
Sample Code:
#include <stdio.h>
int main()
{
int Number, Reminder, Product;
printf("\n Please Enter any Number that you wish : ");
scanf("%d", & Number);
for(Product = 1; Number > 0; Number = Number / 10)
{
Reminder = Number % 10;
Product = Product * Reminder;
}
printf(" \n The Product of Digits of a Given Number = %d", Product);
return 0;
}
Sample Output:
Level : Medium:
1. Write a C Program to Convert a Decimal number to Binary number
Sample code:
// Decimal to binary conversion using an Array in C
#include<stdio.h>
int main()
{
int num ;
// creating an array to store binary equivalent
int binaryArray[32];
printf("Enter a Decimal number :");
scanf("%d", &num);
int i = 0;
while (num > 0)
{
// resultant remainder is stored at given array position
binaryArray[i] = num % 2;
num = num / 2;
i++;
}
// printing binary array in reverse order
for (int j = i - 1; j >= 0; j--)
printf("%d",binaryArray[j]);
return 0;
}
Sample Input & Output:
Enter a Decimal number :13
Output:
1101
2. C Program to convert Decimal to Octal using Array
Sample Code:
#include<stdio.h>
int main()
{
int num;
int octalArray[32];
int i = 0;
printf("Enter a Decimal number:");
scanf("%d",&num);
while (num > 0) {
// resultant remainder is stored at given array position
octalArray[i] = num % 8;
num = num / 8;
i++;
}
// printing octal array in reverse order
for (int j = i - 1; j >= 0; j--)
printf("%d",octalArray[j]);
return 0;
}
Sample Input & Output:
Enter a Decimal number: 178
Output:
262
3. /* C Program to find Sum of Diagonal Elements of a Matrix */
#include<stdio.h>
int main()
{
int i, j, rows, columns, a[10][10], Sum = 0;
printf("\n Please Enter Number of rows and columns : ");
scanf("%d %d", &i, &j);
printf("\n Please Enter the Matrix Elements: \n");
for(rows = 0; rows < i; rows++)
{
for(columns = 0;columns < j;columns++)
{
scanf("%d", &a[rows][columns]);
}
}
for(rows = 0; rows < i; rows++)
{
Sum = Sum + a[rows][rows];
}
printf("\n The Sum of Diagonal Elements of a Matrix = %d", Sum );
return 0;
}
Sample Input & Output:
Please Enter Number of rows and columns : 3 3
Please Enter the Matrix Elements
1 2 3
4 5 6
7 8 9
The Sum of Diagonal Elements of a Matrix = 15
4. Write a C Program to convert the uppercase string into lower case
#include <stdio.h>
#include <conio.h>
int main ()
{
char str[30];
int i;
printf (" Enter the string: ");
scanf (" %s", &str); // take a string
// use for loop to change string from upper case to lower case
for ( i = 0; i <= strlen (str); i++)
{
// The ASCII value of A is 65 and Z is 90
if (str[i] >= 65 && str[i] <= 90)
str[i] = str[i] + 32; /* add 32 to string character to convert into lower case. */
}
printf (" \n Upper Case to Lower case string is: %s", str);
return 0;
}
Sample Input & Output
Enter the string: WELCOME
Upper Case to Lower case string is: welcome
5. Write a program in C to display the multiplication table vertically from 1
to n
Sample Code:
#include <stdio.h>
int main()
{
int j,i,n;
printf("Input upto the table number starting from 1 : ");
scanf("%d",&n);
printf("Multiplication table from 1 to %d \n",n);
for(i=1;i<=10;i++)
{
for(j=1;j<=n;j++)
{
printf("%dx%d = %d, ",j,i,i*j);
}
printf("\n");
}
}
Sample Output:
6. Write a program in C to make such a pattern like right angle triangle
with a number which will repeat a number in a row.
The pattern like :
1
22
333
4444
Sample Code:
#include <stdio.h>
void main()
{
int i,j,rows;
printf("Input number of rows : ");
scanf("%d",&rows);
for(i=1;i<=rows;i++)
{
for(j=1;j<=i;j++)
printf("%d",i);
printf("\n");
}
}
Sample Output:
7. Write a program in C to make such a pattern like a pyramid with an
asterisk.
*
**
***
****
Sample Code:
#include <stdio.h>
void main()
{
int i,j,spc,rows,k;
printf("Input number of rows : ");
scanf("%d",&rows);
spc=rows+4-1;
for(i=1;i<=rows;i++)
{
for(k=spc;k>=1;k--)
{
printf(" ");
}
for(j=1;j<=i;j++)
printf("* ");
printf("\n");
spc--;
}
}
Sample output:
8. Write a program to check whether an element exists in an array or not
(linear search)
Sample Code:
#include<stdio.h>
#include<conio.h>
int main()
{
int a[5],i,sum=0,v,position = -1;
for(i=0;i<5;i++)
{
printf("enter a number:");
scanf("%d",&a[i]);
}
printf("\nenter a number to search :");
scanf("%d",&v);
for(i=0;i<5;i++)
{
if(v==a[i])
{
position=i+1; break;
}
}
if(position == -1)
{
printf(" \nvalue does not exist");
}
else
printf( "\n value exist at position= %d",position);
return 1;
}
Sample Output:
9. Write a c program to check whether a given number is a perfect number
or not
/*Perfect number is a positive number whose sum of all positive divisors
excluding that number is equal to that number. For example 6 is the perfect
number since divisors of 6 are 1, 2 and 3. Sum of its divisor is 1 + 2+ 3 = 6*/
Sample Code:
#include <stdio.h>
void main()
{
int n,i,sum;
int mn,mx;
printf("Input the number : ");
scanf("%d",&n);
sum = 0;
printf("The positive divisor : ");
for (i=1;i<n;i++)
{
if(n%i==0)
{
sum=sum+i;
printf("%d ",i);
}
}
printf("\nThe sum of the divisor is : %d",sum);
if(sum==n)
printf("\nSo, the number is perfect.");
else
printf("\nSo, the number is not perfect.");
printf("\n");
}
Sample Output:
10. Write a C program to display Pascal's triangle
Sample Code:
#include <stdio.h>
void main()
{
int no_row,c=1,blk,i,j;
printf("Input number of rows: ");
scanf("%d",&no_row);
for(i=0;i<no_row;i++)
{
for(blk=1;blk<=no_row-i;blk++)
printf(" ");
for(j=0;j<=i;j++)
{
if (j==0||i==0)
c=1;
else
c=c*(i-j+1)/j;
printf("% 4d",c);
}
printf("\n");
}
}
Sample Output: