Sie sind auf Seite 1von 34

/*Function to accept and display a character */

#include<stdio.h> #include<conio.h> void main() { char alpha; alph=getc(stdin); fflush(stdin); putc(alph,stdout); }

/*function to input character and display the character input twice*/


#include<stdio.h> #include<conio.h> void main() { char alpha; alph=getc(stdin); fflush(stdin); putc(alph,stdout); putc(alph,stdout);

/*function to accept and store 2 characters in different memory location and to display them one after the other using the function getchar() and putchar()*/ #include<stdio.h> #include<conio.h> void main() { char a,b; a=getchar(); fflush(stdin); b=getchar(); putchar(a); putchar(b); }

/*Write a function to display a string*/ #include<stdio.h> #include<conio.h> void main() { char str[n+1]; puts(Enter a string max. of 10 characters); gets(str); fflush(stdin); puts(str); }

/*write a function that prompts a name and address upto 30 characters and accepts them one at a time*/ char Name[30], Add[30]; puts(Enter a name of max 30 characters); gets(Name); fflush(stdin); puts(Enter an address of max 30 characters); gets(Add); puts(Your name is); puts(Name); puts(Your address is); puts(Add); getch(); }

/*if the input character is A, the message displayed is character is A, otherwise the message character is not A is displayed*/ #include<stdio.h> void main() { char chr; puts(Enter a character); getc(chr); fflush(stdin); if(chr==A) puts(character is A); else puts(character is not A); }

/*function to determine if the input is a vowel*/ #include<stdio.h> main() { char vow; Puts(Enter a character); vow=getchar() fflush(stdin); if(vow==a) puts(vowel a); else if(vow==e); puts(vowel e); else if(vow==i); puts(vowel i); else if(vow==o); puts(vowel o); else if(vow==u); puts(vowel u); else puts(invalid); }

/*a function that accepts a one character grade code, and depending on what grade is input, displays the HRA percentage accordingly: */ #include<stdio.h> main() { char chr; Puts(Enter a grade from A,B,C or D); chr=getchar() fflush(stdin); if(chr==A) puts(HRA is 45%); else if(chr==B) puts(HRA is 40%); else if(chr==C) puts(HRA is 30%); else if(chr==B) puts(HRA is 25%); else puts(invalid); }

#include<stdio.h> #include<conio.h>

void main() { int a,b,c; clrscr();

printf("nEnter value of a, b & c: "); scanf("%d %d %d",&a,&b,&c);

if((a>b)&&(a>c)) printf("na is greatest");

if((b>c)&&(b>a)) printf("nb is greatest");

if((c>a)&&(c>b)) printf("nc is greatest");

getch();

#include<stdio.h> #include<conio.h> void main() { int i=1; clrscr(); for(i=1;i<=10;i++) printf("%d",i); getch(); }

#include<stdio.h> #include<conio.h> void main() { int a,b,sum; clrscr(); printf("Enter two no: "); scanf("%d%d",&a,&b); sum = a+b; printf("Sum : %d",sum); getch(); }

#include<stdio.h> #include<conio.h>

void main() { int num,rem,rev=0; clrscr();

printf("nEnter any no to be reversed : "); scanf("%d",&num);

while(num>=1) { rem = num % 10; rev = rev * 10 + rem; num = num / 10; }

printf("nReversed Number : %d",rev); getch(); }

#include<stdio.h> #include<conio.h> void main() { int a,b,c; clrscr(); printf("nEnter value of a, b & c: "); scanf("%d %d %d",&a,&b,&c); if((a>b)&&(a>c)) printf("na is greatest"); if((b>c)&&(b>a)) printf("nb is greatest"); if((c>a)&&(c>b)) printf("nc is greatest"); getch(); }

#include<stdio.h> #include<conio.h> void main() { float radius,area; clrscr(); // Clear Screen printf("nEnter the radius of Circle : "); scanf("%d",&radius); area = 3.14 * radius * radius; printf("nArea of Circle : %f",area); getch(); }

#include<iostream.h> #include<conio.h> void main() { //clrscr(); int number,count=0; cout<<"ENTER NUMBER TO CHECK IT IS PRIME OR NOT "; cin>>number; for(int a=1;a<=number;a++) { if(number%a==0) { count++; } } if(count==2) { cout<<" PRIME NUMBER \n"; } else { cout<<" NOT A PRIME NUMBER \n"; } //getch();

#include<iostream> using namespace std; int main() { int width,height,area,perimeter; cout<<"Enter cin>>width; cout<<"Enter cin>>height; area=height*width; cout<<"Area of Rectangle ="<<area<<endl; perimeter=2*(height+width); cout<<" Perimeter of rectangle are = "< return 0; } Height of Rectangle = "; Width of Rectangle = ";

/*Function to accept a character and display it 10 times*/


#include<stdio.h> #include<conio.h> void main() { clrscr(); int i=0; char ch; puts("Enter a character:"); ch=getchar(); fflush(stdin); while(i<10) { puts(" "); putchar(ch); i=i+1; } getch(); }

/*Function to accept character from keyboard until input '!' is given and to display whether the total number of non-vowel characetr entered is more than /less than or equal to the total number of vowels entered*/ #include<stdio.h> #include<conio.h> void main() { clrscr(); int v=0,nv=0; char ch; do { puts("Enter a character :"); ch=getchar(); fflush(stdin); switch(ch) { case 'a': case 'A': case 'e': case 'E': case 'i': case 'I': case 'o': case 'O': case 'u': case'U': v=v+1; break; default : nv=nv+1; } }while(ch!='!'); if(v>nv) { puts("Vowel are greater than Non-vowel"); } else if(v<nv) { puts("Vowel are less than non-vowel"); } else { puts("Vowel are equal to non-vowel"); } getch(); }

/*Function to display table of any number*/ #include<stdio.h> #include<conio.h> void main() { clrscr(); int num,i=1; puts("Enter the number of which you want to print the table: "); scanf("%d",num); while(i<=10) { num=num*i; i=i+1; printf("%d\n",num); } getch(); }

//Program to show the use of arithmetic assignment operators #include<iostream.h> void main() { int num=5; cout<<"Num= "<<num<<endl; num+=2; //num=num+2 cout<<"Num after using (+=) num*=3; //num=num*3 cout<<"Num after using (*=) num-=7; //num=num-7 cout<<"Num after using (-=) num/=2; //num=num/2 cout<<"Num after using (/=) num%=2; //num=num%2 cout<<"Num after using (%=) }

= "<<num<<endl; = "<<num<<endl; = "<<num<<endl; = "<<num<<endl; = "<<num<<endl;

//Program showing the use of integer variables #include<iostream.h> void main() { int var1; //Defining the integer type variables int var2=10,var3; //Defining integer variables and assigning value to them var3=30; cout<<"The value of var1 is "<<var1<<endl; cout<<"The value of var2 is "<<var2<<endl; cout<<"The value of var3 is "<<var3<<endl; }

/*Function to accept numbers until 0 is entered or 10 numbers have been accepted and prints the total number of enteries, hte number of positive entries and sum of all the positive numbers entered*/ #include<stdio.h> #include<conio.h> void main() { clrscr(); int i,sum=0,num,j=0; for(i=0;i<10;i++) { printf("Enter a number : "); scanf("%d",&num); fflush(stdin); if (num<0) { continue; } if(num==0) { break; } j=j+1; sum=sum+num; } printf("Total no. of enteries are %d\nAnd total no. of positive enteries are %d",i,j); printf("\nSum of %d positive numbers entered is %d",j,sum); getch(); }

/*Function to accept 25 numbers from the keyboard and prints the sum of all the positive numbers entered*/ #include<stdio.h> #include<conio.h> void main() { clrscr(); int i,sum=0,num; for(i=0;i<10;i++) { printf("Enter a number : "); scanf("%d",&num); fflush(stdin); if (num<=0) { continue; } sum=sum+num; } printf("\nSum of all the positive numbers entered getch(); }

is %d",sum);

//Program to show the use of logical operator "AND" && #include<iostream.h> #include<conio.h> #include<stdio.h> void main() { int num; cout<<"Enter a number "; cin>>num; if( (num!=0) && (num%2)==0 ) evaluates true fulfilled { cout<<"Even number"; } else { cout<<"Odd number or ZERO"; } }

//combines two expressions and //only if both expressions are

//Program to show the use of logical operator "OR" || #include<iostream.h> void main() { char ch; cout<<"Enter a character 'A' or 'a' : "; cin>>ch; if(ch=='A' || ch=='a') //combines two condition and evaluates true //if any one of the condition are fulfilled { cout<<"Valid character"; } else { cout<<"Invalid character"; } }

//Program showing the use of integer variables #include<iostream.h> void main() { int var1; //Defining the integer type variables int var2=20.25; //Defining integer variables and assigning value to them var1=30.45; cout<<"The value of var1 is "<<var1; cout<<" and the value of var2 is "<<var2<<endl; //cursor will move to yhe newline }

//Program to show the use of if....else construct and relational operators #include<iostream.h> void main() { int var1,var2; cout<<"Enter the first number "; cin>>var1; cout<<"Enter second number "; cin>>var2; if(var1>var2) //check the expression enclosed in braces { cout<<"var1 is greater than var2"; //this line get printed on the screen if // the above condition become true } else //this will be executed if the above condition become false { cout<<"var1 is less than or equal to var2"; } }

//Program to show the use of arithmetic operators #include<iostream.h> void main() { int var1=71, var2=6, add,sub,mul,div,rem; //defining integer variables add=var1+var2; //add two numbers sub=var1-var2; //subtract two numbers mul=var1*var2; //multiply two numbers div=var1/var2; //divide two numbers rem=var1%var2; //calculate the remainder after dividing two numbers cout<<"Addition of var 1 and var2 is "<<add<<endl; cout<<"Subtraction of var 1 and var2 is "<<sub<<endl; cout<<"Product of var 1 and var2 is "<<mul<<endl; cout<<"Quotient is "<<div<<endl; cout<<"Remainder is "<<rem; }

//Program showing the use of character variables #include<iostream.h> void main() { char var1; //Defining character variables char var2='A'; //Defining variables and assigning value to it var1='a'; cout<<"The value of var1 is "<<var1<<endl; cout<<"The value of var2 is "<<var2<<endl; }

//Program to show the use of if....else construct (without else) #include<iostream.h> void main() { int var1,var2; cout<<"Enter the first number "; cin>>var1; cout<<"Enter second number "; cin>>var2; if(var1>var2) //check the expression enclosed in braces { cout<<"var1 is greater than var2"; //this line get printed on the screen if // the above condition become true } }

//This program displays the message "Hello world" #include<iostream.h> void main() { cout<<"HELLO WORLD"<<endl; }

//message "HELLO WORLD" is displayed

/*Function to find sum of numbers input until the number 999 entered or 10 numbers have been entered*/ #include<stdio.h> #include<conio.h> void main() { clrscr(); int sum=0,i=0,num; while(i<10) { printf("Enter a number : "); scanf("%d",&num); fflush(stdin); if (num==999) { break; } sum=sum+num; i=i+1; } printf("Sum of %d numbers is %d",i,sum); getch(); }

Das könnte Ihnen auch gefallen