Write a C program that uses a for loop to print out the first N odd numbers (whe
ID: 3690968 • Letter: W
Question
Write a C program that uses a for loop to print out the first N odd numbers (where N is a variable with its value set in your code). Turn In a file named odds.c Write a C program that uses a for loop to print out all the squares of the number between two variables: low and high as well as their sum (low and high should be variables that can be easily changed). So, for example, if your program is run with low = 3 and high A>> 6, your program should produce: 9 16 25 36 The sum of the squares from 3 to 6 is 86 You must use a for loop and turn in your answer to this problem in a file named for square. Write a C program that prints the first n prime numbers, where n Is an integer variable. Turn in primes.c. For example, if n = 5 your program should output: The first 5 primes are: 2 3 5 7 11Explanation / Answer
odds.c
#include<stdio.h>//input output header file
void main()
{//main function
int n,i;
printf("Enter the number: ");
scanf("%d",&n);//keyboard inputting
printf(" First %d odd natural numbers are: ",n);
for(i=1;i<=n;i=i+2)
{//logic fr n odd numbers
printf("%d ",i);// /t is used as tabs
}
printf(" ");//prints next line
}
output
sh-4.3$ main
Enter the number: 10
First 10 odd natural numbers are:
1 3 5 7 9
sum_of_squaresbet_two_num.c
#include <stdio.h>//header file for minput output function
int main()
//main function
{
int start, finish, total=0;
printf("Enter the starting number: ");
scanf("%d",&start);//keyboard inputting
printf("Enter the finishing number: ");
scanf("%d",&finish);
while (start <= finish)
{//while loop
total += start*start;
start++;
}
printf("the sum of squares between two numbers is %d ",total);
return 0;
}
output
sh-4.3$ main
Enter the starting number: 2
Enter the finishing number: 5
the sum of squares between two numbers is 54
no_of_primes.c
#include<stdio.h>//header file for input output function
int main()
{//main function
int no_of_primes, i = 3, k, j;
printf("Enter the number of prime numbers required ");
scanf("%d",&no_of_primes);//keyboard inputting
if ( no_of_primes >= 1 )
{//if statement
printf("First %d prime numbers are : ",no_of_primes);
printf("2 ");
}
for ( k = 2 ; k <= no_of_primes ; )
{//multiple loops
for ( j = 2 ; j <= i - 1 ; j++ )
{
if ( i%j == 0 )
break;
}
if ( j == i )
{
printf("%d ",j);
k++;
}
i++;
}
return 0;
}
output
sh-4.3$ main
Enter the number of prime numbers required
5
First 5 prime numbers are :
2
3
5
7
11
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.