Week 2 Coding Assignment Name: Priyanka Indra Roll No.: 84 Dept: CSE1 Sem:6
Week 2 Coding Assignment Name: Priyanka Indra Roll No.: 84 Dept: CSE1 Sem:6
Week 2 Coding Assignment Name: Priyanka Indra Roll No.: 84 Dept: CSE1 Sem:6
Output:
Enter a number:1243
Sum of digits of 1243= 10
Output:
Enter a number:6
6 is Perfect Number
Enter a number:23
23 is not Perfect Number
Output:
Enter the range:5
The series is:AMM,COO,EQQ,GSS,IUU,
Output:
Enter the range:10
The series is:ABA,BCB,CDC,DED,EFE,FGF,GHG,HIH,IJI,JKJ,
Output:
Enter a number:12345
Number of digits in 12345= 5
Output:
Enter a number:134
Reverse of 134= 431
Output:
Enter a number:121
121 is Palindrome Number
Enter a number:134
134 is not Palindrome Number
Output:
Enter a number:5
5 is Prime
Enter a number:6
6 is not Prime
Output:
Enter the value for n: 4
Sum of all first 4 prime numbers: 17
10. Read a number from STDIN, then display the sequence given below:
Input: 52934
Output: A5, B2, C9, D3, E4
Source Code:
#include<stdio.h>
int reverse(int n)
{
int rev=0;
while(n>0)
{
rev=rev*10+(n%10);
n=n/10;
}
return rev;
}
int main()
{
int num,rev=0;
char ch='A';
printf("Enter a number:");
scanf("%d",&num);
rev=reverse(num);
printf("The series is:");
while(rev>0)
{
printf("%c%d,",ch,(rev%10));
ch++;
rev=rev/10;
}
return 0;
}
Output:
Enter a number:52934
The series is:A5,B2,C9,D3,E4,
Output:
Enter two numbers:6 12
GCD= 6
LCM= 12
Output:
Enter the lower and upper range:20 40
The prime numbers in the range:23,29,31,37,
Output:
Enter a number:153
153 is Armstrong number
Enter a number:124
124 is not Armstrong number
Output:
Enter lower and upper range:50 200
The Armstrong numbers:153
Output:
Enter a number:6
Factors of 6: 1 2 3 6
switch (op) {
case '+':
printf("%.1lf + %.1lf = %.1lf", first, second, first + second);
break;
case '-':
printf("%.1lf - %.1lf = %.1lf", first, second, first - second);
break;
case '*':
printf("%.1lf * %.1lf = %.1lf", first, second, first * second);
break;
case '/':
printf("%.1lf / %.1lf = %.1lf", first, second, first / second);
break;
default:
printf("Error! operator is not correct");
}
return 0;
}
Output:
Enter an operator (+, -, *,): *
Enter two operands: 10 20
10.0 * 20.0 = 200.0
17. C program to check whether a number can be expressed as sum of two prime
numbers.
Source Code:
#include<stdio.h>
int sum_of_two_primes(int n);
int main()
{
int n,i;
printf("Enter a number:");
scanf("%d",&n);
int flag=0;
for(i=2;i<=n/2;++i)
{
if(sum_of_two_primes(i)==1)
{
if(sum_of_two_primes(n-i)==1)
{
printf("\n%d can be expressed as the sum of %d and %d\n",n,i,n-i);
flag=1;
}
}
}
if(flag==0)
printf("%d cannot be expressed as the sum of two prime numbers\n");
return 0;
}
int sum_of_two_primes(int n)
{
int i,isPrime=1;
for(i=2;i<=n/2;i++)
{
if(n%i==0)
{
isPrime=0;
break;
}
}
return isPrime;
}
Output:
Enter a number:20
20 can be expressed as the sum of 3 and 17
20 can be expressed as the sum of 7 and 13
Output:
Enter a positive number:5
Sum= 15
Output:
Enter a number:6
Factorial of 6 = 720
Output:
Enter two positive numbers:36 60
GCD of 36 and 60 = 12