You are on page 1of 36

ITP file

NAME:- Gaurav kumar


BATCH :-B2
ROLL NO.:- B48

Q1. Write a program to print your Name, Date


of Birth and Group.

#include<stdio.h>
#include<conio.h>
int main()
{
char Name[20],Bgroup[10];
int DOB[10];
printf("Enter the name\n");
scanf("\n%s",&Name);
printf("Enter the Date of Birth\n");
scanf("\n%s",&DOB);
printf("Enter the Blood Group\n");
scanf("\n%s",&Bgroup);
printf("The name is %s\n The Date of Birth is %s\n The Blood Group is %s\
n",Name,DOB,Bgroup);
return 0;
}
Q2. Write a program to display the ASCII
equivalent of a character.
#include<stdio.h>
#include<conio.h>
int main()
{
char ch;
int DOB[10];
printf("Enter the character\n");
scanf("\n%c",&ch);
printf("The ASCII value of the character is %d\n",ch);
return 0;
}
Q3. Write a program to give sum of two
numbers.
#include<stdio.h>
#include<conio.h>
int main()
{ int a,b;
printf("Enter the first number\n");
scanf("\n%d",&a);
printf("Enter the second number\n");
scanf("\n%d",&b);
int sum=a+b;
printf("\nThe sum of the two numbers is : %d",sum);
return 0; }

Q4. Write a program evaluate algebraic


expression after reading necessary values
from the user:
c = 2*a + (b/a) - ((a/2.5) * b * ((a/3) - b))
#include<stdio.h>
#include<conio.h>
int main()
{ float a,b;
printf("Enter the value of a\n");
scanf("\n%f",&a);
printf("Enter the value of b\n");
scanf("\n%f",&b);
float c=2*a + (b/a) - ((a/2.5) * b * ((a/3) - b));
printf("\nThe value of the expression is: %f",c);
return 0; }

Q5. Write a program to find sum of a


geometric series.
#include<stdio.h>
#include<math.h>
int main()
{ float a,r,i,last_term,sum=0;
int n;
printf("Enter the first term of GP\n");
scanf("\n%f",&a);
printf("Enter the common ratio of GP\n");
scanf("\n%f",&r);
printf("Enter the number of terms of GP\n");
scanf("\n%d",&n);
sum = (a*(1-pow(r,n)))/(1-r);
last_term = a*pow(r,n-1);
printf("The sum of the GP is :%f\n The last term of the GP is :
%f",sum,last_term);
return 0; }

Q
6
.

Write a program to find the factorial number


using for Loop.
#include<stdio.h>
#include<conio.h>
int main()
{ int num;
printf("Enter the number\n");
scanf("\n%d",&num);
int fact=1;
for(int i=1;i<=num;i++)
{ fact*=i; }
printf("The Factorial of the entered number is :%d\n",fact);
return 0; }

Q7. Write a program to calculate the sum of


two numbers using function without
argument and without return value.
#include<stdio.h>
#include<conio.h>
int sum()
{ int a,b,c;
printf("Enter the first number\n");
scanf("\n%d",&a);
printf("Enter the second number\n");
scanf("\n%d",&b);
c=a+b;
return c; }
int main()
{ printf("\nThe sum of the two numbers is :%d",sum());
return 0; }

Q8. Write a program to calculate the area of


square using function without argument and
with return value.
#include<stdio.h>
#include<conio.h>
int area()
{ int a,c;
printf("Enter the side of the square\n");
scanf("\n%d",&a);
c=a*a;
return c; }
int main()
{ printf("\nThe area of the square is :%d",area());
return 0; }

Q9.
Write a program to calculate the average of
five numbers using function with argument
and without return value.
#include<stdio.h>
#include<conio.h>
float avg(int a,int b,int c,int d,int e)
{ float average=0;
average=(a+b+c+d+e)/5;
return average; }
int main()
{ float a,b,c,d,e;
printf("Enter the First number\n");
scanf("\n%f",&a);
printf("Enter the Second number\n");
scanf("\n%f",&b);
printf("Enter the Third number\n");
scanf("\n%f",&c);
printf("Enter the Fourth number\n");
scanf("\n%f",&d);
printf("Enter the Fifth number\n");
scanf("\n%f",&e);
printf("\nThe average of five numbers is :%f",avg(a,b,c,d,e));
return 0; }

Q10. Write a program to calculate the area of


rectangle by creating a function with
argument and with return value.
#include<stdio.h>
#include<conio.h>
int area(int l,int b)
{ int ar=l*b;
return ar;
}
int main()
int l,b;
printf("Enter the length of the rectangle\n");
scanf("\n%d",&l);
printf("Enter the breadth of the rectangle\n");
scanf("\n%d",&b);
printf("\nThe area of the rectangle is :%d",area(l,b));
return 0; }

Q11. Write a recursive program to print the


first m Fibonacci number.
#include <stdio.h>
int fibo(int n)
{ if(n==1 || n==2) {
return 1; }
else {
return fibo(n-1)+fibo(n-2);
}
}
int main()
{
int n,f;
printf("Enter the number\n");
scanf("%d",&n);
printf("Series = ");
for(int i=1;i<=n;i++)
{ f=fibo(i);
printf("%d ",f);
}
}

Q12. Write a program to print the sum of


natural numbers using recursion.
#include<stdio.h>

int sum(int n);


int main()
{ int n;
printf("Enter the value of n till the sum is to be found\n");
scanf("%d",&n);
int s=sum(n);
printf("Sum of natural numbers is %d",s);
return 0;
}
int sum(int n)
{ if(n==0)
{ return 0;
}
int s=sum(n-1);
return n+s;
}

Q13. Write a program to print the factorial of


a number using recursion.
#include<stdio.h>
int fact(int n)
{ if(n==1)
{ return 1;
}
else {
return n*fact(n-1);
}
}

int main()
{ int n;
printf("Enter the number\n");
scanf("%d",&n);
printf("Factorial is :%d",fact(n));
return 0;
}

Q14. Write a recursive program for tower of


hanoi.
#include<stdio.h>

void toh(int n,int t1id,int t2id,int t3id);


int main()
{ int n,t1d,t2d,t3d;
printf("Enter the highest and discs of tower\n");
scanf("%d",&n);
scanf("%d",&t1d);
scanf("%d",&t2d);
scanf("%d",&t3d);
toh(n,t1d,t2d,t3d);
return 0;
}
void toh(int n,int t1id,int t2id,int t3id)
{ if(n==0)
{ return;
}
toh(n-1,t1id,t2id,t3id);
printf("%d -> %d \n",t1id,t2id);
toh(n-1,t3id,t2id,t1id);
}
Q15. Write a program to print the following
pattern
*
**
***
****
*****

#include<stdio.h>
#include<conio.h>
int main()
{ int n;
printf("Enter the height\n");
scanf("%d\n",&n);
for(int i=1;i<=n;i++)
{ for(int j=1;j<=i;j++)
{ printf(" * ");
}
printf("\n");}
return 0;
}
Q16. Write a program to store the values
entered by the user in an array and then
displays the position of those elements in an
array.
#include<stdio.h>
int main()
{ int n;
printf("Enter the length of the arrray\n");
scanf("%d",&n);
int arr[n];
printf("Enter the elements of the array\n");
for(int i=0;i<n;i++)
{ scanf("%d\n",&arr[i]);
} for(int i=0;i<n;i++)
{ printf("Position of element %d is:%d\n",arr[i],i);
}
return 0; }
Q17. Write a Program to find the average of
elements entered by the user in an array.
#include<stdio.h>
int main()
{ int n;
printf("Enter the length of the arrray\n");
scanf("%d",&n);
int arr[n];
printf("Enter the elements of the array\n");
for(int i=0;i<n;i++)
{ scanf("%d\n",&arr[i]);
}
int sum=0;
for(int i=0;i<n;i++)
{ sum+=arr[i]; }
float avg=sum/n;
printf("The average of the elements of the array entyered by the user in an
array is :%f",avg);
return 0;
}
Q18. Write a menu driven program for
matrices to do the following operation
depending on whether the operation requires
one or two matrices
1. Addition of two matrices

2. Subtraction of two matrices

3.Finding upper and lower triangular matrices

4. Transpose of a matrix

5. Product of two matrices


#include <stdio.h>

int main()
{
printf("Choose your operation:\n1. Press 1 for Addition of two matrices\n2.
Press 2 for Subtraction of two matrices\n3. Press 3 for Finding upper and lower
triangular matrices\n4. Press 4 for Transpose of a matrix\n5. Press 5 for
Product of two matrices");
int ch;
scanf("%d",&ch);
switch(ch){
case 1: add();
break;
case 2: subtract();
break;
case 3: triangular();
break;
case 4: transpose();
break;
case 5: product();
break;
default: printf("Invalid choice");
}
return 0;
}
void add(){
int n1,m1,n2,m2;
printf("Enter the rows and columns of first matrix:\n");
scanf("%d",&n1);
scanf("%d",&m1);
int arr1[n1][m1];
printf("Enter the elements of first matrix:\n");
for(int i=0;i<n1;i++){
for(int j=0;j<m1;j++){
scanf("%d",&arr1[n1][m1]);
}
}
printf("Enter the rows and columns of second matrix:\n");
scanf("%d",&n2);
scanf("%d",&m2);
int arr2[n2][m2];
printf("Enter the elements of second matrix:\n");
for(int i=0;i<n2;i++){
for(int j=0;j<m2;j++){
scanf("%d",&arr2[n2][m2]);
}
}
if(n1!=n2 || m1!=m2){
printf("Enter the same dimensions of both matrices");
return;
}
else{
int ad[n1][m1];
for(int i=0;i<n1;i++){
for(int j=0;j<m1;j++){
ad[i][j]=arr1[i][j]+arr2[i][j];
}
}
for(int i=0;i<n1;i++){
for(int j=0;j<m1;j++){
printf("%d",ad[i][j]);
}
printf("\n");
}
}
}
void subtract(){
int n1,m1,n2,m2;
printf("Enter the rows and columns of first matrix:\n");
scanf("%d",&n1);
scanf("%d",&m1);
int arr1[n1][m1];
printf("Enter the elements of first matrix:\n");
for(int i=0;i<n1;i++){
for(int j=0;j<m1;j++){
scanf("%d",&arr1[n1][m1]);
}
}
printf("Enter the rows and columns of second matrix:\n");
scanf("%d",&n2);
scanf("%d",&m2);
int arr2[n2][m2];
printf("Enter the elements of second matrix:\n");
for(int i=0;i<n2;i++){
for(int j=0;j<m2;j++){
scanf("%d",&arr2[n2][m2]);
}
}
if(n1!=n2 || m1!=m2){
printf("Enter the same dimensions of both matrices");
return;
}
else{
int res[n1][m1];
for(int i=0;i<n1;i++){
for(int j=0;j<m1;j++){
res[i][j]=arr1[i][j]-arr2[i][j];
}
}
for(int i=0;i<n1;i++){
for(int j=0;j<m1;j++){
printf("%d",res[i][j]);
}
printf("\n");
}
}
return;
}
void triangular(){
int n,m,cnt1=0,cnt2=0;
printf("Enter the rows and columns of matrix:\n");
scanf("%d %d",&n,&m);
int arr[n][m];
printf("Enter the elments:\n");
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
scanf("%d",&arr[i][j]);
}
}
if(n!=m){
printf("Upper or lower triangular matrix not possible");
return;
}
else{
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
if(i>j){
if(arr[i][j]==0){
cnt1=1;
}
else{
cnt1=0;
}
}
else if(j>i){
if(arr[i][j]==0){
cnt2=1;
}
else{
cnt2=0;
}
}
}
}
if(cnt1==1){
printf("Upper triangular matrix");
}
if(cnt2==1){
printf("Lower triangular matrix");
}
if(cnt1==0 && cnt2==0){
printf("Neither upper nor lower triangular matrix");
}
}
}
void transpose(){
int n,m;
printf("Enter the number of rows and columns:\n");
scanf("%d %d",&n,&m);
int arr[n][m];
printf("Enter the elements of matrix:\n");
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
scanf("%d",&arr[i][j]);
}
}
for(int i=0;i<n;i++){
for(int j=i;j<m;j++){
int temp=arr[i][j];
arr[i][j]=arr[j][i];
arr[j][i]=temp;
}
}
printf("Transpose of matrix is:\n");
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
printf("%d",arr[i][j]);
}
printf("\n");
}
}
void product(){
int n1,m1,n2,m2;
printf("Enter the rows and columns of first matrix:\n");
scanf("%d",&n1);
scanf("%d",&m2);
int arr1[n1][m1];
printf("Enter the elements of first matrix:\n");
for(int i=0;i<n1;i++){
for(int j=0;j<m1;j++){
scanf("%d",&arr1[n1][m1]);
}
}
printf("Enter the rows and columns of second matrix:\n");
scanf("%d",&n2);
scanf("%d",&m2);
int arr2[n2][m2];
printf("Enter the elements of second matrix:\n");
for(int i=0;i<n2;i++){
for(int j=0;j<m2;j++){
scanf("%d",&arr2[n2][m2]);
}
}
int res[n1][m2];
printf("Result matrix is \n");
for (int i = 0; i < n1; i++) {
for (int j = 0; j < m2; j++){
for(int k=0;k<m1;k++){
res[i][j]+=arr1[i][k]+arr2[k][j];
}
}
}
for(int i=0;i<n1;i++){
for(int j=0;j<m2;j++){
printf("%d",res[i][j]);
}
printf("\n");
}
}
Q19. Write a program to find whether the
number entered by user is even or odd.
#include<stdio.h>

int main()

{ int n;

printf("Enter the number you want to check odd or even\n");

scanf("%d",&n);

if(n%2==0)

{ printf("The given number is even\n");


}

else{ printf("The given number is odd\n"); }

return 0;

Q20. Write a program to find whether a


three-digit number entered by user is a
palindrome or not.
#include<stdio.h>

int main()

{ int n,r,rev=0;

printf("Enter any three digit number\n");

scanf("%d",&n);

int temp=n;

while(temp>0)
{ r=temp%10;

rev=rev*10+r;

temp=temp/10;

if(n==rev)

{ printf("The given number is palindrome\n");

else{ printf("The given number is not a palindrome\n");}

return 0;

Q21. Write a program to find whether the


number entered by user is even or odd.
#include<stdio.h>

int main()
{ int n;

printf("Enter the number you want to check odd or even\n");

scanf("%d",&n);

if(n%2==0)

{ printf("The given number is even\n");

else{ printf("The given number is odd\n"); }

return 0;

Q22. Write a program to find whether


number entered by user is a palindrome or
not.
#include<stdio.h>

int main()
{ int n,r,rev=0;

printf("Enter any number\n");

scanf("%d",&n);

int temp=n;

while(temp>0)

{ r=temp%10;

rev=rev*10+r;

temp=temp/10;

if(n==rev)

{ printf("The given number is palindrome\n");

else { printf("The given number is not a palindrome\n");}

return 0;

}
Q23. Write a program to swap two numbers
using pointers.
#include<stdio.h>

int main()

{ int x,y,*a,*b,temp;

printf("Enter the value of x and y\n");

scanf("%d%d\n",&x,&y);

printf("Before Swapping the values, x= %d and y= %d\n",x,y);

a=&x;

b=&y;

temp=*b;

*b = *a;

*a = temp;

printf("After Swapping the values, x= %d and y= %d\n",x,y);

return 0;

}
Q24. Write a program to find whether the
character entered by user is a vowel using

switch statement.
/*Write a program to find whether the character entered by user is a vowel using switch
statement.*/

#include<stdio.h>

int main()

{ char ch;

printf("Enter any character\n");

scanf("%c\n",&ch);

switch(ch)

{ case 'a': printf("Entered character is a vowel\n");

break;

case 'e': printf("Entered character is a vowel\n");

break;

case 'i': printf("Entered character is a vowel\n");

break;

case 'o': printf("Entered character is a vowel\n");

break;

case 'u': printf("Entered character is a vowel\n");


break;

case 'A': printf("Entered character is a vowel\n");

break;

case 'E': printf("Entered character is a vowel\n");

break;

case 'I': printf("Entered character is a vowel\n");

break;

case 'O': printf("Entered character is a vowel\n");

break;

case 'U': printf("Entered character is a vowel\n");

break;

default : printf("Entered character is not a vowel\n");

break; }

return 0;

You might also like