0% found this document useful (0 votes)
768 views18 pages

Week 2 Coding Assignment Name: Priyanka Indra Roll No.: 84 Dept: CSE1 Sem:6

The document contains source code for 17 different C programming assignments, ranging from basic programs like calculating the sum of digits in a number to more advanced concepts like checking if a number is the sum of two prime numbers. For each assignment, the source code and sample output are provided. The assignments cover topics like loops, functions, conditionals, operators, and more to solve mathematical and logical problems.

Uploaded by

Priyanka Indra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
768 views18 pages

Week 2 Coding Assignment Name: Priyanka Indra Roll No.: 84 Dept: CSE1 Sem:6

The document contains source code for 17 different C programming assignments, ranging from basic programs like calculating the sum of digits in a number to more advanced concepts like checking if a number is the sum of two prime numbers. For each assignment, the source code and sample output are provided. The assignments cover topics like loops, functions, conditionals, operators, and more to solve mathematical and logical problems.

Uploaded by

Priyanka Indra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

WEEK 2 CODING ASSIGNMENT

Name: Priyanka Indra Roll No.: 84 Dept: CSE1 Sem:6th


1. Write a C program to find the sum of digit of a number.
Source Code:
#include<stdio.h>
int main()
{
int num,sum=0;
printf("Enter a number:");
scanf("%d",&num);
int num1=num;
while(num>0)
{
sum=sum+(num%10);
num=num/10;
}
printf("Sum of digits of %d= %d",num1,sum);
}

Output:
Enter a number:1243
Sum of digits of 1243= 10

2. Write a C program to check whether a number is perfect or not.


Source Code:
#include<stdio.h>
int main()
{
int i=1,num,sum=0;
printf("Enter a number:");
scanf("%d",&num);
while(i<num)
{
if(num%i==0)
sum=sum+i;
i++;
}
if(sum==num)
printf("%d is Perfect Number",num);
else
printf("%d is not Perfect Number",num);
}

Output:
Enter a number:6
6 is Perfect Number
Enter a number:23
23 is not Perfect Number

3. C program to display the sequence AMM, COO, EQQ,..........


Source Code:
#include<stdio.h>
int main()
{
char a='A',b='M';
int n;
printf("Enter the range:");
scanf("%d",&n);
printf("The series is:");
for(int i=1;i<=n;i++)
{
printf("%c%c%c,",a,b,b);
a+=2;
b+=2;
}
return 0;
}

Output:
Enter the range:5
The series is:AMM,COO,EQQ,GSS,IUU,

4. C program to display the sequence ABA, BCB, CDC, DED, ...................


Source Code:
#include<stdio.h>
int main()
{
char a='A';
int n;
printf("Enter the range:");
scanf("%d",&n);
printf("The series is:");
for(int i=1;i<=n;i++)
{
printf("%c%c%c,",a,(a+1),a);
a++;
}
return 0;
}

Output:
Enter the range:10
The series is:ABA,BCB,CDC,DED,EFE,FGF,GHG,HIH,IJI,JKJ,

5. C program to count number of digits in an integer.


Source Code:
#include<stdio.h>
int main()
{
int num,count=0;
printf("Enter a number:");
scanf("%d",&num);
int num1=num;
while(num>0)
{
int r=num%10;
count++;
num=num/10;
}
printf("Number of digits in %d= %d",num1,count);
return 0;
}

Output:
Enter a number:12345
Number of digits in 12345= 5

6. C program to reverse a number.


Source Code:
#include<stdio.h>
int main()
{
int num,num1,rev=0;
printf("Enter a number:");
scanf("%d",&num);
num1=num;
while(num>0)
{
rev=rev*10+(num%10);
num=num/10;
}
printf("Reverse of %d= %d",num1,rev);
return 0;
}

Output:
Enter a number:134
Reverse of 134= 431

7. C program to check whether a number is palindrome or not.


Source Code:
#include<stdio.h>
int main()
{
int num,num1,rev=0;
printf("Enter a number:");
scanf("%d",&num);
num1=num;
while(num>0)
{
rev=rev*10+(num%10);
num=num/10;
}
if(num1==rev)
printf("%d is Palindrome Number",num1);
else
printf("%d is not Palindrome Number",num1);
return 0;
}

Output:
Enter a number:121
121 is Palindrome Number
Enter a number:134
134 is not Palindrome Number

8. C program to check whether a number is prime or not.


Source Code:
#include<stdio.h>
int main()
{
int num,flag=0;
printf("Enter a number:");
scanf("%d",&num);
for(int i=2;i<=num/2;i++)
{
if(num%i==0)
{
printf("%d is not Prime",num);
flag=1;
break;
}
}
if(flag==0)
printf("%d is Prime",num);
return 0;
}

Output:
Enter a number:5
5 is Prime
Enter a number:6
6 is not Prime

9. C program to find the sum of first n prime numbers.


Source Code:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i,j=2,n,count=0,sum=0,flag=1;
printf("Enter the value for n: ");
scanf("%d",&n);
while(count<n){
for(i=2; i<=j-1; i++){
if(j%i==0){
flag=0;
break;
}
}
if(flag){
sum+=j;
count++;
}
j++;
flag=1;
}
printf("Sum of all first %d prime numbers: %d",n,sum);
return 0;
}

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,

11. C program to find GCD and LCM of two numbers.


Source Code:
#include<stdio.h>
int main()
{
int num1,num2,num=0,den=0,rem=0,gcd=0,lcm=0;
printf("Enter two numbers:");
scanf("%d\n%d",&num1,&num2);
if(num1>num2)
{
num=num1;
den=num2;
}
else
{
num=num2;
den=num1;
}
rem=num%den;
while(rem!=0)
{
num=den;
den=rem;
rem=num%den;
}
gcd=den;
lcm=(num1*num2)/gcd;
printf("GCD= %d\nLCM= %d",gcd,lcm);
}

Output:
Enter two numbers:6 12
GCD= 6
LCM= 12

12. C program to display prime numbers between two intervals.


Source Code:
#include<stdio.h>
int main()
{
int n1,n2,flag=0;
printf("Enter the lower and upper range:");
scanf("%d %d",&n1,&n2);
printf("The prime numbers in the range:");
while(n1<n2)
{
flag=0;
for(int i=2;i<=n1/2;++i)
{
if(n1%i==0)
{
flag=1;
break;
}
}
if(flag==0)
printf("%d,",n1);
++n1;
}
printf("\n");
return 0;
}

Output:
Enter the lower and upper range:20 40
The prime numbers in the range:23,29,31,37,

13. C program to check Armstrong number.


Source Code:
#include<stdio.h>
#include<math.h>
int main()
{
int num,count=0,res=0;
printf("Enter a number:");
scanf("%d",&num);
int num1=num,num2=num;
while(num1>0)
{
int r=num1%10;
count++;
num1=num1/10;
}
while(num2>0)
{
int r=num2%10;
res=res+(int)pow(r,count);
num2=num2/10;
}
if(res==num)
printf("%d is Armstrong number",num);
else
printf("%d is not Armstrong number",num);
return 0;
}

Output:
Enter a number:153
153 is Armstrong number
Enter a number:124
124 is not Armstrong number

14. C program to display Armstrong number between two intervals.


Source Code:
#include<stdio.h>
#include<math.h>
int isArmstrong(int num)
{
int num1=num,num2=num,count=0,res=0;
while(num1>0)
{
int r=num1%10;
count++;
num1=num1/10;
}
while(num2>0)
{
int r=num2%10;
res=res+(int)pow(r,count);
num2=num2/10;
}
if(res==num)
return 1;
else
return 0;
}
int main()
{
int n1,n2;
printf("Enter lower and upper range:");
scanf("%d %d",&n1,&n2);
printf("The Armstrong numbers:");
for(int i=n1;i<=n2;i++)
{
if(isArmstrong(i))
printf("%d ",i);
}
return 0;
}

Output:
Enter lower and upper range:50 200
The Armstrong numbers:153

15. C program to display factors of a number.


Source Code:
#include<stdio.h>
int main()
{
int i=1,num,sum=0;
printf("Enter a number:");
scanf("%d",&num);
printf("Factors of %d: ",num);
while(i<=num)
{
if(num%i==0)
printf("%d ",i);
i++;
}
return 0;
}

Output:
Enter a number:6
Factors of 6: 1 2 3 6

16. C Program to make a simple calculator using switch...case.


Source Code:
#include <stdio.h>
int main()
{
char op;
double first, second;

printf("Enter an operator (+, -, *,): ");


scanf("%c", &op);
printf("Enter two operands: ");
scanf("%lf %lf", &first, &second);

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

18. C program to find the sum of natural numbers using recursion.


Source Code:
#include<stdio.h>
int add(int n);
int main()
{
int num;
printf("Enter a positive number:");
scanf("%d",&num);
if(num>0)
printf("Sum= %d",add(num));
else
printf("Invalid Input\n");
return 0;
}
int add(int n)
{
if(n!=0)
return n+add(n-1);
else
return n;
}

Output:
Enter a positive number:5
Sum= 15

19. C program to find factorial of a number using recursion.


Source Code:
#include<stdio.h>
int fact(int n);
int main()
{
int num;
printf("Enter a number:");
scanf("%d",&num);
printf("Factorial of %d = %d",num,fact(num));
return 0;
}
int fact(int n)
{
if(n>0)
return n*fact(n-1);
else
return 1;
}

Output:
Enter a number:6
Factorial of 6 = 720

20. C program to find G.C.D using recursion.


Source Code:
#include<stdio.h>
int gcd(int n1,int n2);
int main()
{
int n1,n2;
printf("Enter two positive numbers:");
scanf("%d %d",&n1,&n2);
if(n1>0 && n2>0)
printf("GCD of %d and %d = %d",n1,n2,gcd(n1,n2));
else
printf("Invalid Input\n");
}
int gcd(int n1,int n2) {
if(n2!=0)
return gcd(n2,n1%n2);
else
return n1;
}

Output:
Enter two positive numbers:36 60
GCD of 36 and 60 = 12

You might also like