You are on page 1of 33

1

/*
Assignment 1
1. Write a program in C to add two number (values should be assigned to
variable)
*/

#include <stdio.h>
int main()
{
int a=5,b=7,c;
c=a+b;
printf(" Sum is : %d ", c);
return 0;
}

/*
Assignment 1
2. Write a program in C to calculate the area and perimeter of a circle. (PI
value should be taken using macro preprocessor)
*/

#include <stdio.h>
#define PI 3.14
int main()
{
float r, p ,ar;
printf(" Enter the radius of the circle: ");
scanf("%f",&r);
p = 2*PI*r;
ar = PI*r*r;
printf("\n Perimeter of circle = %f", p);
printf("\n Area of circle = %f", ar);
return 0;
}
2

/*
Assignment 2
1. Write a program to find the area and perimeter of a rectangle.
*/

#include <stdio.h>
int main()
{ float l,b,ar,p;
printf("Enter the length of the rectangle: ");
scanf("%f",&l);
printf("\nEnter the length of the rectangle: ");
scanf("%f",&b);
ar = l*b;
p= 2*(l+b);
printf("\nArea of the rectangle is : %f sq. units",ar);
printf("\nPerimeter of the rectangle is : %f units",p)
return 0;
}
3

/*
Assignment 2
2. Write a program to enter marks of 5 subjects and calculate average.
*/

#include <stdio.h>
int main()
{ float s1,s2,s3,s4,s5,avg;
printf(“Enter subject 1st marks : “);
scanf(“%f”, &s1);
printf(“Enter subject 2nd marks : “);
scanf(“%f”, &s2);
printf(“Enter subject 3rd marks : “);
scanf(“%f”, &s3);
printf(“Enter subject 4th marks : “);
scanf(“%f”, &s4);
printf(“Enter subject 5th marks : “);
scanf(“%f”, &s5);
avg = ((s1 + s2 + s3 + s4 + s5) / 5 );
printf(“ Average : %f “, avg);
return 0;
}
4

/*
Assignment 2
1. Write a program to convert kilometre to meter.
*/

#include <stdio.h>
int main()
{ float Km, M;
printf(“Enter the length in Kilometers (km) : “);
scanf(“%f”, &Km);
M = Km * 1000.0;
printf(“\n%f Kilometers is equal to %f Meters”, Km, M);
return 0;
}

/*
Assignment 3
1. Write a program to find the ASCII value of a character.
*/

#include <stdio.h>
int main()
{ char c;
printf ("Enter the character : ");
scanf ("%c", &c);
printf ("The ASCII value of %c is %d", c, c);
return 0;
}
5

/*
Assignment 3
2. Write a program to find whether a person is eligible for voting or not.
*/

#include <stdio.h>
int main()
{ int age;
printf ("Enter your age : ");
scanf ("%d", &age);
if (age >= 18)
printf ("\n Person is eligible to vote.");
else
printf ("\n Person is not eligible to vote.");
return 0;
}
6

/*
Assignment 3
3. Write a program to take two numbers in float , multiply it and show the
result with typecasting and without type casting.
*/

#include <stdio.h>
int main()
{ float a, b, c, d;
printf ("Enter a float number a :");
scanf ("%f", &a);
printf ("Enter a float number b :");
scanf ("%f", &b);
c = (int) (a * b);
d = a * b;
printf ("\n Product (with typecasting) : %f", c);
printf ("\n Product (without typecasting) : %f", d);
return 0;
}

/*
Assignment 4
1. Write a program to enter 2 numbers .If the 1st number is less than the 2nd
then calculate the cube of the greatest number otherwise calculate the
square of the smallest number.
*/

#include <stdio.h>
int main()
{ int a, b, c;
printf(" Enter two numbers : ");
scanf("%d%d", &a, &b);
7

if(a < b)
{ c = b * b * b;
printf ( "\nThe cube of the greatest number is %d", c);
}
else
{ c = b * b;
printf ( "\nThe square of the smallest number is %d", c);
}

return 0;
}

/*
Assignment 4
2. Write a program to input three numbers and display the second smallest
number using nested if.
*/

#include<stdio.h>
int main()
{ int a, b, c;
printf ("Enter 3 numbers:\n");
scanf ("%d%d%d", &a, &b, &c);
if (a <= b && a <= c)
{ if (b <= c)
{ printf ("\n%d is the 2nd smallest number: \n", b);
}
else
{ printf ("\n%d is the 2nd smallest number. \n", c);
}
}
else
if (b <= a && b <= c)
8

{ if (a <= c)
{ printf ("\n%d is the 2nd smallest number. \n", a);
}
else
{ printf ("\n%d is the 2nd smallest number. \n", c);
}
}
else
if (a <= b)
{ printf ("\n%d is the 2nd smallest number.\n", a);
}
else
{ printf ("\n%d is the 2nd smallest number.\n", b);
}
return 0;
}

/*
Assignment 4
3. Write a program to enter 3 sides of a triangle and check whether a triangle
is possible or not. If possible then display whether it is an equilateral, an
isosceles or a scalene triangle triangle otherwise display the message
"Triangle isn't possible".
*/

#include <stdio.h>
int main()
{ float a, b, c;
printf ("Enter the three sides of a triangle : \n");
scanf ("%f%f%f", &a,&b, &c);
if ( (a < b + c) && ( b < a + c) && (c < a + b ) )
if (a == b && b == c)
printf ("\nEquilateral triangle.");
9

else if ((a == b) || (b == c) || (c == a))


printf ("\nIsosceles triangle.");
else
printf ("\nScalene triangle.");
else
printf ("\n Triangle not possible.");
return 0;
}

/*
Assignment 5
1. Write a program to find the factorial of any number using for loop.
*/

#include <stdio.h>
int main()
{ int i, n ,f = 1;
printf(“\n Enter a number to find the factorial: “);
scanf(“%d”, &n);
for (i = n; i >= 1; --i)
f *= i;
printf(“ Factorial of %d is ; %d”, n ,f);
return 0;
}
10

/*
Assignment 5
2. Write a program to find whether a number is palindrome or not using a
while loop.
*/

#include <stdio.h>
int main()
{ int n, a, r, s = 0;
printf("\n Enter the Number : ");
scanf("%d", &n);
a = n;
while(n>0)
{ r = n % 10;
s = s *10 + r;
n = n / 10;
}
if( a ==s )
{ printf("\n %d is a Palindrome Number" ,a);
}
else
{ printf("\n %d is not a Palindrome Number" ,a);
}
return 0;
}

/*
Assignment 5
3. Write a program in C to display the sum of any two numbers for ten
iterations. If the sum of two numbers is negative then the program will
terminate.
*/
11

#include <stdio.h>
int main()
{
int a, b, c, i;
for(i = 1; i <= 10; i++)
{ printf("\nEnter two numbers : ");
scanf("%d %d", &a, &b);
c = a + b;
if(c < 0)
break;
printf("Sum of two numbers : %d ", c);
}
return 0 ;
}

/*
Assignment 5
4. Write a program to input a set of numbers. The program checks whether
each number is a perfect square number or not. The program will terminate
when zero is entered by the user using while loop.
*/

#include <stdio.h>
int main()
{ int number = -1, i = -1;
while(number != 0)
{ printf("\nEnter the number : ");
scanf("%d", &number);
while(i<= number)
{ i++;
12

if(number ==i*i)
{ printf("%d is a perfect square.", number);
break;
}
}
if( number == 0)
return 0;
if(number!=i*i)
printf("%d is not a perfect square.", number);
}
return 0;
}

/*
Assignment 5
5. Write a program to display all even numbers from 1 to 10 by using
continue statement in a do-while loop.
*/

#include <stdio.h>
int main()
{ int i = 0;
do
{ i++;
if(i%2!=0)
continue;
printf("\n Even numbers = %d\n", i);
}while (i<= 10);
return 0;
}
13

/*
Assignment 6
1. s=1! + 3! +5! +.................9!
*/

#include <stdio.h>
int fact(int n);
int main()
{ int i, n, sum=0;
printf("Enter a positive no : ");
scanf("%d", &n);
printf("s = ");
for(i=1;i<=n;i=i+2)
{ sum+=fact(i);
printf("+%d!", i);
}
printf("\nfactorial : %d", sum);
return 0;
}

int fact(int n)
{ int f=1;
for(int j=n; j>=1; j--)
{ f=f*j;
}
return f;
}
14

/*
Assignment 6
2. 5 4 3 2 1
4321
321
21
1
*/

#include <stdio.h>

int main()
{ int i, j;
for(i=5; i>=1; i--)
{ for(j=i; j>=1; j--)
{printf("%d ",j);
}
printf("\n");
}
return 0;
}
15

/*
Assignment 6
3. 1 2 3 4 5
2345
345
45
5
*/

#include <stdio.h>
int main()
{ int i, j, k;
for(i=1; i<=5; i++)
{ for(j=1; j<i; j++)
{ printf("\t");
}
for(k=i; k<=5; k++)
{ printf("%d\t", k);
}
printf("\n");
}
return 0;
}

/*
Assignment 6
4. Write a program in C whether a no. is perfect no. or not(eg.
28=1+2+4+7+14)
*/

#include <stdio.h>
int main()
16

{ int i, n, s = 0;
printf("\n Please Enter the number : ");
scanf("%d", &n);
for(i = 1; i < n; i++)
{ if(n % i == 0)
s += i;
}
if(s == n)
printf("\n %d is a Perfect Number", n);
else
printf("\n%d is not the Perfect Number", n);
return 0;
}

/*
Assignment 7
1. Write a program in C to find sum,product of two numbers and print line
above and below the result using a function with no argument & no return
type.
*/

#include <stdio.h>
void sum();
void product();
void display();

int x, y;
float z;

int main()
{ printf("Enter two numbers : ");
scanf("%d %d", &x, &y);
display();
17

sum();
display();
product();
display();
return 0;
}

void sum()
{ z=x+y;
printf("\nThe sum of %d and %d is %f\n", x, y, z);
}

void product()
{ z=x*y;
printf("\nThe product of %d and %d is %f\n", x , y , z);
}

void display()
{ for(int i=0; i<=25; ++i)
printf("-");
}

/*
Assignment 8
1. Write a program to add individual digits of a number using recursion.
*/

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

int main()
{ int num, ind_s;
printf("Enter a number : ");
scanf("%d", &num);
18

ind_s = ind_sum(num);
printf("The sum of individual digits of %d is %d\n", num, ind_s);

return 0;
}

int ind_sum(int n)
{ if(n)
return (n % 10 + ind_sum(n / 10));
else
return 0;
}

/*
Assignment 8
2. Write a program to reverse a 3 digit number using recursion.
*/

#include <stdio.h>
int sum=0, rem;

int rev(int n)
{ if(n)
{ rem=n%10;
sum=sum*10+rem;
rev(n/10);
}
else
return sum;
return sum;
}

int main()
{ int num, rnum;
19

printf("\nEnter a number:");
scanf("%d", &num);
rnum = rev(num);
printf("\nAfter reverse the no is :%d", rnum);
return 0;
}

/*
Assignment 9
1. s=1/2 +1/4 +1/8 + 1/16.......................upto n terms
*/

#include <stdio.h>
#include <math.h>

int main()
{ double sum=0;
long int d=0;
int i,n;
printf("\n Enter the number of terms : ");
scanf("%d", &n);
for(i=1;i<=n;++i)
{ d=pow(2,i);
sum+=(1/(double)d);
if(i==1)
printf("\n s = (1/2) + ");
else
if(i==n)
printf(" (1/%ld) ",d);
else
printf(" (1/%ld) + ",d);
}
printf("\n\n the sum of the series ; %f ",sum);
return 0;
20

/*
Assignment 9
2. write a program to input 10 different numbers. Display the greatest and
smallest number among them.(use for loop)
*/

#include <stdio.h>
#include <math.h>

int main()
{ int num, n, l, s;
printf("\n Enter the number of elements : ");
scanf("%d", &n);
printf("\n Enter first number : ");
scanf("%d", &num);
l=n;
s=n;
for(int i=1; i<n; ++i)
{ printf("Enter another number : ");
scanf("%d", &num);
if(num>l)
l=num;
if(num<s)
s=num;
}
printf("\n\n The largest number is %d\n The smallest number is %d", l,
s);
return 0;
}
21

/*
Assignment 9
3. Using a switch case statement ,write a menu driven program to check
whether it is an Automorphic number( no. which is contained in the last
digits of its square) or niven number( no. that is divisible by the sum of digits)
.
*/

#include <stdio.h>
int main()
{ int i, num, n, ch, f=0, r, sum=0;
printf(“\t\tMENU\t\t”);
printf(“\n1.Automorphic Number\n2.Niven Number”);
printf(“\n\nEnter your choice :”);
scanf(“%d”, &ch);
switch(ch)
{ case 1 :
printf( “Enter a number : “);
scanf(“%d”, &num);
n = num;
int s =n*n, f=0;
while(n!=0)
{ if(n%10!=s%10)
{ f=1;
n=n/10;
s=s/10;
}
22

}
if(f==0)
printf(“%d is an Automorphic number.”, num);
else
printf(“%d is not an Automorphic number.”, num);
break;

case 2 :
printf(“ Enter a number : “);
scanf(“%d”, &num);
n = num;
while(n!=0)
{ r=b%10;
sum+=r;
n/=10;
}
if((num%sum)==0)
printf("%d is a Niven number.”, num);
else
printf(“%d is not a Niven number.”, num);
break;

default : printf(“Wrong Choice!”);


}
return 0;
}

/*
Assignment 9
4. write a program to print pattern
*
* #
* # *
* # * #
* # * # *
*/
23

#include <stdio.h>
int main()
{ int n;
printf(“Enter number of terms : “);
scanf(“ %d “,&n));
for(int i=1; i<=n; ++i)
{ for( int j=1; j<=i; ++j)
{ if(j%2!=0)
print(“* “);
else
print(“# “);
}
printf(“\n”);
}
return 0;
}

/*
Assignment 9
5. Write a program in function to display all composite numbers And all
prime numbers between 1 to 100
*/

#include <stdio.h>
void Prime();
void Composite();

int main()
{ Composite();
Prime();
return 0;
24

void Prime()
{ int isPrime;
printf("\nPrime numbers between 1 and 100:\n");
for ( int i= 2;i<=100; i++)
{ isPrime = 0;
for(int j =2; j<=i/2; j++)
{ if(1%j ==0)
{ isPrime =1;
break;
}
}
if(isPrime==0)
printf("%d\t", i );
}
}

void Composite()
{ int isComp;
printf("\nComposite Numbers between 1 and 100 : \n");
for(int i = 2; i<= 100; i++)
{ isComp=0;
for( int j = i-1; j>1 ; j--)
{ if(i%j == 0)
isComp = 1;
}
if(isComp == 1)
printf("%d\t", i);
}
}
25

/*
Assignment 10
1. Enter elements in a matrix and display it.
*/

#include <stdio.h>
int main()
{
int a[10][10], i, j, r, c;
printf("Enter the rows and column of a matrix : ");
scanf("%d %d", &r, &c);
for(i=0; i<r; i++)
{ for(j=0; j<c; j++)
{ printf("Enter the element : ");
scanf("%d", &a[i][j]);
}
}
printf("\nMATRIX\n");
for(i=0; i<r; i++)
{ for(j=0; j<c; j++)
{ printf(" %d ",a[i][j]);
}
printf("\n");
}
return 0;
}
26

/*
Assignment 10
2. Common element between two matrix
*/

#include <stdio.h>
int main()
{
int a[3][3], b[3][3], i, j, m, n;
printf("Enter the elements of a 3 X 3 matrix 1st : ");
for(i=0; i<3; i++)
{ for(j=0; j<3; j++)
{ printf("Enter the element : ");
scanf("%d',&a[i][j];
}
}
printf("\n1st MATRIX \n : ");
for(i=0; i<3; i++)
{ for(j=0; j<3; j++)
{ printf("%d ",a[i][j]);
}
printf("\n");
}
printf("Enter the elements of a 3 X 3 matrix 2nd : ");
for(i=0; i<3; i++)
{ for(j=0; j<3; j++)
{ printf("Enter the element : ");
scanf("%d',&a[i][j];
}
}
printf("\n2nd MATRIX \n : ");
for(i=0; i<3; i++)
{ for(j=0; j<3; j++)
{ printf("%d ",a[i][j]);
}
printf("\n");
}
printf("\n Common elements between 1st and 2nd Matrix : ");
for(i=0; i<3; ++i)
{ for(j=0; j<3; ++j)
{ for(m=0; m<3; ++m)
{ for(n=0; n<3; ++n)
27

if(a[i][j]==b[m][n])
printf(" %d",a[i][j]);
}
}
}
return 0;
}

/*
Assignment 10
3. C program to find the transpose of a matrix
*/

#include <stdio.h>
int main()
{
int a[10][10], transpose[10][10], r, c, i, j;
printf("Enter rows and columns: ");
scanf("%d %d", &r, &c);
printf("Enter matrix elements:\n");
for (i = 0; i < r; ++i)
28

{ for (j = 0; j < c; ++j)


{ printf("Enter the element: ");
scanf("%d", &a[i][j]);
}
}

printf("\nEntered matrix: \n");


for (i = 0; i < r; ++i)
{ for (j = 0; j < c; ++j)
{ printf("%d ", a[i][j]);
}
printf("\n");
}

printf("\nTranspose of the matrix:\n");


for (i = 0; i < c; ++i)
{ for (j = 0; j < r; ++j)
{ printf("%d ", a[j][i]);
}
printf("\n");
}
return 0;
}

/*
29

Assignment 10
4. Sum of two matrix
*/

#include <stdio.h>
int main()
{
int a[3][3], b[3][3], i, j, m, n;
printf("Enter the elements of a 3 X 3 matrix 1st \n");
for(i=0; i<3; i++)
{ for(j=0; j<3; j++)
{ printf("Enter the element : ");
scanf("%d",&a[i][j]);
}
}

printf("1st MATRIX\n");
for(i=0; i<3; i++)
{ for(j=0; j<3; j++)
{ printf("%d ",a[i][j]);
}
printf("\n");
}
printf("\nEnter the elements of a 3 X 3 matrix 2nd \n");
for(i=0; i<3; i++)
{ for(j=0; j<3; j++)
{ printf("Enter the element : ");
scanf("%d",&b[i][j]);
}
}

printf("2nd MATRIX\n");
for(i=0; i<3; i++)
{ for(j=0; j<3; j++)
{ printf("%d ",b[i][j]);
}
printf("\n");
}

printf("\nSum of two matrices \n");


for(i=0; i<3; i++)
{ for(j=0; j<3; j++)
30

{ printf(" %d\t",a[i][j]+b[i][j]);
}
printf("\n");
}
return 0;
}

/*
Assignment 10
5.Diagonal sum of a matrix
*/

#include <stdio.h>
int main()
31

{ int a[10][10], i , j, s, d1 = 0, d2 = 0;
printf("Enter the size of square matrix (<10): ");
scanf("%d",&s);
for(i=0; i<s; i++)
{ for(j=0; j<s; j++)
{ printf("Enter the element : ");
scanf("%d",&a[i][j]);
}
}
for(i=0; i<s; i++)
{ for(j=0; j<s; j++)
{ if(i==j)
d1+=a[i][j];
}
}
for(i=0; i<s; i++)
{ for(j=0; j<s; j++)
{ if((i+j)==(s-1))
d2+=a[i][j];
}
}
printf("\nMATRIX\n");
for(i=0; i<s; i++)
{ for(j=0; j<s; j++)
{ printf("%d ",a[i][j]);
}
printf("\n");
}
printf("\n Sum of the leading diagonal elements : %d " , d1);
printf("\n Sum of the non - leading diagonal elements : %d", d2);
return 0;
}
32

/*
Assignment 10
6. Row sum and column sum of a matrix
*/

#include <stdio.h>
int main()
{ int a[10][10], i , j, r, c, s1, s2 ;
printf("Enter the no of rows and columns of the matrix (<10): ");
scanf("%d %d",&r, &c);

for(i=0; i<r; i++)


{ for(j=0; j<c; j++)
{ printf("Enter the element : ");
scanf("%d",&a[i][j]);
}
}

printf("\nMATRIX\n");
for(i=0; i<r; i++)
{ for(j=0; j<c; j++)
{ printf("%d ",a[i][j]);
}
printf("\n");
33

}
for(i=0; i<r; i++)
{ s1=0;
for(j=0; j<c; j++)
{ s1+=a[i][j];
}
printf("\n Sum of row %d : %d ", i+1, s1);
}

for(j=0; j<c; j++)


{ s2=0;
for(i=0; i<r; i++)
{ s2+=a[i][j];
}
printf("\n Sum of column %d : %d", j+1, s2);
}
return 0;
}

You might also like