C Programming: Series Calculation, Positive Sum, Switch, Loop Transformation, Quizzes of Computer Science

Three c programming exercises. The first exercise involves writing a program to calculate a specific series. The second exercise requires writing a program that prompts the user for a sequence of integers, computes the summation of positive numbers, and counts the number of negative numbers. The third exercise involves using a switch-statement in c. The last exercise is a transformation of the third exercise's while loop into a for loop.

Typology: Quizzes

2020/2021

Available from 06/05/2024

omr-22
omr-22 🇪🇬

12 documents

1 / 2

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1. Write a C program to calculate the following series
1
x2
x2+3
x34
x4n
xn
2. Write a C program that prompts the user for a sequence of integers and the
program computes the summation of positive numbers and counts the
number of negative numbers in this sequence. The sequence is ended when
the user enter a value of 0. By using do … while.
3. Write a C program to use switch-statement. Let x be an integer denoted by a
user.
f(x)
{
3x2+1,if x ϵ {4,7,10 }
9x+2, if {1,3,8}
8, if x =2
3x3, otherwise
4. Trace and Answer
a.
b.
c.
int i;
for(i=1;i<=20;i++)
{
if (i%3==0)
break;
}
printf(“%d”,i);
int count=0;
for(int i=1;i<=20;i++)
{
if (i%3==0)
continue;
count ++;
}
printf(“%d”,count);
int balance =100;
int consumed =0;
while(1)
{
if(balance<10)
break;
if(!(consumed %10))
continue;
balance=consumed;
printf(“The balance is %d”,balance);
}
1
pf2

Partial preview of the text

Download C Programming: Series Calculation, Positive Sum, Switch, Loop Transformation and more Quizzes Computer Science in PDF only on Docsity!

1. Write a C program to calculate the following series

x

x

2 +^

x

3 −^

x

4 …^

n x n

2. Write a C program that prompts the user for a sequence of integers and the

program computes the summation of positive numbers and counts the

number of negative numbers in this sequence. The sequence is ended when

the user enter a value of 0. By using do … while.

3. Write a C program to use switch-statement. Let x be an integer denoted by a

user. f^ (^ x )

3 x 2

  • 1 ,if x ϵ { 4 , 7 , 10 } − 9 x + 2 , if x ϵ { 1 , 3 , 8 } 8 , if x = 2 3 x 3 , otherwise

4. Trace and Answer

a.

b.

c.

int i;

for(i=1;i<=20;i++)

if (i%3==0)

break;

printf(“%d”,i);

int count=0;

for(int i=1;i<=20;i++)

if (i%3==0)

continue;

count ++;

printf(“%d”,count);int balance =100;

int consumed =0;

while(1)

if(balance<10)

break;

if(!(consumed %10))

continue;

balance=consumed;

printf(“The balance is %d”,balance);

5. Convert question 4.c from while loop into a for loop