CSE Assignment-1
CSE Assignment-1
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);
#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)
2
22.4 11.1
#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!”
#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.
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.
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 + .......
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.
#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
#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.
#include <stdio.h>
int main() {
int n,reverse=0,temp;
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.