0% found this document useful (0 votes)
30 views11 pages

Week-5 To 7 Programs

Uploaded by

akshithabhi2812
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)
30 views11 pages

Week-5 To 7 Programs

Uploaded by

akshithabhi2812
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/ 11

Week5:

Draw Flow chart using RAPTOR to,

1. Display ‘n’ numbers using looping.

Flowchart: Program
#include<stdio.h>
int main(void)
{
int n,i;

printf(“Enter value of n\n”);


scanf(“%d”,&n);

printf(“Numbers from 1 to %d\n”,n);


for(i=1;i<=n;i++)
printf(“%d\n”,i);

return 0;
}

Input:Enter value of n Output:Numbers from 1 to 5


5 1
2
3
4
5
2. Calculate the sum of ‘n’ natural numbers.

Flowchart: Program
#include<stdio.h>
int main()
{
int n,i,sum=0;

printf(“Enter value of n\n”);


scanf(“%d”,&n);

for(i=1;i<=n;i++)
{
sum=sum+i;
}
printf(“Sum of %d natural numbers = %d\n”,n,sum);

return 0;
}

Input:Enter value of n Output: Sum of 5 natural numbers =


5 15
3. Calculate sum of even numbers and odd numbers from 1 to n (n value supplied by the user).

Flowchart: Program
#include<stdio.h>
int main(void)
{
int n,i,sumeven=0,sumodd=0;

printf(“Enter value of n\n”);


scanf(“%d”,&n);

for(i=1;i<=n;i++)
{
if(i%2==0)
sumeven=sumeven+i;
else
sumodd=sumodd+i;
}
printf(“Sum of even numbers from 1 to %d =
%d\n”,n,sumeven);
printf(“Sum of odd numbers from 1 to %d =
%d\n”,n,sumodd);

return 0;
}

Input: Enter value of n Output:Sum of even numbers from 1


5 to 5 = 6
Sum of odd numbers from 1 to 5 = 9
WEEK6 Date:

1. Write a C program to implement arithmetic calculator using switch- case.

#include <stdio.h>

int main()
{
int a, b;
char choice;
printf("Enter your choice\n");
printf("a. Addition\nb. Subtraction\nc. Multiplication\nd. Division\n ");
scanf("%c", &choice);
printf("Enter 2 integer numbers\n");
scanf("%d %d", &a, &b);

switch(choice)
{
case 'a': printf("%d + %d = %d\n", a, b, (a+b));
break;

case 'b': printf("%d - %d = %d\n", a, b, (a-b));


break;

case 'c': printf("%d x %d = %d\n", a, b, (a*b));


break;

case 'd': if( b != 0)


printf("%d / %d = %d\n", a, b, (a/b));
else
printf("Number can't be divided by 0\n");
break;

default: printf("You entered wrong choice\n");


break;
}
return 0;
}
INPUT:
Enter your choice
a. Addition
b. Subtraction
c. Multiplication
d. Division
OUTPUT:
a
Enter 2 integer numbers
2
4
2+4=6
2.Write a C program to find sum of n natural numbers.

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

printf("Enter a positive integer: ");


scanf("%d", &n);

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


sum += i;
}

printf("Sum = %d", sum);


return 0;
}

INPUT:
Enter a positive integer: 10
OUTPUT:
Sum = 55

Write a C program to find sum of individual digits of the given number.


#include<stdio.h>
int main(void)
{
int num,rem,sum=0,temp;
printf("enter a number\n");
scanf("%d",&num);
temp=num;
while(num>0)
{
rem=num%10;
sum=sum+rem;
num=num/10;
}
printf("Sum of digits in %d is %d\n", temp, sum);
return 0;
}

INPUT:
enter a number
1234
OUTPUT:
Sum of digits in 1234 is 10
4. Write a C program to find factorial of a given number

#include<stdio.h>
int main(void)
{
int n,i,prod=1;

printf("Enter a number\n");
scanf("%d",&n);

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

printf("Factorial of %d is %d\n",n,prod);

return 0;
}

INPUT:
Enter a number
5
OUTPUT:
Factorial of 5 is 120
WEEK7 Date

Write a C program to to check the given number is prime or not.

#include <stdio.h>
int main()
{
int n, i, count = 0;
printf("Enter any number n:");
scanf("%d", &n);
for (i = 1; i <= n; i++)
{
if (n % i == 0)
{
count++;
}
}
if (count == 2)
{
printf("n is a Prime number");
}
else
{
printf("n is not a Prime number");
}
return 0;
}

INPUT:
Enter any number n:5

OUTPUT:
n is a Prime number
2. Write a C Program to check the given number is Palindrome or not.

#include <stdio.h>
int main()
{
int number, t, rev=0, rmndr;

printf("Please enter a number to check Palindrome : ");


scanf("%d",&number);

t = number;
while (number > 0)
{
rmndr = number%10;
rev = rev*10 + rmndr;
number = number/10;
}
if(t == rev)
{
printf("%d is a palindrome", t);
}
else
{
printf("%d is not a palindrome", t);
}
return 0;
}

INPUT:
Please enter a number to check Palindrome : 121
OUTPUT:
121 is a palindrome
3.Write a C Program to display the prime numbers below n.

#include<stdio.h>
void main()
{
int i,j,n;

printf("Enter the number till which you want prime numbers\n");


scanf("%d",&n);

printf("Prime numbers are:-\n");


for(i=2;i<=n;i++)
{
int c=0;
for(j=1;j<=i;j++)
{
if(i%j==0)
{
c++;
}
}

if(c==2)
{
printf("%d ",i);
}
}
}

INPUT:
Enter the number till which you want prime numbers
20
OUTPUT:
Prime numbers are:-
2 3 5 7 11 13 17 19
WEEK-8

1.Write a C program to find GCD and LCM of two given numbers using functions
#include<stdio.h>
int GCD(int a,int b)
{
int temp,rem;
while(a!=0)
{
rem=b%a;
b=a;
a=rem;
}
return b;
}
int LCM(int a,int b)
{
return (a*b)/GCD(a,b); (
}
int main()
{
int a,b;
printf("enter two numbers\n");
scanf("%d%d",&a,&b);
printf(" GCD of %d,%d is %d\n",a,b,GCD(a,b));
printf("LCM of %d,%d is %d\n",a,b,LCM(a,b));
return 0;
}

OUTPUT:
enter two numbers
72
120
GCD of 72,120 is 24
LCM of 72,120 is 360
2.Write a C program to check the given number is Armstrong m number or not using functions.

#include <stdio.h>
void isArmstrong(int num);
int main()
{
int num;
printf("Enter the number:");
scanf("%d",&num);
isArmstrong(num);
return 0;
}
void isArmstrong(int num)
{
int qsum=0,r,temp;
temp=num;
while(num>0)
{
r=num%10;
qsum=qsum+(r*r*r);
num=num/10;
}
if(temp==qsum)
printf("%d is armstrong number",temp);
else
printf("%d is not armstrong number",temp);
}

OUTPUT:

Enter the number:153


153 is armstrong number

Enter the number:242


242 is not armstrong number

You might also like