You are on page 1of 18

* Write a program to display Hello on the screen: #include<iostream.h> #include<conio.

h> void main() { cout<<Hello; getch(); } Output: Hello

* Write a program to accept two numbers and display the bigger number: #include<iostream.h> #include<conio.h> void main() { int a; int b; cout<<Enter first number; cin>>a; cout<<Enter second number; cin>>b; if (a>b) cout<<First number is greater; else cout<<Second Number is greater; getch(); } Output: Enter first number 5 Enter second number 10 Second Number is greater

*Write a program to accept three numbers and display their average and sum: #include<iostream.h> #include<conio.h> void main() { int a; int avg; int sum; int b; int c; cout<<Enter first number; cin>>a; cout<<Enter second number; cin>>b; cout<<Enter third number; cin>>c; sum=a+b+c; avg=sum/3; cout<<Sum is <<sum; cout<<Average is <<avg; getch(); } Output: Enter first number1 Enter second number2 Enter third number3 Sum is 6 Average is 2

*Write a program to accept a number and display whether it is even or odd: #include<iostream.h> #include<conio.h> void main() { int a; cout<<Enter a number; cin>>a; if (a%2==0) cout<<Even number; else cout<<Odd number; getch(); } Output: Enter a number5 Odd number

*Write a program to accept three numbers and display the largest: #include<iostream.h> #include<conio.h> void main() { int a; int avg; int sum; int b; int c; cout<<Enter first number; cin>>a; cout<<Enter second number; cin>>b; cout<<Enter third number; cin>>c; if (a>b&&b>c) cout<<first number is largest; elseif (a>c&&b>a) cout<<second number is largest; elseif (b>a&&c>b) cout<<third number is largest; getch(); } Output: Enter first number15 Enter second number20 Enter third number3 second number is largest

*Write a program to display table of ten: #include<iostream.h> #include<conio.h> void main() { int i; int a; cout<<Enter number; cin>>a; for (i=1;i<11,i++) { cout<<a<< X <<i<< = <<a*i; } getch(); } Output: Enter number2 2X1=2 2X2=4 2X3=6 2X4=8 2 X 5 = 10 2 X 6 = 12 2 X 7 = 14 2 X 8 = 16 2 X 9 = 18 2 X 10 = 20

*Write a program to accept a number and display its table: #include<iostream.h> #include<conio.h> void main() { int i; for (i=1;i<11,i++) { cout<<10<< X <<i<< = <<10*i; } getch(); } Output: 10 X 1 = 10 10 X 2 = 20 10 X 3 = 30 10 X 4 = 40 10 X 5 = 50 10 X 6 = 60 10 X 7 = 70 10 X 8 = 80 10 X 9 = 90 10 X 10 = 100

*Write a program to display the factorial of 5: #include<iostream.h> #include<conio.h> void main() { int a; for (int i=1;i<=5;i++) { a *=i; } cout<<a; getch(); } Output: 120

* Write a program to accept a number and display its factorial: #include<iostream.h> #include<conio.h> void main() { int a; int b; cout<<Enter number; cin>>b; for (int i=1;i<=b;i++) { a *=i; } cout<<a; getch(); } Output: Enter number4 24
7

* Write a program to display **** **** **** **** #include<iostream.h> #include<conio.h> void main() { for (int i=1;i<=4;i++) { for (int j=1;j<=4;j++) { cout<<*; } cout<<\n; } getch(); } Output: **** **** **** ****

* Write a program to display * ** *** **** #include<iostream.h> #include<conio.h> void main() { for (int i=1;i<=4;i++) { for (int j=1;j<=i;j++) { cout<<*; } cout<<\n; } getch(); }

Output: * ** *** ****

* Write a program to display whether number entered is prime or not: #include<iostream.h> #include<conio.h> void main() { int a; int fl=0; cout<<Enter number; cin>>a; for (int i=2;i<a;i++) { if (a%2==0) fl=1; break; } if (fl==1) cout<<Not prime; else cout<<Prime; getch(); } Output: Enter number4 Not prime

10

* Write a program to print Fibonacci Series up till 10 terms: #include<iostream.h> #include<conio.h> void main() { int a=1; int b=1; int c=1; int i; for (i=3;i<=10;i++) { c=b+a; a=b; b=c; cout<<c<< ; } getch(); } Output: 1 1 2 3 5 8 13 21 34 55

11

* Write a program to display series 1 4 9 16 25 : #include<iostream.h> #include<conio.h> void main() { int a; int b; int i; cout<<Enter number; cin>>a; for (i=1;1<=a;i++) { b=i*i; cout<< <<b; } getch(); } Output: Enter number6 1 4 9 16 25 36

12

*Write a program to accept and display a string array: #include<iostream.h> #include<conio.h> #include<stdio.h> void main() { char arr[50]; cout<<Enter string; gets(arr); cout<<arr; getch() } Output: Enter string Apeejay Saket Apeejay Saket * Write a program to display number of characters in a string: #include<iostream.h> #include<conio.h> #include<stdio.h> void main() { char[50]; cout<<Enter string; gets(arr); for (int i=0;arr[i]!=\0;i++) { cout<<Length of string is <<i; } getch(); } Output: Enter String Rahul Length of string is 6

13

* Write a program to display the reverse of a string: #include<iostream.h> #include<conio.h> #include<stdio.h> void main() { char arr[50]; gets(arr); for (int i=0;arr[i]!=\0;i++) { i--; for (;i>=0;i--) { cout<<arr[i]; } } Output: apeejay yajeepa

14

* Write a program to check whether a given string is a palindrome or not: #include<iostream.h> #include<conio.h> #include<stdio.h> #include<string.h> void main() { int i; int fl; int len; char arr[50]; cout<<Enter string; gets(arr); len=strlen(arr); len--; for (i=0;arr[i]!=\0;i++,len--) { if (arr[i]!=arr[len]) fl=1; break; } if (fl==1) cout<<Not a palindrome; else cout<<Its a palindrome; getch(); } Output: Enter string malayalam Its a palindrome

15

* Write a program to accept a string and a character and check whether the character is present in the string. If the character is found, display the position of the character. #include<iostream.h> #include<conio.h> #include<stdio.h> void main() { char arr[50]; char var[1]; int pos=0; cout<<Enter string; gets(arr); cout<<Enter character; cin<<var; for (int i=0;arr[i]!=\0;i++) { if (arr[i]==var) cout<<Character is present at <<i+1; pos=1; } if (pos==0) cout<<Not found; getch(); } Output: Enter string apeejay Enter Character p Character is present at 2

16

Write a program to accept two strings and compare them: #include<iostream.h> #include<conio.h> #include<stdio.h> void main() { char A[50]; char B[50]; int pos=0; cout<<Enter first string; cin>>A; cout<<Enter secong string; cin>>B; for(int i=0;A[i]!=\0||B[i]!=\0;i++) { if (A[i]==B[i]) pos=1; break; } if (pos==1) cout<< Similar strings; else cout<< Not similar; getch(); } Output: Enter first string apeejay Enter second string appejay Similar strings

17

* Write a program to remove spaces from a string: #include<iostream.h> #include<conio.h> #include<stdio.h> void main() { char arr[50]; int pos=0; cout<<Enter string; gets(arr); for(int i=0;arr[i]!=\0;i++) { if (arr[i]== ) {pos=1; for (;arr[i]!=\0;i++) { arr[i]=arr[i+1]; } arr[i-1]=\0; i=pos-1; } } puts(arr); getch(); } Output: Enter string This is a test file Thisisatestfile

18

You might also like