You are on page 1of 81

C Programming

Day-1
Assignment 1
Q1. Accept the radius from user and compute the area and volume of a circle.
>>Source code
#include<stdio.h>
int main()
{
int radius,area,perimeter;
printf("enter the radius of circle\n");
scanf("%d",&radius);
area = 3.14 * radius*radius;
perimeter = 2* 3.14* radius;
printf("area of circle = %d and perimeter = %d\n",area,perimeter);
return 0;
}
Op –

Q2. Accept a character from user and display ASCII value of it.

>>Source code
#include<stdio.h>
int main()
{
char a;
printf("enter the character\n");
scanf("%c",&a);
printf("ASCII value of given character is = %d\n",a);
return 0;

}
Output –
Q3. Accept marks of 5 subjects (out of 100) of a student and display total marks and compute the
percentage also.
>>Source code
#include<stdio.h>

int main()
{
int m1,m2,m3,m4,m5;
float total=0.0,percentage=0.0;
printf("enter the marks of five subject out of 100\n");
scanf("%d%d%d%d%d",&m1,&m2,&m3,&m4,&m5);
total = m1+m2+m3+m4+m5;
printf("%f", total);
percentage =(total/500)*100;
printf("total marks = %f and percentage = %f",total,percentage);
return 0;
}
Output-
Q4 Accept the basic salary of an employee and compute the net salary after adding earnings and
subtracting deductions.
// PF is 2 % of basic
// Tax is 3 % of basic
// HRA is 5 % basic
// DA is 8 % of basic
>>Source Code
#include<stdio.h>
int main()
{
int basic_sal,pf,tax,hra,da,gross_salary;
printf("enter the salary\n");
scanf("%d",&basic_sal);
pf= (basic_sal*2)/100;
tax = (basic_sal*3)/100;
hra =(basic_sal*5)/100;
da= (basic_sal*8)/100;

gross_salary = basic_sal-pf-tax+hra+da;
printf("gross salary = %d",gross_salary);
return 0;
}
Output-

Q5. Accept two numbers and swap two numbers using

i) third variable

>>Source code -
#include<stdio.h>
int main()
{
int n1,n2,temp;
printf("enter the two numbers\n");
scanf("%d%d",&n1,&n2);
printf("before swapping n1 = %d, n2 = %d",n1,n2);
temp = n1;
n1=n2;
n2=temp;
printf("after swapping n1 = %d, n2 = %d",n1,n2);
return 0;
}
Output –

//ii) by performing arithmetic operations.


#include<stdio.h>
int main()
{
int n1,n2;
printf("enter the two numbers\n");
scanf("%d%d",&n1,&n2);
printf("before swapping n1 = %d, n2 = %d",n1,n2);
n1 = n1 + n2;
n2 =n1-n2;
n1=n1-n2;
printf("after swapping n1 = %d, n2 = %d",n1,n2);
return 0;
}

Output –
C Programming
Day-2
Assignment 2
Q1 Accept dimensions of a cylinder and print the surface area and volume (Hint: surface area = 2πr^2 +
2πrh, volume = π r^2 h).
Define a constant variable pi=3.14.

>>Source code-
#include<stdio.h>
int main()
{
float r,h,area,volume;
const float pi=3.14;
printf("enter the dimension of cylinder\n");
scanf("%f%f",&r,&h);
volume = 2*pi*r*r + 2*pi*r*h;
area = pi*r*r*h;

printf("area of cylinder = %f volume = %f\n",area,volume);


return 0;
}
Output-

Q2.Accept temperatures in Fahrenheit (F) and print it in Celsius(C) and Kelvin (K) (Hint: C=5/9(F-32), K =
C + 273.15) doubt not showing result
>>Source code
#include<stdio.h>
int main()
{ int temp;
float celcius=0.0,k=0.0;
printf("enter the temperature\n");
scanf("%d",&temp);
celcius=((temp-32)*5)/9;
k = celcius + 273.15;
printf("Temperature in celsius = %f , kelvin = %f",celcius,k);
return 0;
}
Output –

Q3 Write a program to accept an integer and check if it is even or odd.


>>Source code
#include<stdio.h>
int main()
{
int num;
printf("enter the num\n");
scanf("%d",&num);
if(num%2==0)
printf("even\n");
else
printf("odd\n");
return 0;
}
Output –

Q4. Write a program to accept a number and check if it is divisible by 5 and 7.

>>Source code
#include<stdio.h>
int main()
{
int num;
printf("enter the num\n");
scanf("%d",&num);

if(num%5==0&&num%7==0)
printf("num is divisible by 5 and 7\n");
else
printf("num is not divisible by 5 and 7\n");

return 0;
}
Output-

Q5.Write a program, which accepts annual basic salary of an employee and calculates and displays the
Income tax as per the following rules. Basic: < 1,50,000 Tax = 0 ; 1,50,000 to 3,00,000 Tax = 20%
> 3,00,000 Tax = 30%

>>Source code
#include<stdio.h>
int main()
{
int basic_sal,tax;
printf("enter the salary\n");
scanf("%d",&basic_sal);

if(basic_sal<150000)
printf("Tax = 0");
else if(150000<basic_sal>300000)
{
tax = (basic_sal*20)/100;
printf("Tax = %d",tax);
}
else
{
tax = (basic_sal*30)/100;
printf("Tax = %d",tax);
}
return 0;
}
Output –

Q6. Accept a lowercase character from the user and check whether the character is a vowel or
consonant. (Hint: a,e,i,o,u are vowels)
>>Source code
#include<stdio.h>
int main()
{
char ch;
printf("enter the character\n");
scanf("%c",&ch);
if(ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u')
printf("it is vowel\n");
else
printf("it is consonant\n");
return 0;
}
Output-
Q7. Write a C program to input angles of a triangle and check whether triangle is valid or not
>>Source code
#include<stdio.h>
int main()
{
int a1,a2,a3,total;
printf("enter the angles of triangle\n");
scanf("%d%d%d",&a1,&a2,&a3);
total = a1+a2+a3;
if(total==180)
printf("valid triangle\n");
else
printf("invalid triangle\n");
return 0;
}
Output –

Q8. Write a C program to check whether a entered character is uppercase or lowercase alphabet.
>>Source code
#include<stdio.h>
int main()
{
char ch;
printf("enter the character\n");
scanf("%c",&ch);
if(ch>='a'&&ch<='z')
printf("character is lower case\n");
else
printf("character is upper case\n");
return 0;
}
Output –
Q9.
>>Source code
//9. Write a C program to accept a character and invert the case of it.

#include<stdio.h>
// doubt
int main()
{
char ch,up,lo;
printf("enter the character\n");
scanf("%c",&ch);
if(ch>='a'&&ch<='z')
{
printf("character is lower case\n");
up = ch-32;
printf("equivalent character = %c\n",up);
}
else
{
printf("character is upper case\n");
lo = ch+32;
printf("equivalent character = %c\n",up);
}
return 0;
}
Output –
Q10. Write a program to accept 3 numbers and compute minimum and maximum from them.
>>Source code
#include<stdio.h>
int main()
{
int n1,n2,n3;
printf("enter the number\n");
scanf("%d%d%d",&n1,&n2,&n3);
if(n1>n2)
{
if(n1>n3)
printf("max = %d",n1);
else
printf("max = %d",n3);
}
else
{
if(n2>n3)
printf("max = %d",n2);
else
printf("max = %d",n3);
}
if(n1<n2)
{
if(n1<n3)
printf("min = %d",n1);
else
printf("min = %d",n3);
}
else
{
if(n2<n3)
printf("min = %d",n2);
else
printf("min = %d",n3);
}
return 0;
}
Output –
Day-3
Assignment 3
Q1. Accept a single digit from the user and display it in words. For example, if digit entered is 9, display
Nine.
>>Source code-
#include<stdio.h>
int main()
{
int a;
printf("enter the single digit number\n");
scanf("%d",&a);
switch(a)
{
case 1:
printf("one\n");
break;
case 2:
printf("two\n");
break;
case 3:
printf("three\n");
break;
case 4:
printf("four\n");
break;
case 5:
printf("five\n");
break;
case 6:
printf("six\n");
break;
case 7:
printf("seven\n");
break;
case 8:
printf("eight\n");
break;
case 9:
printf("nine\n");
break;
}
return 0;
}
Output –
Q2. Write a program, which accepts two integers and an operator as a character (+ - * / ), performs the
corresponding operation and displays the result.
>>Source code-
#include<stdio.h>
int main()
{
int a,b,result;
char ch;
printf("enter the two numbers\n");
scanf("%d%d",&a,&b);
printf("enter the operation\n");
scanf(" %c",&ch);
switch(ch)
{
case '+':
result = a+b;
printf("addition = %d",result);
break;
case '-':
result = a-b;
printf("substraction = %d",result);
break;
case '*':
result = a*b;
printf("multiplication = %d",result);
break;
case '/':
result = a/b;
printf("divide = %d",result);
break;
default :
printf("invalid choice");
break;
}
return 0;
}
Output –

Q3.
>>Source code
#include<stdio.h>
// q3
int main()
{
int x,y,c,rem,d,temp;
float quo;
printf("enter the two numbers\n");
scanf("%d%d",&x,&y);
printf("enter the choice\n");
scanf(" %d",&c);
switch(c)
{
case 1:
if(x==y)
printf("x == y\n");
else
printf("x is not equal to y\n");
break;
case 2:
if(x<y)
printf("x is less than y\n");
else
printf("x is not less than y\n");
break;
case 3:
quo = (float)x/y;
rem = x%y;
printf("quotient = %f and reminder =%d\n",quo,rem);
break;
case 4:
printf("enter the number to check\n");//x=20 y=10
scanf("%d",&d);//15
if(x<d)
{
if(d<y)
printf(" %d is in the range\n",d);
}
else if(d<x)
{
if(d>y)
printf(" %d is in the range\n",d);
}
else
printf(" %d not in the range\n",d);
break;

case 5:
temp =x;
x=y;
y=temp;
printf("swapping x= %d and y=%d\n",x,y);
break;
default :
printf("invalid choice\n");
}
}
Output –
Q4
>>Source code –
#include<stdio.h>
//q4
int main()
{
float r,area,pi=3.14,circumference,volume,height;
int choice;
printf("enter the radius os cicle\n");
scanf("%f",&r);
printf("enter the choice\n");
scanf("%d",&choice);
switch(choice)
{
case 1:
area = pi*r*r;
printf("area of circle = %f\n",area);
break;
case 2:
circumference = 2*pi*r;
printf("circumference = %f\n",circumference);
break;
case 3:
printf("enter the height of sphere\n");
scanf("%f",&height);
volume = pi*r*r*height;
printf("volume = %f",volume);
break;
default :
printf("invalid choice\n");
}
return 0;
}
Output –

Q5
>>Source code
#include<stdio.h>
int main()
{
float n1,n2,d1,d2,a,b,c;
int choice;
printf("enter the numerators\n");
scanf("%f%f",&n1,&n2);
printf("enter the denominator\n");
scanf("%f%f",&d1,&d2);
printf("1.ADD\n 2.SUBCTRACT\n 3. MULTIPLY");
printf("enter the choice\n");
scanf("%d",&choice);
switch (choice)
{
case 1:
a= (n1*d2+n2*d1);
b= d1*d2;
c= a/b;
printf("result = %f",c);
break;
case 2:
a= (n1*d2-n2*d1);
b= d1*d2;
c= a/b;
printf("result = %f",c);
break;
case 3:
a= (n1*n2);
b= d1*d2;
c= a/b;
printf("result = %f",c);
break;

default:
printf("invalid choice\n");
break;
}
return 0;
}
Output –
Day-4
Assignment 4
Q1. Write a program that accepts numbers continuously as long as the number is positive and prints the
sum of the given numbers.

>>Source code-
#include<stdio.h>
int main()
{
int a,total=0;
printf("enter the numbers\n");
for(;a!=0;)
{
scanf("%d",&a);
total = total +a;
}
printf("addition of numbers = %d\n",total);
return 0;
}
Output –

Q2.Write a program to accept two integers x and n and compute x raised to n.


>>Source code-
#include<stdio.h>
int main()
{
int x,n,result,i;
printf("enter the two numbers\n");
scanf("%d%d",&x,&n);
result=x;
for(i=1;i<n;i++)
{
result = result*x;
}
printf("%d raise to %d = %d",x,n,result);
return 0;
}
Output-

Q3.Write a program to accept a character, an integer n and display the next n characters.
>>Source code
#include<stdio.h>
int main()
{
int n,i;
char ch;

printf("enter the integer\n");


scanf("%d",&n);
printf("enter the character\n");
scanf(" %c",&ch);

for(i=1;i<n;i++)
{
printf("character at %d is %c\n",ch+i,ch+i);
}
return 0;
}
Output-
Q4.Write a program to calculate factorial of a number.
>>Source code
#include<stdio.h>
int main()
{
int n,result=1,i;
printf("enter the integer\n");
scanf("%d",&n);
for(i=1;i<=n;i++)
result = result *i;
printf("factorial of %d is %d",n,result);
return 0;
}
Output-

Q5.Write a program to calculate factors of a given number.

>>Source code
#include<stdio.h>
int main()
{
int n,result=1,i;
printf("enter the integer\n");
scanf("%d",&n);
printf("factors of %dis\t",n);
for(i=1;i<=n;i++)
{
if(n%i==0)
printf("%d,",i);
}
return 0;
}
Output –

Q6. Accept two numbers and calculate GCD of them.


>>Source Code-
#include<stdio.h>
int main()
{
int a,b,gcd,i;
printf("enter the two integer\n");
scanf("%d%d",&a,&b);
for(i=1;i<=a&&i<=b;i++)
{
if(a%i==0&&b%i==0)
gcd=i;
}
printf("GCD of %d and %d = %d",a,b,gcd);
return 0;
}
Output-

Q7
>>Source code-
#include<stdio.h>
int main()
{
float r,area,pi=3.14,h,b;
int choice,len,wid;
do
{
printf("\n1.Compute area of circle\n");
printf("2.Compute area of rectangle\n");
printf("3.Compute area of triangle\n");
printf("4.exit\n");
printf("enter your choice\n");
scanf("%d",&choice);
switch (choice)
{
case 1:
printf("enter the radius of circle\n");
scanf("%f",&r);
area = 2*pi*r*r;
printf("area of circle = %f\n",area);
break;
case 2:
printf("enter the width and length of rectagle\n");
scanf("%d%d",&len,&wid);
area = len*wid;
printf("area of rectangle = %d",area);
break;
case 3:
printf("enter the height and base of triangle\n");
scanf("%f%f",&h,&b);
area=(h*b)/2;
printf("area of triangle = %f\n",area);
break;
case 4:
printf("exit\n");
break;
default:
printf("invalid choice\n");
break;
}
} while (choice!=4);
return 0;
}
Output –
Day-5
Assignment 5
Q1.Write a program to accept n numbers in an array and display the largest and smallest number. Using
these values, calculate the range of elements in the array.
>>Source code-
#include<stdio.h>
int main()
{
int arr[100],i,n,min,max;
printf("enter the size of array\n");
scanf("%d",&n);
printf("enter the elements of array\n");
for(i=0;i<n;i++)
scanf("%d",&arr[i]);
min = max= arr[0];
for(i=0;i<n;i++)
{
if(min>arr[i])
min = arr[i];
}
for(i=0;i<n;i++)
{
if(max<arr[i])
max = arr[i];
}
printf("min = %d and max =%d\n",min,max);
printf("The range of element in array in between %d to %d",min,max);
return 0;
}
Output-

Q2. Write a program to accept an array of n elements and a number say key. Check whether key is
present in the array or not.
>>Source code
#include<stdio.h>
int main()
{
int arr[100],i,j=0,key,n;
printf("enter the size of array\n");
scanf("%d",&n);
printf("enter the elements of array\n");
for(i=0;i<n;i++)
scanf("%d",&arr[i]);
printf("enter the key value\n");
scanf("%d",&key);

for(i=0;i<n;i++)
{
if(key==arr[i])
j= arr[i];
}
if(j)
printf("key is present in array\n");
else
printf("key is not present in array\n");
return 0;
}
Output-
Q3. Write a program to accepts an integer array and an integer say num and counts the occurrences of
the num in the array.
>>Source code
#include<stdio.h>
int main()
{
int arr[100],i,count=0,n,key;
printf("enter the size of array\n");
scanf("%d",&n);
printf("enter the key value\n");
scanf("%d",&key);

printf("enter the elements of array\n");


for(i=0;i<n;i++)
scanf("%d",&arr[i]);
for(i=0;i<n;i++)
{
if(key==arr[i])
count = count+1;
}
printf("the %d in an array %d times\n",key,count);
return 0;
}
Output-
Q4. Write a program to accept n numbers from the user and store them in an array. Then sort the array
in descending order and display it.
>>Source code
#include<stdio.h>
int main()
{
int arr[100],i,j,k,n;
printf("enter the size of array\n");
scanf("%d",&n);
printf("enter the elements of array\n");
for(i=0;i<n;i++)
scanf("%d",&arr[i]);

//k=arr[0]; //10,20,35,5,40
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(arr[i]<arr[j])
{
k=arr[i];
arr[i]=arr[j];
arr[j]=k;
}
}
}

printf("array elememts are\n");


for(i=0;i<n;i++)
printf("%d\t",arr[i]);
}
Output-
Q5. Write a program to accept a decimal number and convert it to binary.
>>Source Code-
#include<stdio.h>
int main()
{
int arr[100],i,j,k;
printf("enter the number\n");
scanf("%d",&i);
for(j=0;i>0;j++)
{
arr[j] = i%2;
i=i/2;
}
printf("\n");
printf("binary of %d = ",i);
for(j=j-1;j>=0;j--)
printf("%d",arr[j]);
printf("\n");
return 0;
}
Output-
2D array
Q1. Write a program to accept, display and print the sum of elements of each row and sum of elements
of each column of a matrix.
>>Source code
#include<stdio.h>
int main()
{
int arr[3][3],i,j,k,sr1=0,sr2=0,sr3=0;
int sc1=0,sc2=0,sc3=0;
printf("enter the elements of matrix");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
scanf("%d",&arr[i][j]);
}
for(i=0;i<3;i++)
{
sr1= sr1 + arr[0][i];
sr2= sr2 + arr[1][i];
sr3= sr3 + arr[2][i];
sc1= sc1 + arr[i][0];
sc2= sc2 + arr[i][1];
sc3= sc3 + arr[i][2];
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%d\t",arr[i][j]);
}
printf("\n");
}
printf("sum of 1st row =%d, 2nd row = %d, 3rd row = %d\n",sr1,sr2,sr3);
printf("sum of 1st column =%d, 2nd column = %d, 3rd column = %d\n",sc1,sc2,sc3);
return 0;

}
Output-
Q2. Write a program to accept a matrix A of size mXn and store its transpose in matrix B. Display matrix
B

>>Source code-
#include<stdio.h>
int main()
{
int arr[10][10],i,j,k,b[10][10];
int row,col;
printf("enter the row nd colum\n");
scanf("%d%d",&row,&col);
printf("enter the elements of matrix");
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
scanf("%d",&arr[i][j]);
}
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
b[i][j] = arr[j][i];
}
printf("entered matrix is\n");
printf("\n");
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
printf("%d\t",arr[i][j]);
}
printf("\n");
}
printf("transpose of given matrix is\n");
printf("\n");
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
printf("%d\t",b[i][j]);
}
printf("\n");
}
return 0;
}

Output-

Q3. Write a program to add and multiply two matrices. Perform necessary checks before adding and
multiplying the matrices.
>>Source code-
#include<stdio.h>
int main()
{
int a[3][3],b[3][3],c[3][3];
int i,j,sum,multi;

printf("enter the 1st matrix element\n");


for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
scanf("%d",&a[i][j]);
}

printf("enter the 2nd matrix element\n");

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

printf("1st matrix is\n");


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

printf("2nd matrix is\n");


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

for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
c[i][j] = a[i][j] + b[i][j];
}

printf("Addition of matrix is\n");


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

for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
c[i][j] = a[i][j] * b[i][j];
}
printf("Multiplication of matrix is\n");
printf("\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%d\t",c[i][j]);
}
printf("\n");
}
return 0;
}
Output-
Q4-a
>>Source code-
#include<stdio.h>
int main()
{
int a[4][4],b[4][4],c[4][4];
int i,j,k;
printf("enter the 1st matrix element\n");
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
scanf("%d",&a[i][j]);
}
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
b[i][j] =a[j][j];
}
printf("\n");
k=1;
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
if(b[i][j] !=a[i][j])
{
k++;
break;
}
}
}
if(k==1)
printf("matrix is symmetric\n");
else
printf("matrix is not symmetric\n");
return 0;
}
Output-
Q4-b
>>Source Code-
#include<stdio.h>
int main()
{
int a[4][4],b[4][4],c[4][4];
int i,j,trace=0;

printf("enter the 1st matrix element\n");


for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
scanf("%d",&a[i][j]);
}
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
if(i==j)
trace = trace +a[i][j];
}
}
printf("trace of matrix = %d\n",trace);
printf("\n");
return 0;
}
Output-
Q4-c
>>Source code-
//Check if the matrix is an upper triangular matrix.
#include<stdio.h>
int main()
{
int a[4][4],b[4][4],c[4][4];
int i,j,k;

printf("enter the 1st matrix element\n");


for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
scanf("%d",&a[i][j]);
}
k=1;
for(i=1;i<4;i++)
{
for(j=0;j<i;j++)
{
if(a[i][j]!=0)
{
k=0;
break;
}
}
}
if(k==1)
printf("matrix is upper triangular\n");
else
printf("matrix is not upper triangular\n");
return 0;
}
Output-

String
Q1.
#include<stdio.h>
#include<string.h>

int is_lower(char ch);


char to_upper(char ch);
char to_lower(char ch);
int is_digit(char ch);
void chg_str(char str[]);
int main()
{
char str[100];
printf("\n Enter any sentence:-");
printf("\n");
gets(str);
chg_str(str);
return 0;
}
void chg_str(char str[])
{
int i,j=0;
for(i=0;i<=strlen(str)-1;i++)
{
if(str[i]==' ')
str[i]='*';
if(is_lower(str[i]))
str[i]=to_upper(str[i]);
else
str[i]=to_lower(str[i]);
if(is_digit(str[i]))
str[i]='?';
}
printf("\n %s \n",str);
}
int is_lower(char ch)
{
if(96<ch)
{
if(ch<123)
return 1;
}
return 0;
}
char to_upper(char ch)
{
char i;
i = ch-32;
return i;
}
char to_lower(char ch)
{
char i;
if(ch>64)
{
if(ch<91)
{
i = ch+32;
return i;
}

}
}
int is_digit(char ch)
{
if(ch>=0)
{
if(ch<10)
return 1;
else
return 0;
}
else
return 0;
}
Output-

Q2. Write a program that accepts n strings and displays the longest string. Use array of strings.
>>Sources code-
#include<stdio.h>
#include<string.h>
int main()
{
char arr[100][100];
int i,j,k,num,len;
printf("enter the number of strings required\n");
scanf("%d",&num);
printf("enter the strings\n");
for(i=0;i<num;i++)
scanf("%s",&arr[i]);
len = strlen(arr[0]);
k=0;
for(i=0;i<num;i++)
{
if(len<strlen(arr[i]))
{
len = strlen(arr[i]);
k=i;
}
}
printf("longest string is %s its length = %d",arr[k],len);

return 0;
}
Output-
Day-6
Assignment 6
Q1
>>Souce code-
#include<stdio.h>
int main()
{
int arr[100],i,j,sum_even=0,num,sum_odd=0;
printf("enter the number of element in array\n");
scanf("%d",&num);
printf("enter the array element\n");
for(i=0;i<num;i++)
scanf("%d",&arr[i]);
int *ptr = &arr[0];
for(i=0;i<num;i++)
{
if(*ptr%2==0)
{
sum_even = sum_even + arr[i];
}
else
{
sum_odd = sum_odd +arr[i];
}
ptr++;
}
printf(" sum of all even no = %d\n sum of all odd no is %d\n",sum_even,sum_odd);
return 0;
}

Output-
Q2
>>Source code-
#include<stdio.h>
int iseven(int num)
{
if(num%2==0)
return 1;
else
return 0;
}
int main()
{
int num,i;
printf("enter the number\n");
scanf("%d",&num);

i = iseven(num);
if(i==1)
printf("number is even\n");
else
printf("number is odd\n");

return 0;
}
Output-
Q3
>>Soource code
#include<stdio.h>
int isprime(int num)//11
{
int i,j=0;
for(i=2;i<num/2;i++)
{
if(num%i!=0)
continue;
else
return 0;
}
return 1;
}
int main()
{
int num,i,j;
printf("enter the number\n");
scanf("%d",&num);

j= isprime(num);
if(j==1)
printf("number is prime\n");
else
printf("number is not prime\n");
return 0;
}
Output –

Q4.
>>Source code-
# include <stdio.h>
long factorial(int x)
{
int i;
long fact=1;
for(i=1;i<=x;i++)
fact=fact*i;
return fact;
}
double power(int x,int y)
{
double ans=1.0;
int i;
for(i=1;i<=y;i++)
ans=ans*x;
return ans;
}
void main()
{
int i,n,cnt=3;
float x;
double sum=0;
printf("\nEnter value for x : ");
scanf("%f",&x);
printf("\nEnter number of terms : ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
if(i==1)
sum=x;
else
{
if(i%2==0) // for even terms subtract
{
sum=sum - (power(x,cnt)/factorial(cnt));
}
else // for odd terms add
{
sum=sum + (power(x,cnt)/factorial(cnt));
}
cnt=cnt+2; // it makes cnt as 5,7,9,...
}

}
printf("\nsum of Taylor series of %d terms : %f",n,sum);
}
Output-
Q5
CPP PROGRAMMING
DAY-1
ASSIGNMENT NO-1
Q1
>>Source code
#include<iostream>
using namespace std;
int main()
{
int a,b,sum;
cout<<"enter the two number"<<endl;
cin>>a>>b;
sum = a+b;
cout<<"sum of two number is = "<<sum<<endl;
return 0;
}
Output-

Q2. Write a program to swap two numbers.


>>Source code
#include<iostream>
using namespace std;
int main()
{
int a,b,temp;
cout<<"enter the two number"<<endl;
cin>>a>>b;
temp = a;
a=b;
b= temp;
cout<<"after swapping a= "<<a<<" b = "<<b<<endl;
return 0;
}
Output –
Q3.Write a program to find factorial of a given number.
>>Source code
#include<iostream>
using namespace std;
int main()
{
int a,i,fact=1;
cout<<"enter the number"<<endl;
cin>>a;
for(i=1;i<=a;i++)
{
fact = fact*i;
}
cout<<"factorial of "<<a<<" is = "<<fact<<endl;
return 0;
}
Output –

Q4. Write a program to find m to the power n.


>>Source code
#include<iostream>
using namespace std;
int main()
{
int m,n,i,power=1;
cout<<"enter the number"<<endl;
cin>>m;
cout<<"enter the power"<<endl;
cin>>n;
for(i=1;i<=n;i++)
{
power = power * m;
}
cout<<m<<" raise to "<<n<<" = "<<power<<endl;
return 0;
}
Output-

Q5.Check if number is a prime number or not.


>>Source code
#include<iostream>
using namespace std;
int main()
{
int num,i,j=0;
cout<<"enter the number to check"<<endl;
cin>>num;
for(i=2;i<num;i++)
{
if(num%i==0)
j++;
}
if(j)
cout<<"number is not prime"<<endl;
else
cout<<"number is prime"<<endl;

return 0;
}
Output-
Q6.Sum of series :
1+2+3+….+n
>>Source code-
#include<iostream>
using namespace std;
int main()
{
int num,i,sum=0;
cout<<"enter the number"<<endl;
cin>>num;
for(i=1;i<=num;i++)
{
sum = sum +i;
}
cout<<"sum of number = "<<sum<<endl;
return 0;
}
Output-

Q7. Check whether the number is palindrome or not?


>>Souce code-
#include<iostream>
using namespace std;
int main()
{
int num,rem,i,rev=0,temp;
cout<<"enter the number"<<endl;
cin>>num;
temp=num;
for(i=1;temp>0;i++)
{
rem = temp%10;
rev = rev*10+ rem;
temp=temp/10;
}
if(num==rev)
cout<<"entered number is palindrome"<<endl;
else
cout<<"entered number is not palindrome"<<endl;
return 0;
}
Output-

Q8.Write a program to find sum of all even and odd numbers between 1 to n.
>>Souce code-
#include<iostream>
using namespace std;
int main()
{
int num,i,sum_even=0,sum_odd=0;
cout<<"enter the number"<<endl;
cin>>num;
for(i=1;i<=num;i++)
{
if(i%2==0)
sum_even= sum_even +i;
else
sum_odd= sum_odd+i;
}
cout<<"sum of all even no in between 1 and "<<num<<"is = "<<sum_even<<endl;
cout<<"sum of all odd no in between 1 and "<<num<<"is = "<<sum_odd<<endl;
return 0;
}
Output-

Q9.Write a program to enter a number and print its reverse.


>>Souce code-
#include<iostream>
using namespace std;
int main()
{
int num,i,rev=0,temp,rem;
cout<<"enter the number"<<endl;
cin>>num;
temp = num;
for(i=1;temp>0;i++)
{
rem = temp%10;
rev = rev*10+ rem;
temp=temp/10;
}
cout<<"reverse of the "<<num<<" is = "<<rev<<endl;
return 0;
}
Output-

Q10.Write a program to print all Prime numbers between 1 to n.


>>Souce code-
#include<iostream>
using namespace std;
int main()
{
int x, i, j, f;
cout<<"Enter the range number to print the prime numbers:\n";
cin>>x;
cout<<"\nThe prime numbers between 1 and " << x << " are:\n";
for (i = 1; i <= x; i++)
{
if (i == 1 || i == 0)
{
continue;
}
f = 1;
for (j=2;j<=i/2;++j)
{
if (i%j==0)
{
f = 0;
break;
}
}
if (f==1)
{
cout<<" "<<i;
}
}
return 0;
}
Output-

Q11. Write a program to check entered number is Armstrong number or not.


>>Souce code-
#include<iostream>
using namespace std;
int main()
{
int num,i,rem,sum=0,temp;
cout<<"enter the number"<<endl;
cin>>num;
temp = num;
for(i=1;temp>0;i++)
{
rem = temp%10;
sum = sum + rem*rem*rem;
temp = temp/10;
}
if(num==sum)
cout<<"entered number is armstrong"<<endl;
else
cout<<"entered number is not armstrong"<<endl;
return 0;
}
Output-
Q12. Write a program to find greatest of three numbers using nested if-else.
>>Source code-
#include<iostream>
using namespace std;
int main()
{
int a,b,c;
cout<<"enter the three numbers"<<endl;
cin>>a>>b>>c;
if(a>b)
{
if(a>c)
cout<<"greatest number is "<<a<<endl;
else
cout<<"greatest number is "<<c<<endl;
}
else
{
if(b>c)
cout<<"greatest number is "<<b<<endl;
else
cout<<"greatest number is "<<c<<endl;
}
return 0;
}
Output-

Q13.
>>Source code-
#include<iostream>
using namespace std;

int main()
{
int option,p=0;
cout<<"Welcome to Our Pizza Shop";
do
{
cout<<"Select Menu You love to order"<<endl<<"1]Greek Pizza-500"<<endl<<"2]California Pizza-
800"<<endl<<"3]Silician Pizza-750"<<endl<<"4]New-york style Pizza-1000"<<endl<<"5]Order Now-";
cin>>option;
switch (option)
{
case 1:
p+=500;
break;
case 2:
p+=800;
break;
case 3:
p+=750;
break;
case 4:
p+=1000;
break;
}
} while (option!=5);
cout<<"Your Order is Ready!!!"<<endl<<"Total Amount:"<<p<<" only";
return 0;
}
Output-
Q14.
>>Source code-
#include<iostream>
using namespace std;
int main()
{
int arr[100],i,key,num,choice,flag=0;
cout<<"enter the number of element in array"<<endl;
cin>>num;
cout<<"enter the elements in array"<<endl;
for(i=0;i<num;i++)
cin>>arr[i];

do
{
cout<<"1. print array"<<endl;
cout<<"2. search element array"<<endl;
cout<<"3. reverse array"<<endl;
cout<<"4. Even number from array"<<endl;

cout<<"enter the choice"<<endl;


cin>>choice;
switch (choice)
{
case 1:
cout<<"entered array is"<<endl;
for(i=0;i<num;i++)
cout<<" "<<arr[i];
break;
case 2:
cout<<"enter key element in array"<<endl;
cin>>key;
for(i=0;i<num;i++)
{
if(key==arr[i])
flag++;
}
if(flag==1)
cout<<"key is present in array"<<endl;
else
cout<<"key is not present in array"<<endl;
break;
case 3:
cout<<"reverse of array is "<<endl;
for(i=num-1;i>=0;i--)
cout<<" "<<arr[i];
break;
case 4:
cout<<"even number in array is "<<endl;
for(i=0;i<num;i++)
{
if(arr[i]%2==0)
cout<<" "<<arr[i];
}
break;
default:
cout<<"invalid choice"<<endl;
break;
}
} while (choice!=0);
return 0;
}
Output-
Q15.
>>Source code-
#include<iostream>
using namespace std;

int main()
{
int i,j,num;
cout<<"enter the number"<<endl;
cin>>num;
for(i=1;i<=num;i++)
{
for(j=1;j<=i;j++)
cout<<"* ";
cout<<endl;
}
return 0;
}
Output-
DAY-2&3
ASSIGNMENT NO-2
Q1. Write a program to create student class with data members rollno, marks1,mark2,mark3.Accept
data (acceptInfo()) and display using display member function. Also display total,percentage and grade.
>>Source code
#include<iostream>
using namespace std;

class student
{
int roll_no,mark1,mark2,mark3;

public:
void acceptinfo()
{
cout<<"enter the roll no. and marks"<<endl;
cin>>roll_no>>mark1>>mark2>>mark3;
}

void printdata()
{
cout<<"roll no is "<<roll_no<<" mark1 = "<<mark1<<" mark2 = "<<mark2<<" mark3 =
"<<mark3<<endl;
cout<<"total = "<<mark1+mark2+mark3<<endl;
cout<<"percentage = "<< ((mark1+mark2+mark3)*100)/300<<endl;

}
};
int main()
{
student s1;
s1.acceptinfo();
s1.printdata();
return 0;

}
Output-

Q2.
>>Source code-
#include<iostream>
using namespace std;

class person
{
string name;
int age;
string city;

public :
person()
{
name = "abc";
age = 0;
city ="xyz";
}

person(string name,int age,string city)


{
this->name = name;
this->age = age;
this->city =city;
}
void setname(string str)
{
name = str;
}
string getname()
{
return name;
}
void setage(int i)
{
age = i;
}
int getage()
{
return age;
}
void setcity(string ct)
{
city = ct;
}
string getcity()
{
return city;
}

void display()
{
cout<<"name = "<<name<<" age = "<<age<<" city = "<<city<<endl;
}
};
int main()
{
person p1;
p1.setname("shri");
p1.setcity("nanded");
p1.setage(25);
p1.display();

person p2;
p2.display();

person p3("ram",25,"cpur");
p3.display();

return 0;
}
Output-

Q3.
>>Source code-
#include<iostream>
using namespace std;
class date
{
int dd;
int mm;
int yy;
public :
date()
{
dd = 00;
mm = 00;
yy = 00;
}
date(int d,int m,int y)
{
this->dd = d;
this->mm = m;
this->yy = y;
}
void setdd(int d)
{
dd=d;
}
void setmm(int m)
{
mm=m;
}
void setyy(int y)
{
yy=y;
}

int getdd()
{
return dd;
}
int getmm()
{
return mm;
}
int getyy()
{
return yy;
}

void display()
{
cout<<"date is "<<dd<<"/"<<mm<<"/"<<yy<<endl;
}

};
int main()
{
date d1;
d1.setdd(17);
d1.setmm(9);
d1.setyy(2021);
d1.display();
date d2;
d2.display();

date d3(17,9,2021);
d3.display();
return 0;
}
Output-

Q4.
#include<iostream>
using namespace std;
class book
{
string bname;
int id;
string author;
int price;
public :
book()
{
bname = "none";
id = 00;
author = "none";
price = 00;
}

book(string bname,int id,string auth,int price)


{
this->bname = bname;
this->id = id;
this->author = auth;
this->price = price;
}

void setbname(string bnam)


{
bname = bnam;
}
void setid(int m)
{
id=m;
}
void setauthor(string y)
{
author=y;
}
void setprice(int mrp)
{
price =mrp;
}

string getbname()
{
return bname;
}
int getid()
{
return id;
}
string getauthor()
{
return author;
}

int getprice()
{
return price;
}

void display()
{
cout<<"book name is **"<<bname<<"**"<<" id is "<<id<<" author is "<<author<<" mrp =
"<<price<<endl;
}

};

int main()
{
book b1;
b1.setbname("life amazing secreate");
b1.setid(101);
b1.setauthor("gaur gopal das");
b1.setprice(100);
b1.display();

book b2;
b2.display();

book b3("5 am club",200,"walt disney",500);


b3.display();
return 0;
}
Output-

Q5.
>>Source code-
#include<iostream>
using namespace std;
class point
{
int x;
int y;

public :
point()
{
x=0;
y=0;
}
point(int x, int y)
{
this->x=x;
this->y=y;
}

void setx(int a)
{
x= a;
}
void sety(int b)
{
y= b;
}
int getx()
{
return x;
}
int gety()
{
return y;
}

void display()
{
cout<<"x = "<<x<<" y = "<<y<<endl;
}
};

int main()
{
point p1;
p1.setx(10);
p1.sety(20);
p1.display();

point p2;
p2.display();

point p3(100,200);
p3.display();
return 0;
}
Output-
Q6.
>>Source code-
#include<iostream>
using namespace std;
class ComplexNumber
{
int real;
int imaginary;

public :
ComplexNumber()
{
real=0;
imaginary=0;
}
ComplexNumber(int x, int y)
{
this->real=x;
this->imaginary=y;
}

void setreal(int a)
{
real= a;
}
void setimaginary(int b)
{
imaginary= b;
}
int getreal()
{
return real;
}
int getimaginary()
{
return imaginary;
}
void display()
{
cout<<"real = "<<real<<" imaginary = "<<imaginary<<endl;
}
};
int main()
{
ComplexNumber p1;
p1.setreal(10);
p1.setimaginary(20);
p1.display();

ComplexNumber p2;
p2.display();

ComplexNumber p3(100,200);
p3.display();
return 0;
}

Q7.
>>Source code-
#include<iostream>
using namespace std;
class date
{
int day;
int month;
int year;

public :
date()
{
day = 00;
month = 00;
year = 00;
}
date(int d,int m,int y)
{
this->day = d;
this->month = m;
this->year = y;
}

void setday(int d)
{
day=d;
}
void setmonth(int m)
{
month=m;
}
void setyear(int y)
{
year=y;
}

int getday()
{
return day;
}
int getmonth()
{
return month;
}
int getyear()
{
return year;
}

void display()
{
cout<<"date is "<<day<<"/"<<month<<"/"<<year<<endl;
}

};

int main()
{
date d1;
d1.setday(17);
d1.setmonth(9);
d1.setyear(2021);
d1.display();
date d2;
d2.display();

date d3(17,9,2021);
d3.display();
return 0;
}

Q8
>>Source code
#include<iostream>
using namespace std;

class date
{
int day;
int month;
int year;

public :
date()
{
day = 00;
month = 00;
year = 00;
}

date(int d,int m,int y)


{
this->day = d;
this->month = m;
this->year = y;
}
void displaydob()
{
cout<<"DOB is "<<day<<"/"<<month<<"/"<<year<<endl;
}
void acceptdob()
{
cout<<"enter the DOB"<<endl;
cin>>day>>month>>year;
}
};

class employee
{
int id;
string name;
date dobj;

public :
employee()
{
id = 00;
name ="none";
}
employee(int id,string name,int dd,int mm,int yy)
{
this->id = id;
this->name =name;
this->dobj = date(dd,mm,yy);
}

void acceptdata()
{
cout<<"enter the name and id"<<endl;
cin>>name>>id;
dobj.acceptdob();
}

void displaydata()
{
cout<<"name of employee is "<<name<<" id is "<<id<<endl;
dobj.displaydob();
}
};

int main()
{
employee e1;
e1.acceptdata();
e1.displaydata();

employee e2;
e2.displaydata();
return 0;
}
Output-

Q9.
>>Source code-
#include<iostream>
using namespace std;

class employee
{
int empid;
string empname;
double basic_sal;
double hra;
static double medical;
double pf,net_sal,gross_sal;
static double pt;

public :
employee()
{
empid =00;
empname = "none";
basic_sal =00;
net_sal = 00;
gross_sal = 00;
}

employee(int id,string name, int bsal)


{
this->empid=id;
this->empname=name;
calculate(bsal);
}
void calculate(int bsal)
{
hra = 0.5*bsal;
pf = 0.12*bsal;
gross_sal = bsal+ hra + medical;
net_sal = gross_sal - (pf + pt);

}
void display()
{
cout<<"employee id = "<<empid<<endl<<" emp name = "<<empname<<endl;
cout<<"gross salary = "<<gross_sal<<" net salary = "<<net_sal<<endl;
}

};
double employee :: medical = 1000;
double employee ::pt = 200;

int main()
{
employee e1(101,"shri",25000);
e1.display();
return 0;
}
Output-

Q10.
>>Source code-
#include<iostream>
using namespace std;
class employee
{
int empid,dep_id;
string empname;
public : double basic_sal;

public :
employee()
{
empid =00;
dep_id = 00;
empname = "none";
basic_sal =00;
}
employee(int id,int did,string name,int bsal)
{
this->empid =id;
this->dep_id = did;
this->empname = name;
this->basic_sal =bsal;
}
void display()
{
cout<<"employee id = "<<empid<<endl;
cout<<"department id = "<<dep_id<<endl;
cout<<"employee name = "<<empname<<endl;
cout<<"employee basic salary = "<<basic_sal<<endl;
}

virtual void compute_net_salary()=0;

};

class mgrstate : public employee


{
public : double bonus;
double net_sal;
public :
mgrstate()
{
bonus =0;
net_sal =0;
}
mgrstate(int empid,int did,string name, double bsal,double bonus):employee(empid,did,name,bsal)
{
this->bonus=bonus;
// this->basic_sal=bsal
}
void compute_net_salary()
{
net_sal = basic_sal + bonus;
cout<<"net salary of employee = "<<net_sal;
}
void display()
{
cout<<"bonus of manager "<<bonus<<endl;
}

};

class workerstate :public employee


{
public : int hoursWorked,hourlyRate;
double net_sal;

public :
workerstate()
{
hourlyRate =0;
hoursWorked= 0;
net_sal =0;
}
workerstate(int empid,int did,string name, double bsal,int hw,int hrate):
employee(empid,did,name,bsal)
{
this->hoursWorked= hw;
this->hourlyRate = hrate;
}
int gethourlyRate()
{
return hourlyRate;
}

void compute_net_salary()
//void compute_net_salary()
{
net_sal = basic_sal + (hoursWorked * hourlyRate);
cout<<"net salary of employee = "<<net_sal;
}
void display()
{
cout<<"working hour of worker "<<hoursWorked<<endl;
cout<<"overtime rate = "<<hourlyRate<<endl;

}
};

int main()
{
workerstate w1(101,10,"shri",25000,10,120);
//workerstate w1;
w1.compute_net_salary();
mgrstate m1;
m1.compute_net_salary();

cout<<endl;
cout<<endl;
cout<<endl;
cout<<endl;
employee *arr[2];
mgrstate mag;
workerstate wok;

int index =0,i=0;


cout<<"add manager"<<endl;
cout<<"add worker"<<endl;
cout<<"show all employee"<<endl;

int flag=0;
while(flag!=1)
{
cout<<"enter your choice"<<endl;
int ch;
cin>>ch;
switch(ch)
{
case 1:
cout<<"enter salary and bonus"<<endl;
cin>>mag.basic_sal;
cin>>mag.bonus;
arr[index]= &mag;
index++;
break;

case 2:
cout<<"enter working hour and its rate"<<endl;
cin>>wok.hoursWorked;
cin>>wok.hourlyRate;
cin>>wok.basic_sal;
arr[index]= &wok;
index++;
break;

case 3:
cout<<"All employee salary is"<<endl;

for(int i=0;i<3;i++)
{
arr[i]->compute_net_salary();
}
break;

case 4:
flag=1;
break;
}
}
return 0;
}
Output-

Q11
>>Source code-
#include<iostream>
using namespace std;

class BankAccount
{
int acc_no;
string customer_name;
double balance;

public :
BankAccount()
{
cout<<"enter the bank details"<<endl;
cin>>acc_no>>customer_name>>balance;

this->acc_no= acc_no;
this->balance= balance;
this->customer_name= customer_name;

}
BankAccount(int acc_no,string customer_name,double balance)
{
this->acc_no = acc_no;
this->customer_name = customer_name;
this->balance = balance;
}

void withdraw(double amt)


{
balance = balance - amt;
}

void deposit(double amt)


{
balance = balance + amt;
}

void display()
{
cout<<"your balance is = "<<balance<<endl;
}
};

int main()
{
BankAccount b1;
b1.display();
b1.deposit(10000);
b1.withdraw(1000);
b1.display();
return 0;
}
Output-
Q12.
>>Source Code-
#include<iostream>
using namespace std;
class shape
{
public: virtual void area()=0;
};

class rectangle : public shape


{
int l,b;
public :
rectangle()
{
l=0;
b=0;
}
rectangle(int l,int b)
{
this->l=l;
this->b=b;
}
void area()
{
cout<<"area of rectangle "<<(l*b)<<endl;
}
};

class circle :public shape


{
float r;

public :
circle()
{
r=0;
}
circle(float r)
{
this->r=r;
}
void area()
{
cout<<"area of circle "<<(3.14*r*r)<<endl;
}

};

class square : public shape


{
int side;

public :
square()
{
side = 0;
}
square(int side)
{
this->side = side;
}
void area()
{
cout<<"area of circle "<<(side*side)<<endl;
}
};
int main()
{
rectangle r1(20,30);
r1.area();
rectangle r2;
r2.area();

circle c1(10);
c1.area();
circle c2;
c2.area();

square s1(10);
s1.area();
square s2;
s2.area();
return 0;
}
Output-

You might also like