j

Wednesday, 25 November 2015

If Statement in C / C++


If Statement in C 

If statement is used to execute a statement or set of statements, or skip this statements. The ability to control the flow of your program, letting it make decisions on what code to execute, is valuable to the programmer. The if statement allows you to control if a program enters a section of code or not based on whether a given condition is true or false. One of the important functions of the if statement is that it allows the program to select an action based upon the user's input. For example, by using an if statement to check a user-entered password, your program can decide whether a user is allowed access to the program.
Without a conditional statement such as the if statement, programs would run almost the exact same way every time, always following the same sequence of function calls. If statements allow the flow of the program to be changed, which leads to more interesting code.
Syntax
Its syntax is as follows.
if(condition)
statements;
else
statements;
Two or more statements are written in curly brackets { }. The syntax for compound statements in if else statement is as follows:
if(condition)
{
Statements 1;
Statements 2:
.
Statements N;
}
else
{
Statements 1;
Statements 2:
.
Statements N;
}

Example

if ( 5 < 10 )
    printf( "Five is less then ten" ); 
 

 Here, we're just evaluating the statement, "is five less than ten", to see if it is true or not; with any luck, it is! If you want, you can write your own full program including stdio.h and put this in the main function and run it to test. To have more than one statement execute after an if statement that evaluates to true, use braces, like we did with the body of the main function. Anything inside braces is called a compound statement, or a block. When using if statements, the code that depends on the if statement is called the "body" of the if statement.
 

Write a program that takes marks from user via keyboard. If marks greater 40 then display “Congratulation! You have Passed” if marks less than 40 then display “Oops! You have Fail”.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int marks;
cout<<"Enter your marks: ";
cin>>marks;
if(marks>=40)
cout<<"Congratulation! You have Passed. ";
else
cout<<"Oops! Your fail.";
getch();
}

Write a program that take grade A,B,C and D if user enter A then Display Exelent if enter B then Good if Enter C Average and if Enter D then Display Poor

Program

#include<iostream.h>
#include<conio.h>
void main()
{
char grd;
cout<<"Enter grade A,B,C or D...";
grd=getche();
if(grd=='a'||grd=='A')
cout<<"\nEXCELENT";
else if(grd=='B'||grd=='b')
cout<<"\nGOOD";
else if(grd=='c'||grd=='C')
cout<<"\nAVERAGE";
else if(grd=='d'||grd=='D')
cout<<"\nPOOR";
else
cout<<"\nINVALID GRADE";
getch();
}


Output

 

Write a program that take grade A,B,C and D if user enter A then Display Exelent if enter B then Good if Enter C Average and if Enter D then Display Poor

Program

#include<stdio.h>
#include<conio.h>
void main()
{
char grd;
printf("Enter grade A,B,C or D...");
grd=getche();
if(grd=='a'||grd=='A')
printf("\nEXCELENT");
else if(grd=='B'||grd=='b')
printf("\nGOOD");
else if(grd=='c'||grd=='C')
printf("\nAVERAGE");
else if(grd=='d'||grd=='D')
printf("\nPOOR");
else
printf("\nINVALID GRADE");
getch();
}


Output

 

Monday, 16 November 2015

Computer Science (A-Cource)-I First Annual papper 2014 (C Language) For B.Sc Double Computer part I



Computer Science (A-Course) IST/A 2014
Q.1 Write a program to finds the sum of the cubes of the integers from 1 to n. where n is entered by user i.e. (13+23+33+43………..+n3)?    (10)
Solution
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int a,n;
long int cube=0;
printf("Enter the value ::");
scanf("%d",&n);
a=2;
while(a<=n)
{
cube=cube+(a*a*a);
a=a+2;
}
printf("Cube of the series is %ld",cube);
getch();
}
Q2. Write a Program to find the greatest of three numbers; here the three values are passed to the function which returns the greatest of three?      (10)
Solution
#include<stdio.h>
#include<conio.h>
int function(int,int,int);
void main()
{
clrscr();
int a,b,c,r;
printf("Enter three numbers:");
scanf("%d%d%d",&a,&b,&c);
r=function(a,b,c);
printf("Greatest number is %d",r);
getch();
}
int function(int x,int y,int z)
{
int max;
max=x;
if(max<y)
max=y;
if(max<z)
max=z;
return max;
}
Q3. Write a program that inputs a number from 1 to 10 and display it’s in words using switch statement. For example if user enter 6, it display “six”?       (10)
Solution
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int n;
printf("Enter a number from 1 to 10....::");
scanf("%d",&n);
switch(n)
{
case 1:
printf("One");
break;
case 2:
printf("Two");
break;
case 3:
printf("Three");
break;
case 4:
printf("Four");
break;
case 5:
printf("Five");
break;
case 6:
printf("Six");
break;
case 7:
printf("Seven");
break;
case 8:
printf("Eight");
break;
case 9:
printf("Nine");
case 10:
printf("Ten");
default:
printf("Invalid Choice...");
}
getch();
}
Q4.
A.      What is difference between “pass by value and pass by reference” in function?   (5)
Pass by value…
                A parameter passing mechanism in which the value of actual parameter is copied to formal parameter of called function is known as pass by value. If the function makes any change in formal parameter, it does not affect the values of actual parameter. It is the default mechanism for passing parameters to functions.
For example write a program that inputs two numbers in main () function, passes these numbers a function. The function displays the maximum number.
                                #include<stdio.h>
#include<conio.h>
int function(int,int);
void main()
{
clrscr();
int a,b;
printf("Enter three numbers:");
scanf("%d%d%d",&a,&b,&c);
function(a,b);
getch();
}
int function(int x,int y)
{
if(x<y)
printf("Maximum number is %d",x);
else
printf("Maximum number is %d",x);
}
                Pass by Reference
A parameter passing mechanism in which the address of actual parameter is passed it the called function is known as pass by reference. The formal parameter is not created separately in the memory. Formal parameter becomes a second name of actual variable parameter. It means that single memory location is shared between actual parameter and formal parameter. If the called function makes any change in formal parameter, the change also available in calling functions.
Write a program that inputs two numbers in main () and passes the integer s to a function by reference. The function swaps the values. The main () function should display the values before and after swapping.
#include<stdio.h>
#include<conio.h>
void swap(int &,int&);
void main()
{
clrscr();
int a,b;
printf(“Enter an integer…:”);
scanf(“%d”,&a);
printf(“Enter an integer…:”);
scanf(“%d”,&b);
printf(“Values before swaping\n”);
printf(“a=%d\nb=%d\n”,a,b;
swap(a,b);
printf(“Values after swaping\n”);
printf(“a=%d\nb=%d\n”,a,b;
getch();
}
void swap(int &x,int&v)
{
Int t;
t=x;
x=y;
y=t;
}
          B.      What is the difference between compiler and interpreter? (5)
Compiler
        A compiler is program that converts the instruction of a high level language into machine language as whole. A program that written in high level language is called source program. A compiler convert’s source program into machine code is known as object program.
                                                                                                                                                                  
        The compiler checks each statement in the source program and generates machine instructions. Compiler also checks syntax errors in the program. A source program containing an error cannot be compiled.

        A compiler can translate the program of only that for which it is written. For example C compiler can translate only those program that are written in C language.
Interpreter
        An interpreter is program that converts one statement of program at one time. It executes this statement before translating the next statement of the source program. If these are an error in the statements, the interpreter stops working and display and error message.
        The advantage of interpreter over compilers is that an error is found immediately. So the programmer can convert errors during program development.
        The disadvantage of interpreter is that it is not very efficient. The interpreter does not produce an abject program. It must covert the program each time it is executed. Visual Basic uses interpreter.
Q5.
            A.       What are Logical Operators? (5)
Logical operators
Logical operators are used to evaluate more than one condition in single conditional structure. The three logical operators are AND(&&) operators, OR (||) and NOT (!) operators.
                AND Operators (&&)
                                AND(&&) operators is a logical operators. It is used to determine the two or more condition in one conditional statement. It remains true if those entire two or more conditional is true. It remains false if one condition is false.
Condition 1
Operator
Condition 2
result
True
&&
False
False
True
&&
True
True
False
&&
True
False
False
&&
False
False

OR Operator(||)
                It is used to determine two or more condition used in one condition statement. It remains true if one condition is true in the given statement. It remains false if all condition is false.
Condition 1
Operator
Condition 2
Result
True
||
False
True
True
||
True
True
False
||
True
True
False
||
False
False

NOT Operator(!)
                It remains true if the given condition is false. It remains false if the given condition is true.
Operator
Condition
Result
!
False
True
!
True
False



            B.      What is flowchart explain different flow chart symbols?   (5)
Flowchart
        Flowchart is combination of two words i.e. flow and chart. Chart consists of different symbols to display information about any program. Flow indicates the direction of processing that takes place in the program. Flowchart is a graphical representation of an algorithm. It is used to show all the steps of an algorithm in a sequence.
        Normally, an algorithm is first converted to a flowchart to show its steps graphically. The flowchart is then converted into a program written in ay programming language.
Flowchart Symbols
1.       Input/output
Parallelogram symbol is used to represent an input or output step. Input statement is used to get input from user. The output statement is used to display a message to the user or display a value


2.       Process
Rectangle symbol is used to represent a process step. A process step may be a complex calculation or simply an assignment statement.



                       
sum=a+b    
 


3.       Selection
Diamond symbol is used to represent a selection step. A condition is given in the diamond. The flow of control from diamond may go in two direction i.e. one direction if the condition is true and the second direction if the condition is false.



 



4.       Start/End
Oval symbol is used to represent the start or end of the flowchart






 
5.       Flow Lines
Arrow symbols are used to represent the direction of flow in the flowchart. There are four types of flow lines.










6.       Connector
Circle symbol is used to combine different line. It is used when two are more flow symbols come from different direction and move to one direction.

                                                


 

Q6. Write a program that inputs a number in main function and passes the number to a function. The function display factorial of the number?    (10)
Solution
#include<stdio.h>
#include<conio.h>
int factorial(int);
void main()
{
int n,r;
clrscr();
printf("Enter any number ::");
scanf("%d",&n);
factorial(n);
getch();
}
int factorial(int x)
{
int f,a;
f=1;
a=1;
while(a<=x)
{
f=f*a;
a++;
}
printf("The Factorial of %d is %d",x,f);
}
Q7. Write a C program to read any character from user and if that character is capital letter then display it with small letter and if it is small letter then display with Capital letter.     (10)
 Solution.
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
char ch;
printf("Enter Any Character....");
scanf("%c",&ch);
if(ch>='a'&&ch<='z')
ch=ch-32;
else if(ch>='A'&&ch<='Z')
ch=ch+32;
else
printf("Invalid Choice...");
printf("%c",ch);
getch();
}
Q8. Write a program that calculate electric bill depending on the following conditions Consumption Units Rate of charges
                000 --- 200 Rs. 0.50 per unit
                201 ---400 Rs. 100 plus Rs. 0.65 per unit excess of 200
                401--- 600 Rs. 230 plus Rs. 0.80 per unit excess of 400
                601 and above Rs. 390 plus Rs. 1.00 per unit excess 600                                 (10)
Solution.
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int bill,un,p;
printf("Enter Consumption Units....");
scanf("%d",&un);
if(un>0&&un<=200)
bill=un*0.5;
else if(un>200&&un<=400)
bill=un*0.65+100;
else if(un>400&&un<=600)
bill=un*0.80+230;
else if(un>600)
bill=un*1+390;
printf("The bill is %d",bill);
getch();
}