Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (1)
- Program to add two numbers
- #include<iostream.h>
- #include<conio.h>
- void main()
- {
- int a,b,sum;
- cout<<”Enter two numbers”;
- cin>>a>>b;
- sum=a+b;
- cout<<”The sum is”<<sum;
- getch();
- }
- (2)
- Program to Convert a Decimal number upto 100 digits into Binary number
- #include<iostream.h>
- #include<conio.h>
- #include<string.h>
- #include<stdio.h>
- void main()
- {
- clrscr();
- char dec[101],bin[101];
- int n,q,rem,t,j,k=0;
- cout<<"Enter Decimal number upto 100 digits";
- gets(dec);
- while(dec[0]!='\0')
- {
- rem=0;j=0;
- for(int i=0;dec[i]!='\0';i++)
- {
- t=dec[i]-48;
- n=rem*10+t;
- q=n/2;
- rem=n%2;
- dec[j]=q+48;
- j++;
- if(dec[0]=='0')
- j=0;
- }
- dec[j]='\0';
- bin[k]=rem+48;
- k++;
- }
- bin[k]='\0';
- strrev(bin);
- cout<<"The binary Number is";
- puts(bin);
- getch();
- }
- (3)
- Function Overloading Example
- #include<iostream.h>
- #include<conio.h>
- #include<math.h>
- void area(int a)
- {
- cout<<"Area of Square"<<a*a;
- }
- void area(int l,int b)
- {
- cout<<"Area of rectangle"<<l*b;
- }
- void area(int a,int b,int c)
- {
- float s,ar;
- s=(a+b+c)/2.0;
- ar=sqrt(s*(s-a)*(s-b)*(s-c));
- cout<<"Area of triangle"<<ar;
- }
- void main()
- {
- int op,a,b,c;
- cout<<"1.Area of Square";
- cout<<"\n2.Area of Rectangle";
- cout<<"\n3.Area of Triangle";
- cout<<"\nEnter your option";
- cin>>op;
- switch(op)
- {
- case 1:
- cout<<"Enter Side";
- cin>>a;
- area(a);
- break;
- case 2:
- cout<<"Enter length and breadth";
- cin>>a>>b;
- area(a,b);
- break;
- case 3:
- cout<<"Enter Sides of a triangle";
- cin>>a>>b>>c;
- area(a,b,c);
- break;
- default:
- cout<<"Invalid Option";
- }
- getch();
- }
- (4)
- Program to add 10 integer numbers from 1 to 10 using while loop
- #include<iostream.h>
- #include<conio.h>
- void main()
- {
- clrscr();
- int x=1,n=10,sum=0;
- while(x<=n)
- {
- sum=sum+x;
- x++;
- }
- cout<<"TOTAL is "<<sum;
- getch();
- }
- (5)
- Program to add 10 integer numbers from 1 to 10 using do...while loop
- #include<iostream.h>
- #include<conio.h>
- void main()
- {
- clrscr();
- int x=1,n=10,sum=0;
- do
- {
- sum=sum+x;
- x++;
- }while(x<=n);
- cout<<"TOTAL is "<<sum;
- getch();
- }
- (6)
- Program to allot Grade to students on the basis of their percentage
- #include<iostream.h>
- #include<conio.h>
- void main()
- {
- clrscr();
- int hindi,english,maths,total,percent;
- cout<<"enter hindi marks ";
- cin>>hindi;
- cout<<"enter english marks ";
- cin>>english;
- cout<<"enter maths marks ";
- cin>>maths;
- total= hindi + english + maths;
- cout<<"total is "<<total<<endl;
- percent = total/3; /*switch case can not be used for conditions*/
- if(percent >= 90 && percent <=100)
- { cout<<"Excellent "; }
- else if(percent >= 80 && percent <= 89)
- {cout<<"A+ "; }
- else if(percent >= 70 && percent <= 79)
- {cout<<"A "; }
- else if(percent >= 60 && percent <= 69)
- { cout<<"B+ "; }
- else if(percent >= 50 && percent <= 59)
- {cout<<"B "; }
- else if(percent >= 40 && percent <= 49)
- { cout<<"C "; }
- else if(percent >= 0 && percent <= 39)
- {cout<<"F "; }
- getch();
- (7)
- Program to find maximum of three numbers
- #include<iostream.h>
- #include<conio.h>
- int maximum(int a,int b, int c);
- void main()
- {
- int x,y,z,max;
- cout<<"enter three numbers ";
- cin>>x>>y>>z;
- max=maximum(x,y,z);
- cout<<"maximum is "<<max;
- getch();
- }
- int maximum(int a,int b,int c)
- {
- if(a>b)
- if(a>c)
- return a;
- else
- return c;
- else
- if(b>c)
- return b;
- else
- return c;
- }
- (8)
- Program to perform arithmetic operations
- #include<iostream.h>
- #include<conio.h>
- #include<stdlib.h> //for exit function
- #include<dos.h> //for sleep function
- double operation(float, float ,char);
- void main()
- {
- clrscr();
- float x,y,z;
- char op;
- cout<<"enter first value ";
- cin>>x;
- cout<<"enter second value ";
- cin>>y;
- cout<<"enter operator ";
- cin>>op;
- z=operation(x,y,op);
- cout<<z;
- getch();
- }
- double operation(float a,float b, char op)
- {
- float c;
- switch(op)
- {
- case '+' : c=a+b;
- break;
- case '-' : c=a-b;
- break;
- case '*' : c=a*b;
- break;
- case '/' : if(b==0)
- {
- cout<<"wrong values";
- sleep(5);
- exit(0);
- }
- else
- c=a/b;
- break;
- case 'q' : exit(0);
- default : cout<<"worng operator";
- break;
- }
- return c;
- }
- (9)
- Program to check maximum number using function and pointer
- #include<iostream.h>
- #include<conio.h>
- long int max(int *,int *);
- void main()
- {
- clrscr();
- int *p,a,b;
- cout<<"ENter two values ";
- cin>>a>>b;
- *p=(a,b);
- cout<<"MAximum is "<<*p;
- getch();
- }
- long int max(int *x,int *y)
- {
- if(*x>*y)
- return *x;
- else
- return *y;
- }
- (10)
- Program to find sum of two numbers using "functions"
- #include<iostream.h>
- #include<conio.h>
- void sum(int a,int b);
- void main()
- {
- clrscr();
- int a,b;
- cout<<"enter 2 vaues";
- cin>>a>>b;
- sum(a,b);
- getch();
- }
- void sum(int a,int b)
- {
- int sum=0;
- sum=a+b;
- cout<<"sum is"<<sum;
- }
- (11)
- Program to find Factorial of number
- #include<iostream.h>
- #include<conio.h>
- void factorial(int a);
- void main()
- {
- clrscr();
- int a;
- cout<<"enter a no.";
- cin>>a;
- factorial(a);
- getch();
- }
- void factorial(int a)
- {
- int fact=1;
- for(int i=1;i<=a;i++)
- {
- fact=fact*i;
- }
- cout<<"factorial is"<<fact;
- }
- (12)
- Program to find whether a number is Prime or not
- #include<iostream.h>
- #include<conio.h>
- void prime(int a);
- void main()
- {
- clrscr();
- int a;
- cout<<"enter a no.";
- cin>>a;
- if(a==1)
- {
- cout<<"1 is neither prime nor composite";
- }
- prime(a);
- getch();
- }
- void prime(int a)
- {
- int flag=0;
- for(int i=2;i<a;i++)
- {
- if(a%i==0)
- {
- flag=1;
- break;
- }
- }
- if(flag==0)
- {
- cout<<"prime no.";
- }
- else
- {
- cout<<"not a prime no.";
- }
- }
- (13)
- Program to check whether a number is Palindrome or not
- #include<iostream.h>
- #include<conio.h>
- void palindrome(int a);
- void main()
- {
- clrscr();
- int a;
- cout<<"enter a no.";
- cin>>a;
- palindrome(a);
- getch();
- }
- void palindrome(int a)
- {
- int r,b=0,f=100,on=a;
- while(a>0)
- {
- r=a%10;
- a=a/10;
- b=b+(r*f);
- f=f/10;
- }
- if(on==b)
- {
- cout<<"palindrome";
- }
- else
- {
- cout<<"not a palindrome";
- }
- }
- (13)
- Program to check whether an alphabet is a Vowel or not
- #include<iostream.h>
- #include<conio.h>
- void vowel(char a);
- void main()
- {
- clrscr();
- char a;
- cout<<"enter a alphabet";
- cin>>a;
- vowel(a);
- getch();
- }
- void vowel(char a)
- {
- if(a=='a'||a=='e'||a=='i'||a=='o'||a=='u')
- {
- cout<<"vowel";
- }
- else
- {
- cout<<"not a vowel";
- }
- }
- (14)
- Program to find maximum in three numbers
- #include<iostream.h>
- #include<conio.h>
- void greater(int a,int b,int c);
- void main()
- {
- clrscr();
- int a,b,c;
- cout<<"enter 3 nos";
- cin>>a>>b>>c;
- greater(a,b,c);
- getch();
- }
- void greater(int a,int b,int c)
- {
- if(a>b&&a>c)
- {
- cout<<a<<"is greater";
- }
- else
- {
- if(b>c)
- {
- cout<<b<<" is greater";
- }
- else
- {
- cout<<c<<" is greater";
- }
- }
- }
- (15)
- Program to check whether an year is a leap year or not
- #include<iostream.h>
- #include<conio.h>
- void lyear(int a);
- void main()
- {
- clrscr();
- int a;
- cout<<"enter a no.";
- cin>>a;
- lyear(a);
- getch();
- }
- void lyear(int a)
- {
- cout<<"\n\n";
- if(a%100==0&&a%400==0||a%4==0)
- {
- cout<<a<<"is a leap year";
- }
- else
- {
- cout<<a<<"it is not a leap year";
- }
- }
- (16)
- Program to Swap two numbers
- #include<iostream.h>
- #include<conio.h>
- void swap(int a,int b);
- void main()
- {
- clrscr();
- int a,b;
- cout<<"enter 2 nos";
- cout<<"\n\n";
- cin>>a;
- cout<<"\n\n";
- cin>>b;
- cout<<"\n\n";
- cout<<"before swapping"<<"\n\n"<<"a="<<a;
- cout<<"\n\n";
- cout<<"b="<<b;
- swap(a,b);
- getch();
- }
- void swap(int a,int b)
- {
- cout<<"\n\n";
- int temp;
- temp=a;
- a=b;
- b=temp;
- cout<<"after swapping"<<"\n\n"<<"a="<<a;
- cout<<"\n\n";
- cout<<"b="<<b;
- }
- (17)
- Program to illustrate RETURN BY REFERENCE
- #include<iostream.h>
- #include<conio.h>
- int &max(int &a,int &b)
- {
- if(a>b)
- return a;
- else
- {
- return b;
- }
- }
- void main()
- { clrscr();
- int a,b;
- cout<<"enter 2 nos";
- cout<<"\n\n";
- cin>>a;
- cout<<"\n\n";
- cin>>b;
- max(a,b)=1;
- if(a==1)
- {
- cout<<"\n\n";
- cout<<"a is greater";
- }
- if(b==1)
- {
- cout<<"\n\n";
- cout<<"b is greater";
- }
- getch();
- }
- (18)
- Program to find Prime number from 1 to 100
- #include<iostream.h>
- #include<conio.h>
- void prime();
- void main()
- {
- clrscr();
- prime();
- getch();
- }
- void prime()
- {
- int flag=0;
- cout<<"prime nos. are";
- for(int i=3;i<=100;i++)
- {
- flag=0;
- for(int j=2;j<i;j++)
- {
- if(i%j==0)
- {
- flag=1;
- break;
- }
- }
- if(flag==0)
- cout<<i<<"\t";
- }
- }
- (19)
- Program to perform "1.SUM OF DIGITS" & "2.REVERSE OF A NUMBER"
- #include<iostream.h>
- #include<conio.h>
- void sod(int a);
- void reverse(int a);
- void main()
- {
- int op,a;
- clrscr();
- cout<<"1.sum of digit\n\n";
- cout<<"2.reverse a no.\n\n";
- cin>>op;
- switch(op)
- {
- case 1: sod(a);
- break;
- case 2: reverse(a);
- break;
- case 3:cout<<"invalid option";
- }
- getch();
- }
- void sod(int a)
- {
- int sum=0,r;
- cout<<"\n\n";
- cout<<"enter a no.";
- cin>>a;
- while(a>0)
- {
- r=a%10;
- a=a/10;
- sum=sum+r;
- }
- cout<<"\n\n";
- cout<<"sum is"<<sum;
- }
- void reverse(int a)
- {
- int b=0,r;
- cout<<"\n\n";
- cout<<"enter a no";
- cin>>a;
- while(a>0)
- {
- r=a%10;
- a=a/10;
- b=b*10+r;
- }
- cout<<"\n\n";
- cout<<"reverse is"<<b;
- }
- (20)
- Program to find FACTORIAL using "RECURSION"
- #include<iostream.h>
- #include<conio.h>
- int fact(int n)
- {
- int f=1;
- if(n==1)
- {
- return f;
- }
- else
- {
- f=n*fact(n-1);
- return f;
- }
- }
- void main()
- {
- clrscr();
- int n;
- cout<<"enter a no.";
- cin>>n;
- cout<<"\n\n";
- cout<<"factorial is"<<fact(n);
- getch();
- }
- (21)
- Program to calculate length of string
- #include<stdio.h>
- #include<iostream.h>
- #include<conio.h>
- void count(char a[]);
- void main()
- {
- clrscr();
- char a[10],i;
- cout<<"\n";
- cout<<"enter a string";
- cout<<"\n";
- gets(a);
- count(a);
- getch();
- }
- void count(char a[])
- {
- int countt=0,i;
- for(i=0;a[i]!='\0';i++)
- {
- countt++;
- }
- cout<<"\n";
- cout<<"the length of string is"<<"\t"<<countt;
- }
- (22)
- Program to calculate number of VOWELS in a string
- #include<stdio.h>
- #include<iostream.h>
- #include<conio.h>
- void vowel(char a[]);
- void main()
- {
- clrscr();
- char a[10];
- cout<<"enter a string";
- gets(a);
- vowel(a);
- getch();
- }
- void vowel(char a[])
- {
- int i,count;
- for(i=0;a[i]!='\o';i++)
- {
- if(a[i]=='a'||a[i]=='e'||a[i]=='i'a[i]=='o'||a[i]=='u')
- count++;
- }
- cout<<"no. of vowels in a string are"<<count;
- }
- Email ThisBlogThis!
- (23)
- Program to check whether a string is a PALINDROME or not
- #include<string.h>
- #include<stdio.h>
- #include<iostream.h>
- #include<conio.h>
- void palindrome(char a[]);
- void main()
- {
- clrscr();
- char a[10];
- cout<<"\n\n";
- cout<<"enter a string";
- cout<<"\n\n";
- gets(a);
- cout<<"\n";
- palindrome(a);
- getch();
- }
- void palindrome(char a[])
- {
- int i,n,flag=0;
- n=strlen(a);
- for(i=0;i<n/2;i++)
- {
- if(a[i]!=a[n-1-i])
- {
- flag=1;
- break;
- }
- }
- if(flag==0)
- {
- cout<<"\n";
- cout<<"it is a palindrome";
- }
- else
- {
- cout<<"\n";
- cout<<"it is not a palindrome";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment