Download as pdf or txt
Download as pdf or txt
You are on page 1of 60

KALASALINGAM ACADEMY OF RESEARCH AND

EDUCATION
(Deemed to be University)
Anand Nagar, Krishnankoil – 626126

Problem Solving using Computer Programming – Programs

Sections: A11,A12 and A18 Course Teacher: M.Muthulakshmi

SIMPLE C PROGRAMS

1. Write a C program for addition of two numbers.

Coding:

#include <stdio.h>
void main( )
{
int number1, number2, sum;
printf("Enter two integers: ");
scanf("%d %d", &number1, &number2);
sum = number1 + number2;
printf("Sum is %d”, sum);
}

Output:
2. Write a C program to calculate Area of circle.

Coding:

#include <stdio.h>
void main()
{
int radius;
float pi=3.14,area;
printf("\nEnter radius of circle: ");
scanf("%d",&radius);
area = pi * radius * radius;
printf("\nArea of circle is: %f",area);
}

Output:

3. Write a C program to swap two numbers.

Coding:

#include<stdio.h>
void main()
{
int a, b, temp;
printf("Enter the value of a: ");
scanf("%d",&a);
printf("Enter the value of b: ");
scanf("%d",&b);
temp = a;
a = b;
b = temp;
printf("\nAfter swapping, the value of a is %d\n", a);
printf("After swapping, the value of b is %d", b);
}

Output:

4. Write a C Program to Compute Quotient and Remainder.

Coding:

#include <stdio.h>
void main()
{
int dividend, divisor, quotient, remainder;
printf("Enter dividend: ");
scanf("%d", &dividend);
printf("Enter divisor: ");
scanf("%d", &divisor);
quotient = dividend / divisor;
remainder = dividend % divisor;
printf("Quotient = %d\n", quotient);
printf("Remainder = %d", remainder);
}
Output:

5. Write a C Program to calculate Simple Interest.

Coding:

#include <stdio.h>
void main()
{
float principle, time, rate, SI;
printf("Enter principle (amount): ");
scanf("%f", &principle);
printf("Enter time: ");
scanf("%f", &time);
printf("Enter rate: ");
scanf("%f", &rate);
SI = (principle * time * rate) / 100;
printf("Simple Interest = %f", SI);
}
Output:

6. Write a C program to work with different arithmetic operators.

Coding:

#include <stdio.h>
void main( )
{
int number1, number2;
int sum,sub,mul,div;
printf("Enter two integers: ");
scanf("%d %d", &number1, &number2);
sum = number1 + number2;
sub=number1-number2;
mul=number1*number2;
div=number1/number2;
printf("Sum of two numbers is %d”, sum);
printf(“Subtraction of two numbers id %d”, sub);
printf(“Multiplication of two numbers is %d”,mul);
printf(“Division of two numbers is %d”,div);
}
Output:

CONTROL STRUCTURES

Programs using if, if else, if elseif, switch:

7. Write a C Program to check whether the given number is odd or even.

Coding:

#include <stdio.h>
void main( )
{
int num;
printf("Enter an integer: ");
scanf("%d", &num);
if(num % 2 == 0)
printf("%d is even.", num);
else
printf("%d is odd.", num);
}
Output:

8. Write a C Program to check whether the given number is positive or negative.

Coding:

#include <stdio.h>
void main()
{
int num;
printf("Enter an integer: ");
scanf("%d", &num);
if(num > 0)
printf("%d is positive number.", num);
else
printf("%d is negative number.", num);
}

Output:
9. Write a C program to check whether the given number is divisible by both 4 and 8.

Coding:

#include <stdio.h>
void main()
{
int num;
printf("Enter an integer: ");
scanf("%d", &num);
if(num % 4 == 0 && num % 8 == 0)
printf("%d is divided by both 4 and 8.", num);
else
printf("%d is not divided by both 4 and 8.", num);
}

Output:

10. Write a C program to find largest of three numbers.

Coding:

#include <stdio.h>
void main()
{
int a, b, c;
printf("Enter the values for a, b and c: ");
scanf("%d %d %d", &a, &b, &c);
if (a >= b && a >= c)
printf("%d is the largest number.", a);
if (b >= a && b >= c)
printf("%d is the largest number.", b);
if (c >= a && c >= b)
printf("%d is the largest number.", c);
}

Output:

11. Write a C program to calculate grade of a student.

Coding:

#include <stdio.h>
int main()
{
float eng, chem, comp, phy, mat;
float avg;
printf(" Please Enter the Five Subjects Marks : \n");
scanf("%f%f%f%f%f", &eng, &chem, &comp, &phy, &mat);
avg = ((eng + chem + comp + phy + mat) / 500) * 100;
printf(" Average is %f",avg);
if(avg >= 90)
{
printf("\n Grade A");
}
else if(avg >= 80)
{
printf("\n Grade B");
}
else if(avg >= 70)
{
printf("\n Grade C");
}
else if(avg >= 60)
{
printf("\n Grade D");
}
else if(avg >= 40)
{
printf("\n Grade E");
}
else
{
printf("\n Fail");
}
}

Output:
12. Write a C program to generate a simple calculator using switch.

Coding:

#include<stdio.h>
void main()
{
int a,b,c;
int op;
printf(" 1.Addition\n 2.Subtraction\n 3.Multiplication\n 4.Division\n");
printf("Enter the values of a & b: ");
scanf("%d %d",&a,&b);
printf("Enter your Choice : ");
scanf("%d",&op);
switch(op)
{
case 1 :
c=a+b;
printf("Sum is %d",c);
break;
case 2 :
c=a-b;
printf("Difference is %d",c);
break;
case 3 :
c=a*b;
printf("Multiplication is %d",c);
break;
case 4 :
c=a/b;
printf("Division is %d ",c);
break;
default :
printf(" Enter Your Correct Choice.");
break;
}
}
Output:

13. Write a C program for Grade calculation using switch.

Coding:

#include<stdio.h>
void main()
{
int mark,n;
printf("Enter mark:");
scanf("%d", &mark);
n=mark/10;
switch( n )
{
case 10:
case 9:
printf("Grade: A");
break;
case 8:
printf("Grade: B");
break;
case 7:
printf("Grade: C");
break;
case 6:
printf("Grade: D");
break;
case 5:
printf("Grade: E");
break;
case 6:
printf("Grade: F");
break;
}
}

Output:

Programs for looping controls (for,while,do while):

14. Write a C program to print numbers from 1 to 10.

Coding:

#include<stdio.h>
void main()
{
int i;
for ( i = 1; i <=10; i++ )
{
printf( "%d\n", i );
}
}
Output:

15. Write a C program to print the multiplication table of a number.

Coding:

#include <stdio.h>
void main()
{
int n, i;
printf("Enter an integer: ");
scanf("%d", &n);
for (i = 1; i <= 10; ++i)
{
printf("%d * %d = %d \n", n, i, n * i);
}
}
Output:

16. Write a C program to print half pyramid using *.

Coding:

#include <stdio.h>
void main()
{
int i, j, rows;
printf("Enter the number of rows: ");
scanf("%d", &rows);
for (i = 1; i <= rows; i++)
{
for (j = 1; j <= i; j++)
{
printf("* ");
}
printf("\n");
}
}
Output:

17. Write a C program to print half pyramid of a numbers.

Coding:

#include <stdio.h>
void main()
{
int i, j, rows;
printf("Enter the number of rows: ");
scanf("%d", &rows);
for (i = 1; i <= rows; i++)
{
for (j = 1; j <= i; j++)
{
printf("%d",i);
}
printf("\n");
}
}
Output:

18. Write a C program to find sum of first n natural numbers.

Coding:

#include <stdio.h>
void main()
{
int num, i, sum = 0;
printf(" Enter a number: ");
scanf("%d", &num);
for (i = 0; i <= num; i++)
{
sum = sum + i;
}
printf("\n Sum of the first %d number is: %d", num, sum);
}
Output:

19. Write a C program to display fibonacci series.

Coding:

#include<stdio.h>
void main()
{
int n1=0,n2=1,n3,i,num;
printf("Enter the number of elements:");
scanf("%d",&num);
printf("\n%d %d",n1,n2);
for(i=2;i<num;i++)
{
n3=n1+n2;
printf(" %d",n3);
n1=n2;
n2=n3;
}
}
Output:

20. Write a C program to find factorial of a number.

Coding:

#include<stdio.h>
void main()
{
int i,fact=1,num;
printf("Enter a number: ");
scanf("%d",&num);
i=1;
while(i<=num)
{
fact=fact*i;
i++;
}
printf("Factorial of %d is: %d",num,fact);
}
Output:

21. Write a C program to print reverse of a number.

Coding:

#include <stdio.h>
void main()
{
int n, rev = 0, rem;
printf("Enter an integer: ");
scanf("%d", &n);
while (n > 0)
{
rem = n % 10;
rev = rev * 10 + rem;
n = n/ 10;
}
printf("Reversed number = %d", rev);
}
Output:

22. Write a C program to check whether the given number is palindrome or not.

Coding:

#include <stdio.h>
void main()
{
int n, rev = 0, rem,temp;
printf("Enter an integer: ");
scanf("%d", &n);
temp=n;
while (n > 0)
{
rem = n % 10;
rev = rev * 10 + rem;
n /= 10;
}
printf("Reversed number = %d\n", rev);
if(temp==rev)
{
printf("The number is palindrome");
}
else
{
printf("The number is not a palindrome");
}
Output:

23. Write a C program to check whether the given number is Armstrong or not.

Coding:

#include<stdio.h>
void main()
{
int n,r,sum=0,temp;
printf("enter the number");
scanf("%d",&n);
temp=n;
while(n>0)
{
r=n%10;
sum=sum+(r*r*r);
n=n/10;
}
if(temp==sum)
{
printf("The number is armstrong number ");
}
else
{
printf("The number is not armstrong number");
}
}
Output:

ARRAYS

24. Write a C program to find sum of n numbers.

Coding:

#include<stdio.h>
void main()
{
int arr[100];
int n, i, sum=0;
printf("How many elements do you want to sum? ");
scanf("%d",&n);
printf("Enter the element: ");
for(i=1; i<=n; i++)
{
scanf("%d", &arr[i]);
sum = sum + arr[i];
}
printf("Sum is %d", sum);
}
Output:

25. Write a C program to find maximum number (greatest number) in an array.

Coding:

#include <stdio.h>
void main()
{
int arr[50];
int i,n,max;
printf("Enter size of the array : ");
scanf("%d",&n);
printf("Enter elements in array : ");
for(i=0; i<n; i++)
{
scanf("%d",&arr[i]);
}
max=arr[0];
for(i=1; i<n; i++)
{
if(arr[i]>max)
max=arr[i];
}
printf("\n maximum of array is : %d",max);
}
Output:

26. Write a C program to find minimum number (smallest number) in an array.

Coding:

#include <stdio.h>
void main()
{
int arr[50];
int i,n,min;

printf("Enter size of the array : ");


scanf("%d",&n);

printf("Enter elements in array : ");


for(i=0; i<n; i++)
{
scanf("%d",&arr[i]);
}
min=arr[0];
for(i=1; i<n; i++)
{
if(arr[i]<min)
min=arr[i];
}
printf("\n minimum of array is : %d",min);
}
Output:
KALASALINGAM ACADEMY OF RESEARCH AND
EDUCATION
(Deemed to be University)
Anand Nagar, Krishnankoil – 626126

Problem Solving using Computer Programming – Programs

Sections: A11,A12 and A18 Course Teacher: M.Muthulakshmi

SIMPLE C PROGRAMS

1. Write a C program for addition of two numbers.

Coding:

#include <stdio.h>
void main( )
{
int number1, number2, sum;
printf("Enter two integers: ");
scanf("%d %d", &number1, &number2);
sum = number1 + number2;
printf("Sum is %d”, sum);
}

Output:
2. Write a C program to calculate Area of circle.

Coding:

#include <stdio.h>
void main()
{
int radius;
float pi=3.14,area;
printf("\nEnter radius of circle: ");
scanf("%d",&radius);
area = pi * radius * radius;
printf("\nArea of circle is: %f",area);
}

Output:

3. Write a C program to swap two numbers.

Coding:

#include<stdio.h>
void main()
{
int a, b, temp;
printf("Enter the value of a: ");
scanf("%d",&a);
printf("Enter the value of b: ");
scanf("%d",&b);
temp = a;
a = b;
b = temp;
printf("\nAfter swapping, the value of a is %d\n", a);
printf("After swapping, the value of b is %d", b);
}

Output:

4. Write a C Program to Compute Quotient and Remainder.

Coding:

#include <stdio.h>
void main()
{
int dividend, divisor, quotient, remainder;
printf("Enter dividend: ");
scanf("%d", &dividend);
printf("Enter divisor: ");
scanf("%d", &divisor);
quotient = dividend / divisor;
remainder = dividend % divisor;
printf("Quotient = %d\n", quotient);
printf("Remainder = %d", remainder);
}
Output:

5. Write a C Program to calculate Simple Interest.

Coding:

#include <stdio.h>
void main()
{
float principle, time, rate, SI;
printf("Enter principle (amount): ");
scanf("%f", &principle);
printf("Enter time: ");
scanf("%f", &time);
printf("Enter rate: ");
scanf("%f", &rate);
SI = (principle * time * rate) / 100;
printf("Simple Interest = %f", SI);
}
Output:

6. Write a C program to work with different arithmetic operators.

Coding:

#include <stdio.h>
void main( )
{
int number1, number2;
int sum,sub,mul,div;
printf("Enter two integers: ");
scanf("%d %d", &number1, &number2);
sum = number1 + number2;
sub=number1-number2;
mul=number1*number2;
div=number1/number2;
printf("Sum of two numbers is %d”, sum);
printf(“Subtraction of two numbers id %d”, sub);
printf(“Multiplication of two numbers is %d”,mul);
printf(“Division of two numbers is %d”,div);
}
Output:

CONTROL STRUCTURES

Programs using if, if else, if elseif, switch:

7. Write a C Program to check whether the given number is odd or even.

Coding:

#include <stdio.h>
void main( )
{
int num;
printf("Enter an integer: ");
scanf("%d", &num);
if(num % 2 == 0)
printf("%d is even.", num);
else
printf("%d is odd.", num);
}
Output:

8. Write a C Program to check whether the given number is positive or negative.

Coding:

#include <stdio.h>
void main()
{
int num;
printf("Enter an integer: ");
scanf("%d", &num);
if(num > 0)
printf("%d is positive number.", num);
else
printf("%d is negative number.", num);
}

Output:
9. Write a C program to check whether the given number is divisible by both 4 and 8.

Coding:

#include <stdio.h>
void main()
{
int num;
printf("Enter an integer: ");
scanf("%d", &num);
if(num % 4 == 0 && num % 8 == 0)
printf("%d is divided by both 4 and 8.", num);
else
printf("%d is not divided by both 4 and 8.", num);
}

Output:

10. Write a C program to find largest of three numbers.

Coding:

#include <stdio.h>
void main()
{
int a, b, c;
printf("Enter the values for a, b and c: ");
scanf("%d %d %d", &a, &b, &c);
if (a >= b && a >= c)
printf("%d is the largest number.", a);
if (b >= a && b >= c)
printf("%d is the largest number.", b);
if (c >= a && c >= b)
printf("%d is the largest number.", c);
}

Output:

11. Write a C program to calculate grade of a student.

Coding:

#include <stdio.h>
int main()
{
float eng, chem, comp, phy, mat;
float avg;
printf(" Please Enter the Five Subjects Marks : \n");
scanf("%f%f%f%f%f", &eng, &chem, &comp, &phy, &mat);
avg = ((eng + chem + comp + phy + mat) / 500) * 100;
printf(" Average is %f",avg);
if(avg >= 90)
{
printf("\n Grade A");
}
else if(avg >= 80)
{
printf("\n Grade B");
}
else if(avg >= 70)
{
printf("\n Grade C");
}
else if(avg >= 60)
{
printf("\n Grade D");
}
else if(avg >= 40)
{
printf("\n Grade E");
}
else
{
printf("\n Fail");
}
}

Output:
12. Write a C program to generate a simple calculator using switch.

Coding:

#include<stdio.h>
void main()
{
int a,b,c;
int op;
printf(" 1.Addition\n 2.Subtraction\n 3.Multiplication\n 4.Division\n");
printf("Enter the values of a & b: ");
scanf("%d %d",&a,&b);
printf("Enter your Choice : ");
scanf("%d",&op);
switch(op)
{
case 1 :
c=a+b;
printf("Sum is %d",c);
break;
case 2 :
c=a-b;
printf("Difference is %d",c);
break;
case 3 :
c=a*b;
printf("Multiplication is %d",c);
break;
case 4 :
c=a/b;
printf("Division is %d ",c);
break;
default :
printf(" Enter Your Correct Choice.");
break;
}
}
Output:

13. Write a C program for Grade calculation using switch.

Coding:

#include<stdio.h>
void main()
{
int mark,n;
printf("Enter mark:");
scanf("%d", &mark);
n=mark/10;
switch( n )
{
case 10:
case 9:
printf("Grade: A");
break;
case 8:
printf("Grade: B");
break;
case 7:
printf("Grade: C");
break;
case 6:
printf("Grade: D");
break;
case 5:
printf("Grade: E");
break;
case 6:
printf("Grade: F");
break;
}
}

Output:

Programs for looping controls (for,while,do while):

14. Write a C program to print numbers from 1 to 10.

Coding:

#include<stdio.h>
void main()
{
int i;
for ( i = 1; i <=10; i++ )
{
printf( "%d\n", i );
}
}
Output:

15. Write a C program to print the multiplication table of a number.

Coding:

#include <stdio.h>
void main()
{
int n, i;
printf("Enter an integer: ");
scanf("%d", &n);
for (i = 1; i <= 10; ++i)
{
printf("%d * %d = %d \n", n, i, n * i);
}
}
Output:

16. Write a C program to print half pyramid using *.

Coding:

#include <stdio.h>
void main()
{
int i, j, rows;
printf("Enter the number of rows: ");
scanf("%d", &rows);
for (i = 1; i <= rows; i++)
{
for (j = 1; j <= i; j++)
{
printf("* ");
}
printf("\n");
}
}
Output:

17. Write a C program to print half pyramid of a numbers.

Coding:

#include <stdio.h>
void main()
{
int i, j, rows;
printf("Enter the number of rows: ");
scanf("%d", &rows);
for (i = 1; i <= rows; i++)
{
for (j = 1; j <= i; j++)
{
printf("%d",i);
}
printf("\n");
}
}
Output:

18. Write a C program to find sum of first n natural numbers.

Coding:

#include <stdio.h>
void main()
{
int num, i, sum = 0;
printf(" Enter a number: ");
scanf("%d", &num);
for (i = 0; i <= num; i++)
{
sum = sum + i;
}
printf("\n Sum of the first %d number is: %d", num, sum);
}
Output:

19. Write a C program to display fibonacci series.

Coding:

#include<stdio.h>
void main()
{
int n1=0,n2=1,n3,i,num;
printf("Enter the number of elements:");
scanf("%d",&num);
printf("\n%d %d",n1,n2);
for(i=2;i<num;i++)
{
n3=n1+n2;
printf(" %d",n3);
n1=n2;
n2=n3;
}
}
Output:

20. Write a C program to find factorial of a number.

Coding:

#include<stdio.h>
void main()
{
int i,fact=1,num;
printf("Enter a number: ");
scanf("%d",&num);
i=1;
while(i<=num)
{
fact=fact*i;
i++;
}
printf("Factorial of %d is: %d",num,fact);
}
Output:

21. Write a C program to print reverse of a number.

Coding:

#include <stdio.h>
void main()
{
int n, rev = 0, rem;
printf("Enter an integer: ");
scanf("%d", &n);
while (n > 0)
{
rem = n % 10;
rev = rev * 10 + rem;
n = n/ 10;
}
printf("Reversed number = %d", rev);
}
Output:

22. Write a C program to check whether the given number is palindrome or not.

Coding:

#include <stdio.h>
void main()
{
int n, rev = 0, rem,temp;
printf("Enter an integer: ");
scanf("%d", &n);
temp=n;
while (n > 0)
{
rem = n % 10;
rev = rev * 10 + rem;
n /= 10;
}
printf("Reversed number = %d\n", rev);
if(temp==rev)
{
printf("The number is palindrome");
}
else
{
printf("The number is not a palindrome");
}
Output:

23. Write a C program to check whether the given number is Armstrong or not.

Coding:

#include<stdio.h>
void main()
{
int n,r,sum=0,temp;
printf("enter the number");
scanf("%d",&n);
temp=n;
while(n>0)
{
r=n%10;
sum=sum+(r*r*r);
n=n/10;
}
if(temp==sum)
{
printf("The number is armstrong number ");
}
else
{
printf("The number is not armstrong number");
}
}
Output:

ARRAYS

24. Write a C program to find sum of n numbers.

Coding:

#include<stdio.h>
void main()
{
int arr[100];
int n, i, sum=0;
printf("How many elements do you want to sum? ");
scanf("%d",&n);
printf("Enter the element: ");
for(i=1; i<=n; i++)
{
scanf("%d", &arr[i]);
sum = sum + arr[i];
}
printf("Sum is %d", sum);
}
Output:

25. Write a C program to find maximum number (greatest number) in an array.

Coding:

#include <stdio.h>
void main()
{
int arr[50];
int i,n,max;
printf("Enter size of the array : ");
scanf("%d",&n);
printf("Enter elements in array : ");
for(i=0; i<n; i++)
{
scanf("%d",&arr[i]);
}
max=arr[0];
for(i=1; i<n; i++)
{
if(arr[i]>max)
max=arr[i];
}
printf("\n maximum of array is : %d",max);
}
Output:

26. Write a C program to find minimum number (smallest number) in an array.

Coding:

#include <stdio.h>
void main()
{
int arr[50];
int i,n,min;

printf("Enter size of the array : ");


scanf("%d",&n);

printf("Enter elements in array : ");


for(i=0; i<n; i++)
{
scanf("%d",&arr[i]);
}
min=arr[0];
for(i=1; i<n; i++)
{
if(arr[i]<min)
min=arr[i];
}
printf("\n minimum of array is : %d",min);
}
Output:
Some additional programs:

1. Write a C program to print ASCII value of a character.

Coding:

#include<stdio.h>
void main()
{
char a;
printf("Enter The Character ");
scanf("%c",&a);
printf("\n ASCII value is %d ",a);
}

Output:

2. Write a C program to check whether the given character is vowel or not.

Coding:

#include <stdio.h>
void main()
{
char ch;
printf("Enter a character\n");
scanf("%c", &ch);
if (ch=='a' || ch=='A' || ch=='e' || ch=='E' || ch=='i' || ch=='I' || ch=='o' || ch=='O' || ch==
'u' || ch=='U')
printf("The alphabet is a vowel");
else
printf("The alphabet is a not a vowel");
}

Output:

3. Write a C program to find average of three numbers.

Coding:

#include <stdio.h>
void main()
{
int n1,n2,n3;
float avg;
printf("\nEnter the three numbers: " );
scanf("%d %d %d",&n1,&n2,&n3);
avg=(n1+n2+n3)/3;
printf("\nAVERAGE: %f",avg);
}
Output:

4. Write a C program to evaluate the expression a+b+2ab

Coding:

#include <stdio.h>
void main()
{
int a,b;
printf("Enter the values of a and b");
scanf("%d%d",&a,&b);
printf("The evaluation result is %d",a+b+2*a*b);
}
Output:

5. Write a C program to find the memory size occupied by primitive data types

Coding:

#include <stdio.h>
void main()
{
int i;
float f;
double d;
char c;
printf("Size of int: %d bytes \n", sizeof(i));
printf("Size of float: %d bytes\n", sizeof(f));
printf("Size of double: %d bytes\n", sizeof(d));
printf("Size of char: %d byte\n", sizeof(c));
}
Output:

6. Write a C program to categorize a person as child, teenager or adult.

Coding:

#include <stdio.h>
void main()
{
int age;
printf("Enter the age");
scanf("%d",&age);
if(age<15)
printf("The person is child");
else if(age<25)
printf("The person is teenager");
else
printf("The person is adult");
}
Output:

7. Write a C program to print inverted half pyramid using *.

Coding:

#include <stdio.h>
void main()
{
int i, j, rows;
printf("Enter the number of rows: ");
scanf("%d", &rows);
for (i = rows; i >= 1; i--)
{
for (j = 1; j <= i; j++)
{
printf("* ");
}
printf("\n");
}
}
Output:

8. Write a C program to print inverted half pyramid of a numbers.

Coding:

#include <stdio.h>
void main()
{
int i, j, rows;
printf("Enter the number of rows: ");
scanf("%d", &rows);
for (i = rows; i >= 1; i--)
{
for (j = 1; j <= i; j++)
{
printf("%d",i);
}
printf("\n");
}
}
Output:

You might also like