This document was ed by and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this report form. Report 3i3n4
22. Program which raise any number x to a positive power n. #include
OUTPUT Enter any number x: 5 Enter value of n: 4 x raised to the power n is: 625
23. Program to calculate commission for the salesmen the commission is calculated according to following rates. Sales
Commission rate
30001 onwards
15%
22000 – 30000
10%
12001 – 22000
7%
5001 – 12000
3%
0 – 5000
0%
#include
else cout<<"Commission = "<<sales*0.10; else cout<<"Commission = "<<sales*0.07; else cout<<"Commission = "<<sales*0.03; else cout<<"Commission = "<<sales*0; }
OUTPUT Enter sales made by salesman: 36548 Commission = 5482.2
24. Program to print whether the entered character is an uppercase or a lowercase character or a digit or any other character. The ASCII codes are given below. Characters
ASCII Range
„0‟ – „9‟
48 – 57
„A‟ – „Z‟
65 – 90
„a‟ – „z‟
97 - 122
#include
else cout<<"\nThe entered character is a upper case alphabet"; else cout<<"\nThe entered character is a digit"; else cout<<"\nThe entered character is any other character"; }
OUTPUT Enter any Character: j The entered character is a lower case alphabet
25. Program to print table of any number.
#include
OUTPUT Enter any number: 2 2
4
6
8
10
12
14
16
18
20
26. Program to print roots of a quadratic equation.
#include
OUTPUT Enter the values of a, b & c of the quadratic equation of the form ax2+bx+c 1 -5 4 Roots are real and unequal Root1= 1 Root2= 4
27. Program to find a number is prime or not.
#include
OUTPUT Enter any number: 17 Entered number is a PRIME NUMBER
28. Program that prints the following series 1
2
4
8
16
32 64 128
#include
for(i=0;i<=7;++i) { j=pow(2,i); cout<<j<<' '; } }
OUTPUT 1
2
4
8
16
32
64
128
29. Program that prints first n natural numbers and prints their sum.
#include
OUTPUT Enter value of n: 20 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 Sum = 210
30. Program to print sum of even and odd natural numbers in first n natural numbers.
#include
OUTPUT Enter the number of numbers: 10 Sum of even numbers: 30 Sum of odd numbers: 25
31. Program for generating the given output.
#include
OUTPUT * ** *** **** *****
32. Program for generating the given output.
#include
OUTPUT * *** ***** *******
33. Program for generating the given output.
#include
OUTPUT 1 22 333 4444 55555
34. Program for generating the given output.
#include
OUTPUT Enter number of lines: 3 * ** ***
35. Program to print factorial of a number.
#include
OUTPUT Enter any number: 5
5 factorial = 120
36. Program for generating the given output.
#include
OUTPUT Enter number of lines: 4 A AB ABC ABCD
37. Program for generating the given output. #include
void main() { int i, j, k, n; cout<<"Enter number of lines: "; cin>>n; for(i=1;i<=n;i++) { cout<<"\n"; for(j=1;j<=i-1;j++) cout<<„ „; for(k=n;k>=i;k--) cout<<"& "; } }
OUTPUT Enter number of lines: 4 &&&& &&& && &
38. Program to check whether a number is palindrome or not. #include
#include<string.h> void main() { int n, n1, n2=n, rev=0; cout<<"Enter any number: "; cin>>n; while(n) {n1=n%10; rev=rev*10+n1; n=n/10; } if(n2==rev) cout<<"\nNumber is palindrome"; else cout<<"\nNumber is not palindrome"; }
OUTPUT Enter any number: 121 Number is palindrome
39. Program to check whether entered character is an alphabet or not.
#include
OUTPUT Enter character: a It is an alphabet.
Enter character: % It is not an alphabet.
40. Program to check whether the entered character is of uppercase or lowercase. #include
#include
char ch; cout<<"Enter character:"; cin>>ch; if(isalpha(ch)) {
cout<<"It is an alphabet.\n"; if(isupper(ch)) cout<<"It is an upper case."; else cout<<"It is a lower case.";
} else cout<<"It is not an alphabet."; }
OUTPUT Enter character: r It is an alphabet. It is a lower case.
41. Program to concatenate two strings. #include
#include<string.h> void main() { char s1[10], s2[10]; cout<<"Enter string 1:"; gets(s1); cout<<"Enter string 2:"; gets(s2); cout<<strcat(s1,s2);
}
OUTPUT Enter string 1: GOOD Enter string 2: LUCK GOODLUCK Enter string 1: WEL Enter string 2: COME WELCOME
42. Program to find out number of words present in a line. #include
void main() { int n=1; char str[100]; cout<<"Enter line:"; cin.getline(str,100); for(int i=1; str[i]!='\0'; i++) { if ( str[i-1]!=' ' && str[i]==' ') n=n+1; } cout<< “\nNumber of words = ”<
OUTPUT Enter line: My name is Alok Kumar. Number of words = 5 Enter line: Program to find out number of words present in a line. Number of words = 11
43. Program to print a word by deleting 1 letter from end in each turn. #include
{
char str[25]; cout<<"Enter string:"; cin.getline(str,25); int len=strlen(str); for(int i=len; i<=0; i--) {
for(int j=0; j<=i; j++) cout<<str[j]; cout<<“\n”;
} }
OUTPUT Enter string: HELLO HELLO HELL HEL HE H
44. Program to check whether the given string is palindrome or not. #include
char str[20];
int len, i, f=1; cout<<"Enter string:"; cin.getline(str,20); len=strlen(str); for(i=0, len=len-1; i<=len/2; i++, len--) {
if(str[i]!=str[len]) f=0; break;
} if(f==1) cout<<"Entered string is palindrome."; else cout<<"Entered string is not a palindrome."; }
OUTPUT Enter string: RADAR Entered string is palindrome.
45. Program to find whether a given character is present in a string or not and also find the position of character. #include
void main() {
clrscr(); char str[20], ch; int i, f=0; cout<<"Enter string:"; cin.getline(str,20); cout<<"Enter character:"; cin>>ch; for(i=1; i<=len; i++) { if(str[i]==ch) { f=1; break; } } if(f==1) cout<<“The given character is present in the string.”<<“\n The position of the character is:”<< i; else cout<<“The given character is not present in the string.”;
}
OUTPUT Enter string: My name is Khan and I am not a terrorist. Enter character: K The given character is present in the string The position of the character is: 12
Enter character: t The given character is present in the string The position of the character is: 32
46. Program to find cube of a number (using function). #include
cin>>a; c=cube(a); cout<<"Cube of the number: "<
OUTPUT Enter number: 5 Cube of the number: 125
47. Program to print largest even and odd number from a list of numbers entered through keyboard. The list terminates as soon as one enters zero (using function). #include
clrscr(); int n; even_odd(n); } void even_odd(int n) { int maxeven=0, maxodd=0; while(n) { cout<<"Enter number:"; cin>>n; if(n%2==0) { if(n>maxeven) maxeven=n; } else if(n%2==1) { if(n>maxodd) maxodd=n; } else if(n==0) break;
} cout<<"Largest odd number:"<<maxodd<<"\n Largest even number:"<<maxeven; }
OUTPUT Enter number:5 6 8 9 7 15 18
Largest odd number: 15 Largest even number: 18
48. Program to calculate factorial of a number (using function). #include
int n; cout<<"Enter number:"; cin>>n; factorial(n);
}
void factorial(int n) {
int fact=1; int i; for(i=1; i<=n; i++) fact=fact*i; cout<< “\nFactorial of ”<
}
OUTPUT Enter number: 6 Factorial of 6 is 720
Enter number: 5 Factorial of 6 is 120
49. Program to find largest number of a list of no. entered through keyboard (using function). #include
largest();
} void largest() {
char ans='y'; int n, large=0; while(ans=='y')
{
cout<<"Enter number:"; cin>>n; if(n>large) large=n; cout<<"You want to enter number?(y or n):"; cin>>ans;
} cout<< “\n Largest number entered: ”<
OUTPUT Enter number: 5 6 8 45 68 26 65 35 79 65 -6 64 25 64 3 9 10 Largest number entered: 79
50. Program that inputs three numbers and find the greatest among them (using functions). #include
int max, d, e, f; cout<<"Enter 3 number:"; cin>>d>>e>>f; greatest(d, e, f);
} void greatest(int d, int e, int f)
{
if(d>e && d>f) cout<<"First number is greatest"; else if(e>d && e>f) cout<<"Second number is greatest"; else if(f>d && f>e) cout<<"Third number is greatest";
}
OUTPUT Enter 3 number: 5 9 2 Second number is greatest
51. Program to find largest and smallest element in a vector. #include
int v[10], large, small, i; cout<<"Enter the value in vector "; for(i=0;i<10; i++) cin>>v[i]; large=v[1]; small=v[1] ; for (i=0;i<10;i++)
{
if(v[i]>large) large=v[i]; else if(v[i]<small) small=v[i];
} cout<<"\n Largest element ="<
} void swap(int c, int d)
{
int temp; temp =c; c=d; d=temp; cout<< “\nValues after swapping numbers.” cout<< “\nNum1”<
}
OUTPUT Enter num1: 9 Enter num2: 15 Value after swapping numbers. Num1: 15 Num2: 9
53. Program to find sum of digits of a number (using function). #include
int n,s; cout<<"Enter number:"; cin>>n; s=sum(n); cout<< “\n Sum of digits :”<<s;
} int sum(int n1) {
int p=0; while(n1) {
p=p+(n1%10); n1=n1/10;
} return p; }
OUTPUT Enter number:356 Sum of digits : 14
54. Program to print all those elements of a matrix that are on and are on the right side of main diagonal. #include
cout<<"\n"; for(int j=0;j<3;j++)
{
if(i<=j) cout<<arr[i][j]; else cout<<" ";
}} }
OUTPUT Enter matrix:
2
6
9
1
5
2
6
9
8
1
5
4
2
3
3
55. Program to print and convert all the elements of an array positive (using function). #include
int size=10, arr[size]; cout<<"Enter elements of an array:"; for(int i=0; i<size; i++) cin>>arr[i]; negative(arr,size);
}
void negative(int arr1[], int size1) {
for(int i=0; i<size1; i++) {
if(arr1[i]<0) arr1[i]=arr1[i]*-1;
} for(i=0;i<size1; i++) cout<<arr1[i]<<“\t”; }
OUTPUT Enter elements of an array: 1
2
-6
-8
5
-9
5
4
8
-9
1
2
6
8
5
9
5
4
8
9
56. Program to generate the given output. #include
int arr1[5][5]; int arr[]={1,2,3,4,5}; for(int i=0;i<5;i++) for(int j=0;j<5;j++) arr1[i][j]=arr[j]; for(i=0; i<5; i++) for(int j=0; j<5; j++)
{
if(i+j>4) arr1[i][j]=0; }
for(i=0;i<5;i++) {
cout<<"\n"; for(int j=0;j<5;j++) cout<<arr1[i][j]<<" ";
} } OUTPUT 12345 12340 12300 12000 10000
57. Program to display a 2D array in 1D array. #include
int A[3][3], B[9]; int i,j,k=0; cout<<"Enter Array A:"; for(i=0; i<3; i++) for(j=0; j<3; j++) cin>>A[i][j]; for(i=0; i<3; i++) {
cout<<"\n";
for(j=0; j<4; j++) cout<
<<" ";} for(i=0; i<3; i++) for(j=0; j<3; j++) {
B[k]=A[i][j]; k++;}
for(k=0; k<9; k++) cout<
OUTPUT Enter Array A:
123 123 123
Array B:
123123123
58. Program to concatenate two arrays. #include
int i, j, k; int A[5], B[5], C[10]; cout<<"Enter array A:"; for(i=0; i<5; i++) cin>>A[i]; cout<<"Enter array B:"; for(j=0;j<5;j++)
cin>>B[j]; for(i=0,k=0; i<5;k++, i++) C[k]=A[i]; for(k=5, i=0; i<5; k++, i++) C[k]=B[i]; cout<<"\nArray C:"; for(k=0;k<10;k++) cout<
OUTPUT Enter array A:
01234
Enter array B:
56789
Array C:
0123456789
59. Program to replace a number from array with 0 and take all 0s of the array to the left. #include
cout<<"Enter number:"; cin>>n; for(i=0; i<9; i++) { if(arr[i]==n) arr[i]=0; } for(i=0; i<9; i++) cout<<arr[i]<<"\n"; cout<<"\n"; for(k=0;k<2; k++) { for(i=8; i>=0; i--) { if(arr[i]==0) { for(int j=i;j>0;j--) { t=arr[j]; arr[j]=arr[j-1]; arr[j-1]=t; } }
} } cout<< “Array after replacing ”<
OUTPUT Enter array:
6
5
4
6
9
5
1
5
8
0
6
4
6
9
1
8
Enter number: 5 Array after replacing „5‟: 0
0
60. Program to calculate the sum of elements of the rows of matrix. #include
int A[3][3], sum; cout<<"Enter matrix A:"; for(int i=0; i<3; i++) {
for(int j=0; j<3; j++) cin>>A[i][j];
}
for(i=0; i<3; i++) {
sum=0; for(int j=0; j<3; j++) sum=sum+A[i][j]; cout<< “\nSum of row”<
<< “ is ” <<sum<<"\n";
} }
OUTPUT Enter matrix A:
1
2
3
4
5
6
7
8
9
Sum of row 1 is 6 Sum of row 2 is 15 Sum of row 3 is 24
61. Program to multiply two matrices. #include
for(int j=0; j<3; j++) cin>>A[i][j]; } cout<<"Enter matrix B:"; for(i=0; i<3; i++) { for(int j=0; j<3; j++) cin>>B[i][j]; } cout<< “\nMatrix A × matrix B :” for(i=0; i<3; i++) { cout<<"\n"; for(int j=0; j<3; j++) { int sum=0; for(int k=0; k<3; k++) { sum+=A[i][j]*B[k][j]; } cout<<sum<<" "; } }
}
OUTPUT Enter matrix A: 1
2
1
2
1
2
1
2
1
Enter matrix B: 2
1
2
1
2
1
2
1
2
5
8
5
10
4
10
5
8
5
Matrix A × matrix B:
62. Program to delete duplicate elements of an array with 0 and take all 0 to right. #include
int A[10]; cout<<"Enter array:"; for(int i=0; i<10; i++) cin>>A[i]; for(i=0; A[i]!='\0'; i++)
{
for(int j=i+1; A[j]!='\0'; j++) {
if(A[i]==A[j]) {
for(int k=j; A[k]!='\0'; k++) A[k]=A[k+1];
}
}} for(i=0; A[i]!='\0'; i++) {
if(A[i]==0) A[i]='\0';
}
cout<< “\nNew array:”; for(i=0; i<10; i++) cout<
<<" "; }
OUTPUT Enter array: 5
8
9
6
4
8
9
3
6
5
New array: 5
8
9
6
4
3
0
0
0
0
63. Program to calculate compound interest for 50 clients of an investment company. Details (including costume name, code and date of starting, number of years, interest rate and total amount) are stored in an array of structures. #include
{ char cname[20]; int ccode; int cday; int cmonth; int cyear; int irate; int totalamount; int tot_years; }; clients c[50]; void main() { clrscr(); float ci[50]; for(int i=0; i<50; i++) { gets(c[i].cname); cin>>c[i].ccode; cin>>c[i].cday; cin>>c[i].cmonth; cin>>c[i].cyear; cin>>c[i].tot_years;
cin>>c[i].irate; cin>>c[i].totalamount; } for (i=0; i<50; i++) { float t,s; t[i]=(1+c[i].irate*0,01); s[i]=pow(t[i],c1[i].year; ci[i]=c1[i].totalamount*s[i]; cout<
}
64. Program to store information of 10 employee and to display information of an employee depending upon the employee number given. #include
int eno; char ename[20]; char eaddress[20]; }e[10]; void main() { clrscr(); int i,n; char ans='y'; for(i=0;i<10;i++) { cin>>e[i].eno; gets(e[i].ename); gets(e[i].eaddress); do{ cout<<"enter employee no"; cin>>n; if(n==e[i].eno) { cout<<e[i].eno; puts(e[i].ename); puts(e[i].eaddress); }
cout<<"u want to proceed or not"; cin>>ans; } while(ans=='y'); } }
65. Program to create an array containing details of 25 students (including Roll number, name, marks in three subjects) and print out a list of students who have failed in more than 1 subject. Assume 40% marks as marks. #include
int sub1; int sub2; int sub3; }s[25]; void main() { for(int i=0;i<25;i++) { cout<<"Enter student Roll number : "; cin>>s[i].rno; cout<<"Enter student name : "; gets(s[i].name); cout<<"Enter marks obtained in three subjects : "; cin>>s[i].sub1>>s[i].sub2>>s[i].sub3; } cout<<"list of students failed in more than 1 subject"; int f=0; for(i=0;i<25;i++) { if(s[i].sub1<=40) f++; if(s[i].sub2<=40) f++;
if(s[i].sub3<=40) f++;
if(f>1) { puts(s[i].name) cout<<s[i].rno; } f=0; } }
More Documents from "Armaan Chauhan" v1m3u