Sunteți pe pagina 1din 21

Practical - C Programming

Q. 1 Write algorithm for the following :

a) to check whether an entered number is odd / even.


b) to calculate sum of three numbers.

Answer 1)a . This is algorithm for checking whether an enter number is


odd/even.

Step 1: take the number from the user .


Step 2: if the number is fully division by 2 and remainder equal to zero
then display
then enter number as even .
Step 3: if the number is not fully division by 2 and remainder not equal to
zero then
display number as odd.

Answer 1)b . This is algorithm for for calculating the sum of 3 number
3
The formula for the sum of 3 number can be sum=∑number i
I=1
Step 1: let the sum be initially set to 0 .
Step 2: let I be initially 1
Step 3: take the ith number from the user
Step 4: add the ith number to the sum
Step 5: increase the value at I by 1.
Step 6: check if the value of I is less than or equal to 3 ; then go to step 4.
All 3 number are similarly entered by the user , until I becomes
greater than 3 . than go to step 7.
Step 7: output the sum .
Q. 2 Write short notes on the following :

a) C Variables b) C data types

a) VARIABLES :

A variables is a data name that may be used to store a data value . unlike
constant that remain unchanged during the execution of a program , a
variables may take different values at different times during execution .

A variable name can be chosen by programmer in a meaningful way as to


reflect its function or nature I the program .

Some example :-

Average , height, total , counter_1 , class_ strength .

Variables names may consist of letters, digits and underscore ( _ ) character


, subject to the following condition .

1. they must be began with letter . some system permit underscore as the
first letter .
2. ANSI standard recognizes a length of 31 character . however the
length should not be normally more than eight character . since only
the first eight character are treated as significant by many compiler .
3. uppercase and lower case are significant . that is the variable Total is
not the same as total or TOTAL .
4. the variable name should not be a keyword .
5. white space is not allowed .

some example of valid variable names are :

johan , value , t_raise , delhi , x ] , ph_value , mark , sum1 .

invalid example include :

.123 , ( are) , % , 25th


b) DATA TYPES

C language is rich in its data types . storage representation and machine


instruction to handle constant differ from machine to machine . the variety
of data types available allow the programmer to select the type
appropriate to the needs of the application as well as the machine ANCI
C supports for classes of data types :

1. primary ( or fundamental ) data type .

2. user defined data type .

3. derived data types.

4. empty data type .

1. primary ( or fundamental ) data type :-

INTEGRAL TYPE

Q. 2 Draw a flowchart for the following :

a) to find greater and smaller number from given two


numbers.
b) to calculate sum of first 10 odd numbers.
a) To find greater and smaller number from given two
numbers.

START

R
EAD
A
AND
B

YES NO
Is
A>B

Print Print
A
B

END
b) To calculate sum of first 10 odd numbers.

START

SUM=
0

N=0

N=N+1

N%2!=0 NO

YES

SUM=SUM+N

YES IS
N=10?

NO

PRINT
SUM

END
Q. 3 Accept principal amount, rate of interest, and duration from the
user. Display Interest Amount and Total Amount (Principal +
Interest).

Solution :-

#include<conio.h>

#include<stdio.h>

float r,t,p,total,sum ,interst ;

void main(void)

{clrscr();

printf("Enter Rate, Principal Amount ,Time (Duration ) \n");

printf("principal=");

scanf( "%f", &p);

printf("rate=");

scanf( " %f", &r);

printf("time=");

scanf( "%f", &t);

interst=((p*r*t)/100);

total=p+interst;

printf("total amount = %f \n interst = %f\n",total, interst );

getch();

}
Q. 4 Accept the salary of an employee from the user. Calculate the
gross salary on the following basis:

Basic HRA DA .

1 - 4000 10% 50%

4001 - 8000 20% 60%

8001 - 12000 25% 70%

12000 and above 30% 80%


Solution :-

#include<conio.h>

#include<stdio.h>

float basic,hra,da,gross_salary,basic1,basic2;

void main(void)

{clrscr();

printf("enter the employee salary status \n");

printf("basic= ");

scanf( "%f", &basic);

if(basic<=4000){hra=10; da=50;}

else if(basic>4000&&basic<=8000){hra=20; da=60;}

else if(basic>8000&&basic<=12000){hra=25; da=70;}

else if(basic>12000){hra=30; da=80;}

gross_salary = (basic*hra)/100+ (basic*da)/100 +basic ;

printf("gross salary =%f\n",gross_salary);

getch();

}
Q. 5 Accept any number from the user. Display whether the number is
divisible by 100 or not.

Solution :-

#include<conio.h>
#include<stdio.h>
int num,i ;
void main(void)
{clrscr();
printf( "enter the number \n");
scanf( "%d", &num);
if(num%100==0)
{
printf("divide by 100 ");
}
else
printf(" not divided by 100 ");
getch();
}
Q. 6 Accept a month in digit from the user. Display the month in
words. If number is not between 1 and 12 display message “Invalid
Month”. (Use ‘switch’)
Solution:-
#include<conio.h>
#include<stdio.h>
int num,i ;
void main(void)
{
char ch ;
clrscr();

while(!(num==100))
{

printf("\n
************************************************\n") ;
printf("\n Choose Month in digit for exit press e \n");
scanf( "%d", &num);

switch( num)
{
case 1:
{
printf ("\n Month in word January" ) ;
break;
}
case 2:
{
printf ("\n Month in word \" February\" \n " ) ;
break;
}
case 3:
{
printf ("\n Month in word \"March\" \n " ) ; break;
}
case 4:
{
printf ("\n Month in word \"April\" \n" ) ; break;
}
case 5:
{
printf ("\n Month in word \"May\" \n" ) ; break;
} case 6:
{
printf ("\n Month in word \"June\" \n" ) ; break;
}
case 7:
{
printf ("\n Month in word \"July\" \n" ) ; break;
}
case 8:
{
printf ("\n Month in word \"August\" \n" ) ; break;
}
case 9:
{
printf ("\n Month in word \"September\" \n " ) ; break;
}
case 10:
{
printf ("\n Month in word \"October\" \n" ) ; break;
}
case 11:
{
printf ("\n Month in word \"November\" \n " ) ; break;
}
case 12:
{
printf ("\n Month in word \"December\" \n " ) ; break;
}
default :
{

printf ("\n Enter digit show invalid month \n");


break;
}
}
scanf( "%c", &ch);
if((ch=='e')||(ch=='E')) break; } }
Q. 7 Display all prime numbers between 50 and 150.

Solution:-

#include<conio.h>
#include<stdio.h>
int i , num,p ,num1,j;
void main(void)
{clrscr();
printf("Enter intial number as 50 \n");
scanf( "%d" , &num1);
printf("Enter last number as 150 \n");
scanf( "%d", &num);
printf("Display all prime number between 50 and 150 \n\n");
for(j=num1 ; j<=num; j++)
{
p=0;
for(i=2; i<=num ; i++)
{
if (j%i==0)
{
p++;
}
}
if(p==1||num==1)
{
printf("Prime NO. is = %d\n", j);
}
}

getch();
}
Q. 8 Write a program to print the following pattern:

a) 1 b) 1

12 22

123 333

1234 4444

12345 55555
Solution :-

a) 1

12

123

1234

12345

#include<conio.h>
#include<stdio.h>
int num,i,j ,k,cnt=0;
void main(void)
{
clrscr();
printf("enter the number 5 \n") ;
scanf( "%d", &num);
printf("pattern are following \n\n");
for(i=1;i<num+1;i++)
{
cnt++;
for(k=0;k<cnt;k++)
{
printf("%d",k+1);
}
printf("\n");
}
getch();
}
b) 1

22

333

4444

55555

Solution:-

#include<conio.h>
#include<stdio.h>
int num,i,j ,k,cnt=0;
void main(void)
{clrscr();
printf("enter the number 5 \n") ;
scanf( "%d", &num);
printf("pattern are following \n\n");

for(i=1;i<num+1;i++)
{ cnt++;
for(k=0;k<cnt;k++)
{
printf("%d", i);
}
printf(" \n\n");
}
getch();
}
Q. 9 Write a program to swap the values of two numbers. Do this

Solution:-

#include<conio.h>

#include<stdio.h>

int a,b,*i,*j ;

void func(int *p,int *q)

{ int temp ;

temp=*q;

b=*p;

a=temp ; }

void main(void)

{clrscr();

printf("a=");

scanf( "%d",&a);

printf("b=");

scanf("%d",&b);

func(&a,&b);

printf("a= %d b= %d ", a,b);

getch(); }
Q. 10 Write a program to accept 10 values in an integer array. Display
the number of odd, even, and negative numbers.

Solution :-

#include<conio.h>
#include<stdio.h>
int i,cnt=0,t=0,nt=0 ;
int a[10];
void main(void)
{
clrscr();
printf("Enter the 10 numbers u want :- \n ");

for(i=0;i<10;i++)
{
scanf( "%d", &a[i]);
}
printf( "10 number are following :-\n ");
for(i=0;i<10;i++)
printf("%d\n",a[i]);
printf("\n Following are even values :-\n");
for(i=0;i<10;i++)
{
if(a[i]%2==0)
{ cnt=cnt+1;
printf( "\n%d \n",a[i]);
}

}
printf("\ntotal even no. are = %d \n" ,cnt );
printf("\n Following are 0dd values :-\n");
for(i=0;i<10;i++)
{
if(a[i]%2!=0)
{ nt=nt+1;
printf( "\n%d\n",a[i]);
}
}
printf("\ntotal odd no. are = %d \n" ,nt );
printf("\n Following are negative values :-\n");
for(i=0;i<10;i++)
{
if(a[i]<0)
{ t=t+1;
printf( "\n%d\n",a[i]);
}
}
printf("\ntotal negative no. are = %d \n" ,t );

getch();
}
Q. 11 Create a structure to store the employee number, name,
department and basic salary. Create a array of structure to accept
and display the values of 10 employees.
Solution :-

#include<stdio.h>

#include<conio.h>

int i, student;

float temp[10];

struct personal

char name[20];

char name1[20];

int basic_salary;

int age;

};

int main(void)

struct personal st[3];

clrscr();

printf( "number of Employee = ");

scanf("%d",&student);

for(i=0;i<student;i++)
{ printf("\n");

printf("**************************************************
*****************************");

printf("\n");

printf ( " Name of Employee = ");

scanf ( "%s", &st[i].name);

printf("\n");

printf ( "Name Of Department = ");

scanf ( "%s", &st[i].name1);

printf("\n");

printf("Employee no. = ");

scanf("%d", &st[i].age );

printf("\n");

printf ( "Basic salary = ");

scanf ( "%d", &st[i].basic_salary);

printf( "\n");

for(i=0;i<student;i++)

printf( "\n");
printf("**************************************************
*****************************");

printf (" Name of Employee = %s ",st[i].name);

printf("\n");

printf ( "Name of Department = %s ",st[i].name1);

printf("\n");

printf("Employee Code No. = %d", st[i].age);

printf("\n");

printf ( "Basic Salary = %d ",st[i].basic_salary);

getch();

S-ar putea să vă placă și