0% found this document useful (0 votes)
49 views41 pages

Bonny Anney Computer Project

The document contains 20 questions related to C programming along with their solutions. Each question presents a programming problem and the corresponding code solution. The questions cover topics such as string manipulation, arrays, matrices, functions, structures, loops, conditional statements, and more.

Uploaded by

SHANKAR PRINTING
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
49 views41 pages

Bonny Anney Computer Project

The document contains 20 questions related to C programming along with their solutions. Each question presents a programming problem and the corresponding code solution. The questions cover topics such as string manipulation, arrays, matrices, functions, structures, loops, conditional statements, and more.

Uploaded by

SHANKAR PRINTING
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 41

Q.1. Write a Program to count the length of string.

Sol.
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
void main()
{
clrscr();
int i,s;
char a[30];
cout<<"\n enter string:";
gets(a);
s=strlen(a);
cout<<"\n length of string="<<s;
getch();
}
OUTPUT:
Q.2.Write a Program to print reverse of array.

Sol.
#include<iostream.h>
#include<conio.h>
void main()
{
textbackground(15);
textcolor(0);
clrscr();
int i,a[7],j,t;
cout<<"\n enter elements in array:";
for(i=0;i<7;i++)
cin>>a[i];
for(i=0;i<7;i++)
{
for(j=0;j<7/2;j++)
{
t=a[j];
a[j]=a[6-j];
a[6-j]=t;

}}
cout<<"\n reverse of array:";
for(i=0;i<7;i++)
cout<<a[i]<<"\t";
getch();
}
Output:
Q.3.Write a Program to print sum of array.

Sol.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int i,a[7],s=0;
cout<<"\n enter elements in array:";
for(i=0;i<7;i++)
cin>>a[i];
for(i=0;i<7;i++)
s+=a[i];
cout<<"\n sum of array:";
cout<<s;
getch();
}
OUTPUT:
Q.4.Write a Program to print sum of 2 matrices.

Sol.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int i,j,a[3][3],b[3][3],c[3][3];
cout<<"\n enter elements in matrix a:";
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
cin>>a[i][j];
}
cout<<"\n enter elements in matrix b:";
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
cin>>b[i][j];
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
c[i][j]=a[i][j]+b[i][j];
}
cout<<"\n sum matrix:";
for(i=0;i<3;i++)
{
cout<<"\n";
for(j=0;j<3;j++)
cout<<c[i][j]<<"\t";
}
getch();
}
OUTPUT:
Q.5.Write a Program to print sum of 2 diagonals a matrix.
Sol.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int i,j,a[3][3],s=0,t=0;
cout<<"\n enter elements in matrix a:";
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
cin>>a[i][j];
}
for(i=0;i<3;i++)
{
cout<<endl;
for(j=0;j<3;j++)
cout<<a[i][j]<<"\t";
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
if(i==j)
s+=a[i][j];
if(i+j==2)
t+=a[i][j];
}}
cout<<"\n sum of right diagonal="<<s;
cout<<"\n sum of left diagonal="<<t;
getch();
}
OUTPUT:
Q.6.Write a Program to print upper & lower halves of a matrix.

Sol.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int i,j,a[3][3];
cout<<"\n enter elements in matrix a:";
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
cin>>a[i][j];
}
for(i=0;i<3;i++)
{
cout<<endl;
for(j=0;j<3;j++)
cout<<a[i][j]<<"\t";
}
cout<<"\n lower half:;
for(i=0;i<3;i++)
{
cout<<endl;
for(j=0;j<=i;j++)
cout<<a[i][j]<<"\t";
}
cout<<"\n upper half:;
for(i=0;i<3;i++)
{
cout<<endl;
for(j=i;j<3;j++)
cout<<a[i][j]<<"\t";
}
getch();
}
OUTPUT:
Q.7.Write a program to count negative & positive no. between -
17 to +17.

Sol.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int n=0,p=0;
for(int i=-17;i<=17;i++)
{
cout<<i<<"\t";
if(i<0)
n++;
if(i>0)
p++;
}
cout<<"\n no. of positive integers:"<<p;
cout<<"\n no. of negative integers:"<<n;
getch();
}
OUTPUT:
Q.8.Write a program to find sum of even no. of natural series.

Sol.
#include<iostream.h>
#include<conio.h>
void main()
{
textbackground(15);
textcolor(0);
clrscr();
int s=0;
for(int i=1;i<=100;i++)
{
if(i%2==0)
{
cout<<i<<"\t";
s+=i;
}
}
cout<<"\n sum of even no. of natural series:"<<s;
getch();
}
OUTPUT:
Q.9.Write a program to find leap year with the help of function
named leap.

Sol.
#include<iostream.h>
#include<conio.h>
void leap();
void main()
{
clrscr();
leap();
getch();
}
void leap()
{
int a;
cout<<"\n enter a year";
cin>>a;
if(a%4==0)
cout<<"\n it is a leap year";
else
cout<<"\n not a leap year:";
getch();
}
OUTPUT:
Q.10.Calculate area of triangle with the help of function named
area.

Sol.
#include<iostream.h>
#include<conio.h>
void area();
void main()
{
clrscr();
area();
getch();
}
void area()
{
int b,h,a;
cout<<"\n enter base length:";
cin>>b;
cout<<"\n enter height of triangle:";
cin>>h;
a=0.5*b*h;
cout<<"\n area of triangle="<<a;
getch();
}
OUTPUT:
Q.11.Write a program to find print series of odd no.

Sol.
#include<iostream.h>
#include<conio.h>
void main()
{
textbackground(15);
textcolor(0);
clrscr();
for(int i=1;i<=100;i++)
if(i%2!=0)
cout<<i<<"\t";
getch();
}
OUTPUT:
Q.12. Write a program to print fibonanci series.

Sol.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a=0,b=1,c=0;
cout<<a;
for(int i=0;i<=10;i++)
{
a=b;
b=c;
c=a+b;
cout<<c<<"\t";
}
getch();
}
OUTPUT:
Q.13.Write a Program to fin sum of those elements in array
which are divisible by 5.

Sol.
#include<iostream.h>
#include<conio.h>
void main()
{
textbackground(15);
textcolor(0);
clrscr();
int i,a[7],t=0;
cout<<"\n enter elements in array:";
for(i=0;i<7;i++)
cin>>a[i];
for(i=0;i<7;i++)
if(a[i]%5==0)
t+=a[i];
cout<<"\n sum of elemnts div. by 5:"<<t;
getch();
}
OUTPUT:
Q.14.Write a Program to print sum of elements with odd
positions.

Sol.
#include<iostream.h>
#include<conio.h>
void main()
{
textbackground(15);
textcolor(0);
clrscr();
int i,j,a[3][3],s=0;
cout<<"\n enter elements in matrix a:";
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
cin>>a[i][j];
}
for(i=0;i<3;i++)
{
cout<<endl;
for(j=0;j<3;j++)
cout<<a[i][j]<<"\t";
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
if(i%2!=0||j%2!=0)
s+=a[i][j];
}
cout<<"\n sum of elements at odd position in matrix:"<<s;
getch();
}
OUTPUT:
Q.15.Write a program for calculator.

Sol.
#include<iostream.h>
#include<conio.h>
#include<process.h>
void main()
{
textbackground(15);
textcolor(0);
clrscr();
float a,b,c,ch;
cout<<"\n\t\t\t\t***Calculator***";
cout<<"\n\t\t\t\t1.Addition";
cout<<"\n\t\t\t\t2.Subtraction";
cout<<"\n\t\t\t\t3.Multiplicaion ";
cout<<"\n\t\t\t\t4.Division";
cout<<"\n\t\t\t\t5.Exit";
cout<<"\n\t\t\t\tEnter your Choice:" ;
cin>>ch;
if(ch==5)
exit(0);
else
cout<<"\n enter two numbers:";
cin>>a>>b;
if(ch==1)
{
c=a+b;
cout<<"\n sum="<<c;
}
else
if(ch==2)
{
c=a-b;
cout<<"\n difference="<<c;
}
else
if(ch==3)
{
c=a*b;
cout<<"\n product="<<c;
}
else
if(ch==4)
{
c=a/b;
cout<<"\n quotient="<<c;
}
else
cout<<"\n Wrong choice!!!!!";
getch();
}
OUTPUT:
Q.16.Write a program to calculate area of a circle with function
named area().
Sol.
#include<iostream.h>
#include<conio.h>
void area();
void main()
{
clrscr();
area();
getch();
}
void area()
{
int pi,r,a;
cout<<"\n enter radius of circle:";
cin>>r;
pi=3.14;
a=pi*r*r;
cout<<"\n area of circle="<<a;
getch();
}
OUTPUT:
Q.17.Write a program to print reverse of a string.
Sol.
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
void main()
{
clrscr();
int i,j;
char a[30],b;
cout<<"\n enter string:";
gets(a);
j=strlen(a)-1;
for(i=0;i<j;i++,j--)
{
b=a[i];
a[i]=a[j];
a[j]=b;
}
cout<<"\n reverse of string="<<a;
getch();
}
OUTPUT:
Q.18.Write a program to count no. of ‘the’ in the entered string.

Sol.
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
void main()
{
clrscr();
int i,j=0;
char a[30];
cout<<"\n enter string:";
gets(a);
for(i=0;a[i]!=0;++i)
{
if(a[i]=='t'&& a[i+1]=='h'&& a[i+2]=='e')
j++;
}
cout<<"\n no. of 'the' in string="<<j;
getch();
}
OUTPUT:
Q.19.Write a Program to search elements in a array.
Sol.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int i,a[7],b,t;
cout<<"\n enter elements in array:";
for(i=0;i<7;i++)
cin>>a[i];
cout<<"\n enter element to be searched:";
cin>>b;
for(i=0;i<7;i++)
{
if(b==a[i]);
t=1;
}
if(t==1)
cout<<"\n Found";
else
cout<<"\n Not found";
getch();
}
OUTPUT:
Q.20. Write a program to make structure of student with
following specifications Roll no., Name, marks of 5 subjects.

Sol.
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
struct student
{
int rn[3];
char nm[50][3];
float m[5][3];
};
void main()
{
textbackground(15);
textcolor(0);
clrscr();
int i,j;
student s;
for(i=0;i<3;i++)
{
cout<<"\n enter Roll no.:";
cin>>s.rn[i];
cout<<"\n enter Name:";
gets(s.nm[i]);
cout<<"\ enter marks of five subject:";
for(j=0;j<5;j++)
cin>>s.m[i][j];
}
for(i=0;i<3;i++)
{
cout<<"\n enter Roll no.:"<<s.rn[i];
cout<<"\n enter Name:"<<s.nm[i];
cout<<"\ enter marks of five subject:";
for(j=0;j<5;j++)
cout<<s.m[i][j]<<"\t";
}
getch();
}
OUTPUT:

You might also like