KhLM2ZqeS06HpWmXQWXLAQ Week 3 Practice Lab Solutions

Download as pdf or txt
Download as pdf or txt
You are on page 1of 13

PracticeLab1_Question1(a)

#include<stdio.h>
int main(){
int a, b, c;
printf("Enter three integers a, b, and c - ");
scanf(" %d %d %d",&a,&b,&c);

if(a==b){
if(a==c){
printf("True\n");
}
else{
printf("False\n");
}
}
else{
printf("False\n");
}

/*
//alternate soln
if(a==b && a==c){
printf("True\n");
}
else{
printf("False\n");
}
*/

return 0;
}
PracticeLab1_Question1(b)
#include<stdio.h>
int main(){
int a, b, c;
printf("Enter three integers a, b, and c - ");
scanf(" %d %d %d",&a,&b,&c);

if(a!=b && a!=c && b!=c){


printf("True\n");
}
else{
printf("False\n");
}

/*
//alternate soln
if(a==b)
printf("False\n");
if(a==c)
printf("False\n");
if(b==c)
printf("False\n");
else
printf("True\n");
*/

return 0;
}
PracticeLab1_Question2
#include<stdio.h>
int main(){

int marks;
printf("Please enter your marks: ");
scanf(" %d", &marks);

if(marks >=90)
printf("Your grade is Extraordinary");

else if(marks >=80)


printf("Your grade is Very Good");

else if(marks >=65)


printf("Your grade is Good");

else if(marks >=50)


printf("Your grade is Average");

else if(marks >=40)


printf("Your grade is Below Average");
else
printf("Fail, but it is not the end of the world.");

}
Practice Lab 2_Question 1-(1)part
#include<stdio.h>
int main()
{
/*
Current wrong output:
1
21
321
4321
54321

Intended Output, part a:


54321
4321
321
21
1
*/
int N;
printf("Enter a Number N: ");
scanf(" %d", &N);

int i = 1; // outer loop control variable


int j; // inner loop control variable
while(i <= N)
{
j = N-i+1;
while(j > 0)
{
printf("%d",j);
j--;
}
i++;
printf("\n");
}
}
Practice Lab 2_Question 1-(2)part
#include<stdio.h>
int main()
{
/*
part b
Intended Output:
54321
5432
543
54
5
*/
int N;
printf("Enter a Number N: ");
scanf(" %d", &N);

i = 1; // outer loop control variable


while(i <= N)
{
j = N;
while(j >= i)
{
printf("%d",j);
j--;
}
i++;
printf("\n");
}
}
Practice Lab 2_Question 2
#include<stdio.h>
int main()
{

int N;
printf("Please Enter a number greater than 0: ");
scanf(" %d", &N);

while( N<=0)
{
printf("Please Enter a number greater than 0: ");
scanf(" %d", &N);
}
printf("Ok, number entered is greater than 0");

printf("\nPlease Enter a number lesser than 0: ");


scanf(" %d", &N);
while( N>=0)
{
printf("Please Enter a number lesser than 0: ");
scanf(" %d", &N);
}
printf("Ok, number entered is lesser than 0\n");

printf("\nPlease Enter a number greater than 5 OR less than -5: ");


scanf(" %d", &N);
while( N<=5 && N>=-5)
{
printf("Please Enter a number greater than 5 OR less than -5: ");
scanf(" %d", &N);
}
printf("Ok, number entered is greater than 5 OR less than -5\n");

printf("Please Enter a number greater than 20 AND less than 100: ");
scanf(" %d", &N);
while( N<=20 || N>=100)
{
printf("Please Enter a number greater than 20 AND less than 100:
");
scanf(" %d", &N);
}
printf("Ok, number entered is greater than 20 AND less than 100\n");

return 0;
}
Practice Lab 2_Question 3
#include<stdio.h>
int main(){

int end = 1000000; // 1. Try smaller number


int start = 1;
unsigned long long i = start, j = 0; // 2. Should we use just int here
if 'end' is too large ? Change datatype as mentioned in the ques

while(i <= end){

j = i;
// printf("%llu ", j); //uncomment if you want to see the series
// loop until the current number becomes 1
while(j!=1)
{
if(j%2 == 0){
j/=2;
}

else{
j = 3*j +1;
}

// printf("%llu ", j); //uncomment if you want to see the


series
}

// %d id for int, explore %llu


// printf("\n%llu \n\n", i);
printf("%llu ", i);
i++;
}

return 0;
}
PracticeLab 2_Question_4 (a)
#include<stdio.h>

int main(){

int month_number; // 1 to 12

printf("Please enter month number between 1 and 12: ");


scanf(" %d", &month_number);
while(month_number <1 || month_number>12){
printf("Please enter Correct month number between 1 and 12: ");
scanf(" %d", &month_number);
}

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

return 0;

}
PracticeLab 2_Question_4 (b)
#include<stdio.h>
/*
sample inputs and outputs
input: 9
output: September
October
November
December

*/
int main(){

int month_number; // 1 to 12

printf("Please enter month number between 1 and 12: ");


scanf(" %d", &month_number);
while(month_number <1 || month_number>12){
printf("Please enter Correct month number between 1 and 12: ");
scanf(" %d", &month_number);
}

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

return 0;

}
Practice Lab 3_Question 1 (a)
#include<stdio.h>
int main(){

int N;
printf("Enter the value of N - ");
scanf(" %d", &N);

int sum = 0;

for(int i = 1; i<=N; ++i){


sum += i*i;
}

printf("Sum of squares from 1 to %d is %d\n", N, sum);

return 0;
}

Practice Lab 3_Question 1 (b)


#include<stdio.h>
int main(){

int N;
printf("Enter the value of N - ");
scanf(" %d", &N);

int sum = 0;

for(int i = 1; i<=N; ++i){


sum += i*i*i;
}

printf("Sum of cubes from 1 to %d is %d\n", N, sum);

return 0;
}
Practice Lab 3_Question 2
The program prompt asks the user to enter a positive number (although it doesn’t throw an error
if zero or a negative number is entered). Then if the number is greater than or equal to 79, it
simply exits. If number entered is less than 79 (even a negative number), the program prints all
the multiples of 5 in the range (start,79). ‘Start’ will be printed only if it is a multiple of 5
otherwise the next immediate multiple of 5 coming after 5 will be printed, similarly at the end as
last number 75 is printed which is the last multiple of 5 before 79.

Practice Lab 3_Question 3 (1)


#include<stdio.h>
int main(){
int num_rows;
printf("Enter the number of rows for which the pattern should be
printed - ");
scanf(" %d", &num_rows);

for(int i = 1; i<=num_rows ;++i){


for(int j = 1;j<=num_rows; ++j){
printf("%d",(i+j+1)%2);
}
printf("\n");
}
printf("\n");

return 0;
}

Practice Lab 3_Question 3 (2)


#include<stdio.h>
int main(){
int num_rows;
printf("Enter the number of rows for which the pattern should be
printed - ");
scanf(" %d", &num_rows);

for(int i = 1; i<=num_rows ;++i){


for(int j = 1;j<=i; ++j){
printf("%d",(i+j+1)%2);
}
printf("\n");
}
printf("\n");

return 0;
}

Practice Lab 3_Question 4

#include<stdio.h>
int main(){

unsigned long long int a, b, ans = 1;


// Variables are declared as unsigned long long int so that they can
store big integers also

printf("Enter two positive integers a and b - ");

/* ENTER A STATEMENT TO TAKE a AND b AS INPUT FROM THE USER HERE*/


scanf(" %llu %llu", &a, &b);

/*WRITE A FOR LOOP TO MULTIPLY ans BY a FOR b NUMBER OF TIMES*/


for(unsigned long long int i = 1; i<=b; ++i)
ans *= a;

printf("%llu^%llu = %llu\n",a,b,ans);

return 0;
}

You might also like