- 1] if Loop
- To print an Even number
- #include<stdio.h>
- #include<conio.h>
- void main()
- {
- int number=0;
- clrscr();
- printf("enter a number:");
- scanf("%d",&number);
- if(number%2==0)
- {
- printf("%d is even number",number);
- }
- getch();
- }
- Output:
- Enter a number 4
- 4 is even number
- Enter a number 5
- 2] While Loop
- To print numbers from 1 to 10
- #include <stdio.h>
- #include <conio.h>
- void main()
- {
- int i=1;
- clrscr();
- while(i<=10)
- {
- printf("%d \n",i);
- i++;
- }
- getch();
- }
- Output:
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 3)Array [1D-Array]
- To print the marks of a student
- #include <stdio.h>
- #include <conio.h>
- void main(){
- int i=0;
- int marks[5];//declaration of array
- clrscr();
- marks[0]=80;//initialization of array
- marks[1]=60;
- marks[2]=70;
- marks[3]=85;
- marks[4]=75;//traversal of array
- for(i=0;i<5;i++){
- printf("%d \n",marks[i]);
- }//end of for loop
- getch();
- }
- Output:
80 60 70 85 75
4]Without recursion
#include<stdio.h>
#include<conio.h>
void main()
- {
- int n1=0,n2=1,n3,i,number;
- clrscr();
- printf("Enter the number of elements:");
- scanf("%d",&number);
- printf("\n%d %d",n1,n2);//printing 0 and 1
- for(i=2;i<number;++i)//loop starts from 2 because 0 and 1 are already printed
- {
- n3=n1+n2;
- printf(" %d",n3);
- n1=n2;
- n2=n3;
- }
- getch();
- }
No comments:
Post a Comment