0% found this document useful (0 votes)
44 views

CSE Assignment-1

Uploaded by

Talha Jobair
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views

CSE Assignment-1

Uploaded by

Talha Jobair
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

CSE-103

Name : Md. Talha Jobair


ID : 2023-1-60-120
Course Information: Programming Structure
Section : 11

Write a program (WAP) that will print following series upto Nth terms.
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, .......
Sample input Sample output
2 1, 2
5 1, 2, 3, 4, 5
11 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11

#include<stdio.h>

int main()
{
int n,i;
printf("Enter the value of N: ");
scanf("%d",&n);

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


{
printf("%d ",i);
}
return 0;
}
Write a program (WAP) that will print following series upto Nth terms.
1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31 .......
Sample input Sample output
2 1, 3
5 1, 3, 5, 7, 9
11 1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21

#include<stdio.h>
int main()
{
int n,i,odd=1;
printf("Enter the value of N: ");
scanf("%d",&n);
printf("The series of odd numbers is: ");
for (i=1; i<=n; i++)
{
printf("%d ",odd);
odd=odd+2;
}
return 0;
}
Write a program (WAP) that will take N numbers as inputs and compute their average.
(Restriction: Without using any array)

Sample input Sample output


3
10 20 30.5

AVG of 3 inputs: 20.166667

2
22.4 11.1

AVG of 2 inputs: 16.750000

#include<stdio.h>
int main()
{
int n,i,sum=0,num;
float avg;
printf("Enter the value of N:");
scanf("%d",&n);
for(i=1; i<=n; i++)
{
printf("Enter number %d:",i);
scanf("%d",&num);
sum=sum+num;
}
printf("The average is: %.2f",(float)sum/n);
return 0;
}
Write a program (WAP) that will take two numbers X and Y as inputs. Then it will print
the square of X and increment (if X<Y) or decrement (if X>Y) X by 1, until X reaches Y. If
and when X is equal to Y, the program prints “Reached!”

Sample input(X,Y) Sample output


10 5 100, 81, 64, 49, 36, Reached!
5 10 25, 36, 49, 64, 81, Reached!
10 10 Reached!

#include<stdio.h>
int main()
{
int x,y;
printf("Enter two numbers: ");
scanf("%d%d",&x,&y);

int square=x*x;
printf("The square of %d is %d\n",x,square);

if(x<y)
{
x++;
printf("Incremented x is: %d\n", x);
}
else if(x > y)
{
x--;
printf("Decremented x is: %d\n", x);
}
printf("Reached!\n");
return 0;
}
Write a program (WAP) that will run and show keyboard inputs until the user types an ’A’
at the keyboard.

Sample input Sample output

X
1
a
A

Input 1: X
Input 2: 1
Input 3: a

#include <stdio.h>
int main()
{
char input;
printf("Enter keyboard inputs");
printf("Type 'A' to end.\n");

do
{
scanf(" %c", &input);
printf("%c\n", input);
} while (input!='A' && input!='a');

printf("Program End");

return 0;
}
Write a program (WAP) that will reverse the digits of an input integer.

Sample input Sample output

13579 97531
4321 1234

#include<stdio.h>
int main() {
int num,reverse=0,remainder=0;
printf("Enter an integer: ");
scanf("%d",&num);
while(num!=0) {
remainder=num%10;
reverse=reverse*10+remainder;
num/=10;
}
printf("Reversed number is: %d", reverse);
return 0;
}
Write a program (WAP) that will give the sum of first Nth terms for the following series.

1, -2, 3, -4, 5, -6, 7, -8, 9, -10, 11, -12, 13, -14, .......
Sample input Sample output

2 Result: -1
3 Result: 2
4 Result: -2

#include<stdio.h>
int main() {
int n,sum=0,i,term;
printf("Enter the value of n: ");
scanf("%d",&n);
for (i=1; i<=n; i++) {
term=(i%2==0) ? -i : i;
sum+=term;
}
printf("The sum of the first %d terms is: %d\n",n,sum);
return 0;
}
Write a program (WAP) that will calculate the result for the first Nth terms of the following
series. [In that series sum, dot sign (.) means multiplication]

12
.2 + 22
.3 + 32
.4 + 42
.5 + .......

Sample input Sample output

2 Result: 14
3 Result: 50
4 Result: 130
7 Result: 924

#include <stdio.h>
int main() {
int n,i;
float sum=0;
printf("Enter the value of n: ");
scanf("%d",&n);
for (i=1; i<=n; i++) {
sum+=i*i*(i+1);
}
printf("The sum of the first %d terms of the series is: %f\n",n,sum);
return 0;
}
Write a program (WAP) that will print Fibonacci series upto Nth terms.
1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, .......
Sample input Sample output
1 1
2 1, 1
4 1, 1, 2, 3
7 1, 1, 2, 3, 5, 8, 13

#include <stdio.h>
int main() {
int n,i;
int first=0,second=1,next;
printf("Enter the number of terms: ");
scanf("%d",&n);
printf("Fibonacci Series up to %d terms:\n",n);
for (i=1; i<=n; i++) {
printf("%d, ",second);
next=first+second;
first=second;
second=next;
}
return 0;
}
Write a program (WAP) that will print the factorial (N!) of a given number N. Please see
the sample input output.

Sample input Sample output


1 1! = 1 = 1
2 2! = 2 X 1 = 2
3 3! = 3 X 2 X 1 = 6
4 4! = 4 X 3 X 2 X 1 = 24

#include <stdio.h>
int main() {
int n, fact = 1;
printf("Enter a positive integer: ");
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
fact *= i;
}
printf("Factorial of %d is %d\n", n, fact);
return 0;
}
Write a program (WAP) that will find xy

(x to the power y) where x, y are positive integers.

Sample input(x,y) Sample output


5 2 25
201
616
050

#include <stdio.h>
int power(int x, int y) {
int result=1;
for (int i = 1; i <= y; i++) {
result = result * x;
}
return result;
}
int main() {
int x, y;
printf("Enter the values of x and y: ");
scanf("%d%d", &x, &y);
int result = power(x, y);
printf("%d to the power %d is %d\n", x, y, result);
return 0;
}
WAP that will determine whether a number is prime or not.
Sample input Sample output
1 Not prime
2 Prime
11 Prime
39 Not prime
101 Prime
#include <stdio.h>
int main() {
int n,i,prime=0;
printf("Enter a positive integer: ");
scanf("%d",&n);
for(i=2; i<=n/2; ++i) {
if(n%i==0) {
prime=1;
break;
}
}
if(n==1) {
printf("1 is neither prime nor composite.\n");
} else {
if(prime==0)
printf("%d is a prime number.\n", n);
else
printf("%d is not a prime number.\n", n);
}
return 0;
}
WAP that will determine whether an integer is palindrome number or not.

Sample input Sample output


9 Yes
91 No
222 Yes
12321 Yes
110 No

#include <stdio.h>
int main() {
int n,reverse=0,temp;

printf("Enter an integer: ");


scanf("%d",&n);
temp=n;

while(temp!=0) {
reverse=reverse*10+temp%10;
temp/=10;
}
if(n==reverse) {
printf("%d is a palindrome number.\n", n);
} else {
printf("%d is not a palindrome number.\n", n);
}
return 0;
}
Write a program that takes an integer number n as input and find out the sum of the
following series up to n terms.

1 + 12 + 123 + 1234 + .......


Sample input Sample output
11
2 13
3 136
4 1370
#include <stdio.h>
int main() {
int n, i, j, sum = 0, term = 0;
printf("Enter a number: ");
scanf("%d", &n);
for (i = 1; i <= n; i++) {
term = term * 10 + i;
sum += term;
}
printf("The sum of the series up to %d terms is %d.\n", n, sum);
return 0;
}

You might also like