Sie sind auf Seite 1von 8

T1-CS101 INTRODUCTION TO COMPUTING 1 2014

C. IF ELSE STATEMENT
1. Program to accept a number and check the given number is Armstrong or not. (An
Armstrong number is a number that is equal to the sum of each of the individual digits
that make up the number raised to the power of the number of digits the number
has. For example 153 is an Armstrong number because 153 = 1^3 + 5^3 + 3^3.)
CODE
# include <stdio.h>// header
# include <conio.h>//header
main( )//the main program
{//terminal
int n, a, b, c, d;//declare int variable
clrscr( ); //to clear the previous screen
printf ( Enter a Three Digit Number: );//function to display a string
scanf (%d, &n);//scan formatted
a=n/100;
b=((n/10)%10);
c=n%10;
d=a*a*a*+b*b*b +c*c*c;
if (n= =d)// executes or skips a sequence of statements
printf (The Given Number is Armstrong number);//function to display a string
else// it extends an if statement to execute a different statement
printf (The Given Number is Not Armstrong number);//function to display a
string
getch( );//function that will wait for key press
}//terminal

Shermaine May F. Apigo

T1-CS101 INTRODUCTION TO COMPUTING 1 2014

OUTPUT
Enter a three digit number 371
The given number is Armstrong number

2. Program to accept a year and check whether the given year is leap year or not.
Shermaine May F. Apigo

T1-CS101 INTRODUCTION TO COMPUTING 1 2014

CODE
# include <stdio.h>//header
# include <conio.h>//header
main( )//the main program
{//terminal, beginning
int y;//declare int variable
clrscr( );//clear previous screen
printf(enter a year:);//displays ENTER A YEAR
scanf(%d,&y);//scan formatted
if(y%4==0& &y%100!=0|| y%400==0);//a sequence or statements
printf(the above given year IS a leap year);//displays a string
else//// it extends an if statement to execute a different statement
printf(the above given year IS not a leap year);//displays a string
getch();support the conio
}//terminal
OUTPUT
Enter a year: 2000
The above given year is a leap year

Shermaine May F. Apigo

T1-CS101 INTRODUCTION TO COMPUTING 1 2014

3. Program to print prime numbers between 1 to 10.


CODE
#include<stdio.h>//header
# include <conio.h>//header
main( )//main function
{//terminal
int n, i, check;//variable for integers
clrscr();//slear the previous screen
for(i=1;i<=100;i++)//iteration statement.
{//terminal
check=1;
for(n=2;n<=i/2;n++)//iteration statement.
if(i%n= =0)//a sequence or statements
{//terminal
check=0;
break;
}//terminal
if(check= =1) //a sequence or statements
printf(\n %d is a prime,i);//displays a string
else//another sequence statements
printf(\n %d is not a prime,i);//displays a string
}//terminal

Shermaine May F. Apigo

T1-CS101 INTRODUCTION TO COMPUTING 1 2014

getch( );//support the conio


OUTPUT
1 is a prime
2 is a prime
3 is a prime
4 is not a prime
5 is a prime
6 is not a prime
7 is a prime
8 is not a prime
9 is not a prime
10 is not a prime

Shermaine May F. Apigo

T1-CS101 INTRODUCTION TO COMPUTING 1 2014

4. Program to read date,month, year and print the next days date,month,year.
CODE
# include <stdio.h>//header
# include <conio.h>//header
main( )//main function
{
int month[12]={31,28,31,30,31,30,31,31,30,31,30,31};//variable for integers
int d,m,y,nd,nm,ny,ndays;//variable for integers
clrscr( );// clear the previous screen
printf(enter the date,month,year);// displays a string
scanf(%d%d%d,&d,&m,&y);//scan formatted
ndays=month[m-1];
if(m==2)//sequence statements
{//terminal
if(y%100==0)//sequence statements
{//terminal
if(y%400==0)//sequence statements
ndays=29;
}//terminal
Else//another sequence statements
if(y%4==0)/sequence statements
ndays=29;

Shermaine May F. Apigo

T1-CS101 INTRODUCTION TO COMPUTING 1 2014

}//terminal
nd=nd+1;
nm=m;
ny=y;
if(nd>ndays)//sequence statements
{
nd=1;
nm++;
}
if(nm>12)//sequence statements
{
nm=1;
ny++;
}
printf(Given date is %d:%d:%d\n,d,m,y);// displays a string
printf(next days date is %d:%d:%d,nd,nm,ny);//displays a string
getch( );//support the conio
}//terminal close

OUTPUT
Enter the date, month, year 19 8 14
Given date is 19 8 14
Next day date is 20 8 14

Shermaine May F. Apigo

T1-CS101 INTRODUCTION TO COMPUTING 1 2014

5. Program to check whether a given number is perfect or not.


CODE
# include <stdio.h>//header
# include <conio.h>//header
main( )//main function
{//terminal
int i,n,s=0;//variable for integers
clrscr();// clear screen
printf(enter the number);//displays strings
scanf(%d,&n);//scan formatted
for(i=1;i<n/2;i++)//iteration statement.
if(n%i==0)//sequence statement
s+=i;
if(s= =n)//sequence statements
printf(the number is perfect no);//displays string
else//another sequence statement
printf(the number is not perfect );//displays string
getch( );//support the conio
}//terminal close

OUTPUT
Enter the number 6
The number is perfect no
Shermaine May F. Apigo

Das könnte Ihnen auch gefallen