0% found this document useful (0 votes)
45 views8 pages

Labsheet-3 (Advanced) Program Flow Control

The document is a labsheet containing 5 programming questions in C. It includes the code for each question and the expected output. Question 1 writes a program to calculate the Julian day from a date. Question 2 checks if a number is prime. Question 3 calculates the greatest common divisor and least common multiple of two numbers. Question 4 calculates an employee's year-end bonus based on their salary and years of service. Question 5 writes a program to store student details like roll number, marks in different subjects and GPA in a file.

Uploaded by

Ankitha Reddy E
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views8 pages

Labsheet-3 (Advanced) Program Flow Control

The document is a labsheet containing 5 programming questions in C. It includes the code for each question and the expected output. Question 1 writes a program to calculate the Julian day from a date. Question 2 checks if a number is prime. Question 3 calculates the greatest common divisor and least common multiple of two numbers. Question 4 calculates an employee's year-end bonus based on their salary and years of service. Question 5 writes a program to store student details like roll number, marks in different subjects and GPA in a file.

Uploaded by

Ankitha Reddy E
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Labsheet-3 (Advanced)

Program Flow Control


Atma Anand
(SC12B156)
Sub: C Programming Lab
Date of Submission: September 5, 2013

Department: Physical Sciences

Question 1:
Program Code:
#i n c l u d e <s t d i o . h>
i n t main ( )
{
i n t d , y ,m, j =0, i ;
p r i n t f ( Enter t h e Date (DDMMYYYY) : ) ;
s c a n f (%2d%2d%4d,&d,&m,&y ) ;
i f (m>12)
{
p r i n t f ( I n v a l i d Month ! ! Terminating ! ) ;
return 1;
}
f o r ( i =1; i<=m; i ++)
{
switch ( i )
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
i f ( i !=m)
j +=31;
e l s e i f ( d>31)
{
p r i n t f ( I n v a l i d Date ! ! Terminating ! ) ;
return 1;
}
break ;
case 4:
case 6:
case 9:
case 11:
i f ( i !=m)
1

j +=30;
e l s e i f ( d>30)
{
p r i n t f ( I n v a l i d Date ! ! Terminating ! ) ;
return 1;
}
break ;
case 2:
i f ( y%400==0 | | ( y%4==0 && y %100!=0))
{
i f ( i !=m)
j +=29;
e l s e i f ( d>29)
{
p r i n t f ( I n v a l i d Date ! ! Terminating ! ) ;
return 1;
}
}
else
{
i f ( i !=m)
j +=28;
e l s e i f ( d>28)
{
p r i n t f ( I n v a l i d Date ! ! Terminating ! ) ;
return 1;
}
}
break ;
}
}
j+=d ;
p r i n t f ( The J u l i a n Day = %d , j ) ;
return 0;
}

Result
Output Screens:

.
.

Discussion
The program computes t h e J u l i a n Day o r g i v e s an e r r o r i f t h e d a t e i s i n c o r r e c t .

Question 2:
Program Code:
#i n c l u d e <s t d i o . h>
i n t main ( )
{
int n , i ;
p r i n t f ( Enter t h e number : \ n ) ;
s c a n f (%d,&n ) ;
f o r ( i =2; i<=n / 2 ; i ++)
i f ( n%i ==0)
{
p r i n t f ( The no . i s NOT a Prime Number . ) ;
return 0;
}
p r i n t f ( The no . i s a Prime Number . ) ;
return 0;
}

Result
Output Screen:

Question 3:
Program Code:
#i n c l u d e <s t d i o . h>
i n t main ( )
{
i n t n1 , n2 , q , r ;
long p ;
p r i n t f ( Enter t h e numbers : \ n ) ;
s c a n f (%d%d,&n1 ,&n2 ) ;
p=( l o n g ) n1n2 ;
w h i l e ( r=n1%n2 )
{
n1=n2 ;
n2=r ;
}
p r i n t f ( GCD = %d , n2 ) ;
p r i n t f ( \nLCM = %l d , p/n2 ) ;
return 0;
}

Result
Output Screen:

Question 4:
Program Code:
#i n c l u d e <s t d i o . h>
i n t main ( )
{
i n t yr ;
long s ;
f l o a t b=0;
p r i n t f ( Enter t h e Number o f Years i n S e r v i c e and S a l a r y : \ n ) ;
s c a n f (%d%l d ,& yr ,& s ) ;
i f ( yr==1)
b=0.1 s ;
e l s e i f ( yr==2)
b =0.15 s ;
e l s e i f ( yr==3)
b =0.35 s ;
e l s e i f ( yr >=4)
b=0.5 s ;
p r i n t f ( Year End Bonus= %.2 f , b ) ;
return 0;
}

Result
Output Screen:

Question 5:
The program \ t e x t i t { s t o r e s } t h e g r a d e s h e e t o f a c l a s s .

Program Code:
// Program t o s t o r e SC Code , Marks and GPA o f S t d e n t s
#i n c l u d e <s t d i o . h>
#i n c l u d e <s t r i n g . h>
struct stu
{
i n t m[ 6 ] ;
float g;
char s [ 8 ] ;
};
typedef s t r u c t stu stud ;
i n t main ( )
{
i n t i =0, l , j ;
c h a r c =y , f [ 1 5 ] ;
struct stu l i s t ;
FILE f p ;
p r i n t f ( Enter t h e no . o f s t u d e n t s : ) ;
s c a n f (%d,& l ) ;
f p=f o p e n ( temp1 . dat , w+);
i f ( f p==NULL)
{
p r i n t f ( E r r o r ! Terminating ! ) ;
return 1;
}
p r i n t f ( Enter t h e d e t a i l s o r xxx i n SC code t o e x i t : \ n\n ) ;
p r i n t f ( SC Code\ t \ tSub1 \ tSub2 \ tSub3 \ tSub4 \ tSub5 \ tSub6 \tGPA\n\n ) ;
f o r ( i =0; i <l ; i ++)
{
s c a n f (% s ,& l i s t . s ) ;
i f ( s t r c m p i ( l i s t . s , xxx)==0)
break ;
f o r ( j =0; j <6; j ++)
s c a n f (%d,& l i s t .m[ j ] ) ;
s c a n f (% f ,& l i s t . g ) ;
f w r i t e (& l i s t , s i z e o f ( s t u d ) , 1 , f p ) ;
}
f s e e k ( f p , 0 , SEEK SET ) ;
p r i n t f ( \ nYou Entered : \ nSC Code\ tSub1 \ tSub2 \ tSub3 \ tSub4 \ tSub5 \ tSub6 \tGPA\n ) ;
f o r ( l =0; l <i ; l ++)
{
f r e a d (& l i s t , s i z e o f ( s t u d ) , 1 , f p ) ;
p r i n t f ( \ n%s \ t , l i s t . s ) ;
f o r ( j =0; j <6; j ++)
p r i n t f (%d\ t , l i s t .m[ j ] ) ;
p r i n t f (% f \ t , l i s t . g ) ;
}
p r i n t f ( \ nSave (Y/N) ) ;
getchar ( ) ;
s c a n f (%c ,& c ) ;
i f ( c==y | | c==Y )
7

{
p r i n t f ( Enter t h e name o f f i l e : ( max 15 l e t t e r s ) ) ;
getchar ( ) ;
gets ( f ) ;
i= rename ( temp1 . dat , f ) ;
i f ( i == 0 )
p u t s ( F i l e s u c c e s s f u l l y saved ! ) ;
else
perror ( Error saving f i l e ! ) ;
}
p r i n t f ( \ nEnd o f Code ) ;
f c l o s e ( fp ) ;
return 0;
}

Result
Output Screen:

You might also like