You are on page 1of 3

Sessional 2: Course: ICP (CSC141) Class: BEE 2C – SP 2021

Marks: 30 ( Maps to CLO3)


Instructor: Amber Madeeha Zeb
TIME ALLOWED: 60 mins

Name: _Muhammad Saad___ Date: ___6th May 2021__


Roll No ____FA20-BEE-137_________ Siqnature:__________________

Question 1: (10 marks)

Write a C program to display and find the sum of the series 1+11+111+111 upto n.
For eg. if

n=4, the series is : 1+11+111+1111. Take the value of 'n' as input from
the user.
PROGRAM:

#include <stdio.h>
#include <stdlib.h>

int main()
{
int n,i,sum,p;
printf("\nInput Number of Series: ");
scanf("%d",&n);
p=1;
sum=0;
for(i=1;i<=n;i++)
{
printf("%d+",p);
sum=sum+p;
p=(p*10)+1;
}
printf("\n\nThe Sum of Series= %d\n",sum);
}

OUTPUT:
Question 2: (20 marks)

Write a C program to input n numbers in an array, calculate the sum of all even
numbers and all odd numbers in the array and print the larger sum.

Example:
If the array contains the following elements:
2, 3, 3, 5, 4, 8, 7, 11, 2

The sum of all even elements


is 2+4+8+2=16 Sum of all
odd elements is
3+3+5+7+11=29 Therefore,
the output should be 29.

PROGRAM:

#include <stdio.h>
#include <stdlib.h>

int main()
{
int size;
printf("\nNumber of Elements You Want To Save In
Array[upto 100]: ");
scanf("%d", &size);

printf("\nEnter the Numbers\n");


int i=0,Array[100],esum=0,osum=0,total=0;
while(i<size)
{
scanf("\n%d", &Array[i]);
i++;
}
/*here osum=Sum of odd and esum=Sum of Even*/
i=0;
while(i<size)
{
if(Array[i]%2==0)
{
esum=esum+Array[i];
}
else
{
osum=osum+Array[i];
}
i++;
}
/*Sum of odd+even=total*/
printf("\nSum of Even Numbers= %d ", esum);
printf("\nSum of Odd Numbers= %d ", osum);
total=esum+osum;
printf("\n\nTotal Sum of Both Even and Odd=
%d\n",total);
return 0;
}

OUTPUT:

You might also like