Date : 16 April 2018
Question 1 :
Convert the following while loop to for loop:
#include<stdio.h>
void main(){
int i =1 , j=5 , test=0;
while(i<5){
test=i+j;
printf("The tst value is %d", test);
i++;
}}
Answer:
#include<stdio.h>
void main(){
int i =1 , j=5 , test=0;
for(i=1; i<5; i++)
{
test=i+j;
printf("The tst value is %d", test);
}
}
Question 2 :
Write a program to print the sum of all the odd numbers from N to M using for loop. take M and N as user input.
Answer :
#include<cstdio>
#include<iostream>
using namespace std;
int main()
{
int N,M,i,x=0;
cin >> N >> M;
for(i = N ; i <= M; i++)
{
if( i % 2 == 1 )
x+=i;
}
couy << x << endl;
}
Question 3 :
Find the output of the given code segment :
int x = 0;
for( i = 0; i<5 ; i++ )
{
for( j = 0 ; j<5 ; j++ )
{
if( j % 2 == 1)
break;
else
x++;
}
printf("%d" , x);
}
Answer :
OUTPUT : 12345